Skip to content

ordinal() emits wrong suffixes for negative integers #536

Not planned

Description

@SonicHedghog

Summary

ordinal() produces incorrect suffixes for negative integers whose absolute values end in 1, 2, or 3.

Reproduction (humanize 4.16.0)

import humanize

humanize.ordinal(-1)    # '-1th'    (expected '-1st')
humanize.ordinal(-21)   # '-21th'   (expected '-21st')
humanize.ordinal(-22)   # '-22th'   (expected '-22nd')
humanize.ordinal(-101)  # '-101th'  (expected '-101st')

Why this seems wrong

The docstring says ordinal "works for any integer". The implementation uses value % 10 (and % 100 for the teen exception), which yields negative residues for negative inputs in Python only when... actually Python's % returns non-negative residues, but the comparisons against positive literals 1/2/3 and the 11-13 teen exception don't align for negative operands, so negatives ending in 1/2/3 (outside the exception window) all fall through to 'th'.

Possible resolutions

  • Apply the suffix logic to abs(value); or
  • Document that negatives are unsupported.

Activity

  1. SonicHedghog commented on Dec 2, 2026

    @SonicHedghog
    Author

    If this behavior is unwanted, I’d be happy to open a small PR applying the suffix selection to abs(value), with regression tests for negative values ending in 1/2/3 and the teen exceptions. I’ll wait for maintainer direction before proceeding.

  2. hugovk commented on Dec 2, 2026

    @hugovk
    Member

    What does "-1st" even mean? Please check for duplicate issues and PRs.

  3. SonicHedghog commented on Dec 2, 2026

    @SonicHedghog
    Author

    You’re right to question it. I checked for duplicates: I don’t see an earlier issue for this exact behavior, but there have already been multiple PR attempts, including PR 405 still open and PR 321 closed, where the same semantic concern was raised. I don’t have a compelling real-world use for “-1st”; I was reading “any integer” in the docstring too literally. I’ll close this rather than add more duplicate noise. Thanks for the correction.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions