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.
@Sreekant13

Copy link
Copy Markdown
Author

Hi @hugovk, gentle nudge on this small fix whenever you have time. Thanks for maintaining humanize.

@Sreekant13

Copy link
Copy Markdown
Author

Flagging an overlap, since I only noticed it now: @semx opened #354 a few days after this one, and it covers the same fractional(0) case plus a second one this PR does not touch, where the fractional part rounds up to a whole number and produces output like "2 1/1" for fractional(2.9999999).

Their fix folds any whole-valued fractional part into the integer part, which handles both cases in one place, so it is the more complete change of the two. I am happy to close this in favour of #354 if that is the direction you prefer, or to add their case here if you would rather keep this one. Whichever is less work for you.

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