Skip to content

Fix fractional(0) returning "0/1" instead of "0"#351

Open
Sreekant13 wants to merge 1 commit into
python-humanize:mainfrom
Sreekant13:fix/fractional-zero
Open

Fix fractional(0) returning "0/1" instead of "0"#351
Sreekant13 wants to merge 1 commit into
python-humanize:mainfrom
Sreekant13:fix/fractional-zero

Conversation

@Sreekant13

Copy link
Copy Markdown

fractional(0) returns "0/1" instead of "0", even though every other whole number renders bare:

>>> import humanize
>>> humanize.fractional(0)
'0/1'   # expected '0'
>>> humanize.fractional(3)
'3'

The whole-number return branch was gated on whole_number being truthy, so 0 (where whole_number == 0) skipped it and fell through to the fraction branch, producing "0/1". Since numerator == 0 and denominator == 1 already uniquely identifies a whole number (including 0), the whole_number and check is both redundant and wrong for 0. Removing it makes fractional(0) return "0", consistent with fractional(3), fractional(2.0), etc.

Fixes # (no linked issue - small self-contained bug fix)

Changes proposed in this pull request:

  • Drop the redundant whole_number and guard in fractional() so 0 is treated as a whole number and returns "0" (previously "0/1").
  • Add regression test cases (0, "0") and (0.0, "0") to test_fractional.

fractional() gated the whole-number return on `whole_number` being truthy, so
0 (where whole_number == 0) fell through to the fraction branch and rendered as
"0/1". Since `numerator == 0 and denominator == 1` already uniquely identifies
a whole number (including 0), drop the redundant `whole_number and`. Now
fractional(0) returns "0". Added regression test cases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant