From 45bf38448039bce472b826bec22cb2e7fbf4cd9a Mon Sep 17 00:00:00 2001 From: patchwright <292882882+patchwright@users.noreply.github.com> Date: Thu, 11 Jun 2026 22:50:02 +0200 Subject: [PATCH] Fix doubled negative sign in fractional() for negative mixed numbers fractional(-1.3) returned '-1 -3/10' instead of '-1 3/10', and fractional(-2.5) returned '-2 -1/2'. The whole-number part already carries the sign (int(-1.3) == -1), so the numerator should not repeat it. This was inconsistent with the pure-fraction case, which is already correct (fractional(-0.3) == '-3/10'). Use abs(numerator) in the mixed-fraction output. Add regression cases (-0.3, -1.3, -2.5) to the parametrized test_fractional, which previously had no negative inputs. --- src/humanize/number.py | 2 +- tests/test_number.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index f4dcb05d..5d0c8626 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -367,7 +367,7 @@ def fractional(value: NumberOrString) -> str: if not whole_number: return f"{numerator:.0f}/{denominator:.0f}" - return f"{whole_number:.0f} {numerator:.0f}/{denominator:.0f}" + return f"{whole_number:.0f} {abs(numerator):.0f}/{denominator:.0f}" def scientific(value: NumberOrString, precision: int = 2) -> str: diff --git a/tests/test_number.py b/tests/test_number.py index cb74fcf0..08719648 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -178,6 +178,9 @@ def test_apnumber(test_input: int | str, expected: str) -> None: (1.5, "1 1/2"), (0.3, "3/10"), (0.333, "333/1000"), + (-0.3, "-3/10"), + (-1.3, "-1 3/10"), + (-2.5, "-2 1/2"), (math.nan, "NaN"), (math.inf, "+Inf"), (-math.inf, "-Inf"),