From 3a6d6cc7964e1761413220b97344e2d2a39c4b28 Mon Sep 17 00:00:00 2001 From: Arunendra21 <156455722+Arunendra21@users.noreply.github.com> Date: Wed, 5 Aug 2026 17:35:14 +0530 Subject: [PATCH] Fix fractional() formatting 0 as "0/1" instead of "0" fractional(0) returned "0/1" because the whole-number branch was gated on `whole_number` being truthy, which excludes 0. Every other integer (1, 2, -2, and so on) already returns a bare number, so 0 was the odd one out. The branch really just needs to check that no fractional part remains (numerator == 0), which holds for any whole number including 0. When the numerator is 0 the fraction is always 0/1, so the denominator == 1 check was redundant and is dropped. Adds test cases for 0, 0.0 and "0". Co-authored-by: eeshsaxena --- src/humanize/number.py | 6 +++--- tests/test_number.py | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index 2fb22c6..ba0cb1f 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -361,9 +361,9 @@ def fractional(value: NumberOrString) -> str: frac = Fraction(number - whole_number).limit_denominator(1000) numerator = frac.numerator denominator = frac.denominator - if whole_number and not numerator and denominator == 1: - # this means that an integer was passed in - # (or variants of that integer like 1.0000) + if not numerator: + # no fractional part remains, so an integer was passed in + # (including 0, or variants of an integer like 1.0000) return f"{whole_number:.0f}" if not whole_number: diff --git a/tests/test_number.py b/tests/test_number.py index 78639c3..9f8abd8 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -187,6 +187,9 @@ def test_apnumber(test_input: int | str, expected: str) -> None: (-1.3, "-1 3/10"), (-2.5, "-2 1/2"), (-0.5, "-1/2"), + (0, "0"), + (0.0, "0"), + ("0", "0"), ], ) def test_fractional(test_input: float | str, expected: str) -> None: