Skip to content

Fix ordinal() suffix for negative numbers - #405

Open
itzzdev09 wants to merge 1 commit into
python-humanize:mainfrom
itzzdev09:ordinal-negative
Open

itzzdev09 wants to merge 1 commit into
python-humanize:mainfrom
itzzdev09:ordinal-negative

Conversation

@itzzdev09

@itzzdev09 itzzdev09 commented Sep 18, 2026

Copy link
Copy Markdown

Calling ordinal() on a negative integer gives the wrong suffix:

>>> ordinal(-1)
'-1th'
>>> ordinal(-9)
'-9st'
>>> ordinal(-21)
'-21th'

The suffix is chosen from value % 10 and value % 100, but Python's modulo of a negative number counts up from the next lower multiple (-9 % 10 == 1, -1 % 10 == 9), so the digit used for the lookup is wrong for every negative value.

Computing the digit from abs(value) fixes it while leaving non-negative values, the 11/12/13 special case, and the non-finite / non-numeric paths unchanged:

>>> ordinal(-1)
'-1st'
>>> ordinal(-9)
'-9th'
>>> ordinal(-21)
'-21st'

Added the negative cases to test_ordinal; the full test_number.py suite passes.

ordinal() computed the suffix from value % 10 and value % 100, but Python's
modulo on a negative number gave the wrong digit, so e.g. ordinal(-9) was
"-9st" and ordinal(-1) was "-1th". Use the magnitude instead.
@hugovk

hugovk commented Sep 18, 2026

Copy link
Copy Markdown
Member

Please see questions on duplicate #321.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants