Skip to content

Commit 45bf384

Browse files
committed
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.
1 parent 976484a commit 45bf384

2 files changed

Lines changed: 4 additions & 1 deletion

File tree

src/humanize/number.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def fractional(value: NumberOrString) -> str:
367367
if not whole_number:
368368
return f"{numerator:.0f}/{denominator:.0f}"
369369

370-
return f"{whole_number:.0f} {numerator:.0f}/{denominator:.0f}"
370+
return f"{whole_number:.0f} {abs(numerator):.0f}/{denominator:.0f}"
371371

372372

373373
def scientific(value: NumberOrString, precision: int = 2) -> str:

tests/test_number.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ def test_apnumber(test_input: int | str, expected: str) -> None:
178178
(1.5, "1 1/2"),
179179
(0.3, "3/10"),
180180
(0.333, "333/1000"),
181+
(-0.3, "-3/10"),
182+
(-1.3, "-1 3/10"),
183+
(-2.5, "-2 1/2"),
181184
(math.nan, "NaN"),
182185
(math.inf, "+Inf"),
183186
(-math.inf, "-Inf"),

0 commit comments

Comments
 (0)