Skip to content
Closed
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: 5 additions & 2 deletions src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,12 @@ def ordinal(value, gender="male"):
P_("8 (female)", "th"),
P_("9 (female)", "th"),
)
if value % 100 in (11, 12, 13): # special case
# Python's modulo keeps negative numbers out of the 0-9/11-13 ranges the
# suffix tables are indexed with, so select on the absolute value
n = abs(value)
if n % 100 in (11, 12, 13): # special case
return f"{value}{t[0]}"
return f"{value}{t[value % 10]}"
return f"{value}{t[n % 10]}"


def intcomma(value, ndigits=None):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
("102", "102nd"),
("103", "103rd"),
("111", "111th"),
("-1", "-1st"),
("-2", "-2nd"),
("-3", "-3rd"),
("-4", "-4th"),
("-11", "-11th"),
("-111", "-111th"),
("-101", "-101st"),
("something else", "something else"),
(None, None),
],
Expand Down