Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,14 @@ def ordinal(value: NumberOrString, gender: str = "male") -> str:
import math

try:
if not math.isfinite(float(value)):
return _format_not_finite(float(value))
value = int(value)
except (TypeError, ValueError):
except (TypeError, ValueError, OverflowError):
try:
float_value = float(value)
except (TypeError, ValueError):
return str(value)
if not math.isfinite(float_value):
return _format_not_finite(float_value)
return str(value)
gender = "male" if gender == "male" else "female"
digit = 0 if value % 100 in (11, 12, 13) else value % 10
Expand Down
27 changes: 27 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import math
import typing
from decimal import Decimal

import pytest

Expand All @@ -25,6 +26,12 @@
("102", "102nd"),
("103", "103rd"),
("111", "111th"),
(21.9, "21st"),
(Decimal("21.9"), "21st"),
(True, "1st"),
(False, "0th"),
("21.9", "21.9"),
("1e3", "1e3"),
("something else", "something else"),
(None, "None"),
(math.nan, "NaN"),
Expand All @@ -38,6 +45,26 @@ def test_ordinal(test_input: str, expected: str) -> None:
assert humanize.ordinal(test_input) == expected


@pytest.mark.parametrize("convert", [int, str, Decimal])
@pytest.mark.parametrize(
"last_digits, suffix",
[(1, "st"), (2, "nd"), (3, "rd"), (11, "th"), (12, "th"), (13, "th")],
)
def test_ordinal_large_integers(
convert: typing.Callable[[int], typing.Any], last_digits: int, suffix: str
) -> None:
value = 10**400 + last_digits
assert humanize.ordinal(convert(value)) == f"{value}{suffix}"


def test_ordinal_int_only_object() -> None:
class IntOnly:
def __int__(self) -> int:
return 21

assert humanize.ordinal(typing.cast("typing.Any", IntOnly())) == "21st"


@pytest.mark.parametrize(
"test_args, expected",
[
Expand Down