diff --git a/src/humanize/number.py b/src/humanize/number.py index 6611a21..a112065 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -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): diff --git a/tests/test_number.py b/tests/test_number.py index eec6217..54cdbf7 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -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), ],