Skip to content

Commit

Permalink
Handle numeric inputs to ordinal without first casting to string. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jul 3, 2023
1 parent 1b75d1d commit 27b2157
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
15 changes: 6 additions & 9 deletions inflect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3602,31 +3602,28 @@ def present_participle(self, word: Word) -> str:

# NUMERICAL INFLECTIONS

@validate_arguments
def ordinal(self, num: Word) -> str:
@validate_arguments(config=dict(arbitrary_types_allowed=True))
def ordinal(self, num: Union[Number, Word]) -> str:
"""
Return the ordinal of num.
num can be an integer or text
e.g. ordinal(1) returns '1st'
ordinal('one') returns 'first'
"""
if DIGIT.match(str(num)):
if isinstance(num, (int, float)):
if isinstance(num, (float, int)) and int(num) == num:
n = int(num)
else:
if "." in str(num):
try:
# numbers after decimal,
# so only need last one for ordinal
n = int(num[-1])
n = int(str(num)[-1])

except ValueError: # ends with '.', so need to use whole string
n = int(num[:-1])
n = int(str(num)[:-1])
else:
n = int(num)
n = int(num) # type: ignore
try:
post = nth[n % 100]
except KeyError:
Expand Down
1 change: 1 addition & 0 deletions newsfragments/178.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``ordinal`` now handles float types correctly without first coercing them to strings.

0 comments on commit 27b2157

Please sign in to comment.