Skip to content

ordinal: fix suffix for negative numbers - #269

Closed
simpleqt wants to merge 1 commit into
jmoiron:masterfrom
simpleqt:sq/r22-ordinal-negative
Closed

simpleqt wants to merge 1 commit into
jmoiron:masterfrom
simpleqt:sq/r22-ordinal-negative

Conversation

@simpleqt

@simpleqt simpleqt commented Oct 5, 2026

Copy link
Copy Markdown

Summary

ordinal indexes its suffix tables with value % 10 (and the 11/12/13 special case with value % 100), but Python's modulo maps negative numbers outside those ranges — -1 % 10 == 9 — so every negative number gets the generic th suffix:

>>> from humanize import ordinal
>>> ordinal(-1)
'-1th'        # expected '-1st'
>>> ordinal(-3)
'-3th'        # expected '-3rd'
>>> ordinal(-101)
'-101th'      # expected '-101st'

The fix selects the suffix on the absolute value, so negative ordinals get the same suffix as their positive counterparts while keeping the sign in the output.

Verification

  • Red/green: added -1/-2/-3/-4/-11/-111/-101 params to test_ordinal; 4 of them fail on master ('-3th' == '-3rd' etc.) and all pass with this change.
  • Full file: pytest tests/test_number.py → 108 passed, no regressions.
AI Disclosure

This PR was prepared with the assistance of an AI coding agent.

  • Tool(s): ZCode (GLM-based coding agent)
  • Used for: discovering the negative-modulo indexing bug via a display-format sweep, implementing the absolute-value selection, and running the red/green and full-file verification described above.

The suffix tables are indexed with value % 10 (and the 11/12/13
special case with value % 100), but Python's modulo maps negative
numbers outside those ranges: -1 % 10 == 9, so every negative number
got the generic 'th' suffix:

    >>> ordinal(-1)
    '-1th'
    >>> ordinal(-101)
    '-101th'

Select the suffix on the absolute value so negative ordinals get the
same suffix as their positive counterparts ('-1st', '-101st').
Copilot AI balanced review requested due to automatic review settings October 5, 2026 13:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@hugovk

hugovk commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

Wrong repo, but a duplicate of many others.

@hugovk hugovk closed this Oct 5, 2026
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.

3 participants