Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,12 @@ def fractional(value: NumberOrString) -> str:
if not whole_number:
return f"{numerator:.0f}/{denominator:.0f}"

return f"{whole_number:.0f} {numerator:.0f}/{denominator:.0f}"
# int() truncates toward zero, so for a negative number both
# whole_number and numerator carry the minus sign, which prints as
# "-1 -3/10". The sign already rides on the whole part; absorb it
# from the fractional part so the result reads as a normal mixed
# fraction.
return f"{whole_number:.0f} {abs(numerator):.0f}/{denominator:.0f}"


def scientific(value: NumberOrString, precision: int = 2) -> str:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ def test_apnumber(test_input: int | str, expected: str) -> None:
(-math.inf, "-Inf"),
("nan", "NaN"),
("-inf", "-Inf"),
(-1.3, "-1 3/10"),
(-2.5, "-2 1/2"),
(-0.5, "-1/2"),
],
)
def test_fractional(test_input: float | str, expected: str) -> None:
Expand Down
Loading