Fix ordinal() suffix for negative integers - #411
Closed
zixuniaowu wants to merge 1 commit into
Closed
zixuniaowu wants to merge 1 commit into
zixuniaowu wants to merge 1 commit into
Conversation
ordinal(-1) returned '-1th' instead of '-1st' (and similarly for -2, -3, -21, -101, ...): the suffix digit was computed from value % 100 / value % 10, and Python's modulo maps negative operands to negative-free results (-1 % 10 == 9), so every negative number got 'th'. Compute the suffix from the absolute magnitude instead. Adds negative parametrized cases and doctest examples.
Member
This comment was marked as low quality.
This comment was marked as low quality.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
humanize.ordinal()returns the wrong suffix for every negative integer:The docstring documents "Works for any integer", and negative inputs are clearly within the promised domain (invalid types fall back to
str(value)), so the behavior contradicts the contract.Root cause
The suffix digit is computed from
value % 100/value % 10. Python's%maps negative operands to non-negative residues (-1 % 10 == 9), so all negative numbers land in the"th"digit bucket. The-11/-12/-13/-111/-112/-113inputs are "accidentally correct" and mask the defect.Fix
Normalize the magnitude before computing the digit; the sign is still applied by the outer f-string:
Also adds two doctest examples plus 12 parametrized negative-integer tests (covering the five genuinely wrong values
-1/-2/-3/-21/-101and the "accidentally correct" families that previously hid the bug).Verification
392aef7) with only the new tests: 5 failed, 25 passed — proves the defect exists upstream, not introduced by this changepytest --doctest-modules src/humanize/number.py: 8 passed (Python 3.12)Base for this branch:
392aef7(currentmainat time of branching, 2026-09-16).