DECROMAX

Ordinal numbers for python

Jan 22nd, 2024
1,051
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.35 KB | Source Code | 0 0
  1. def add_number_suffix(n: int) -> str:
  2.     """Adds suffix to passed in number, ie 1 becomes 1st, 13 becomes 13th....
  3.    :param int number to add suffix
  4.    : returns str
  5.    """
  6.     if 4 <= n <= 20 or 24 <= n % 100 <= 30:  # teens
  7.         suffix = "th"
  8.     else:
  9.         suffix = {1: 'st', 2: 'nd', 3: 'rd'}.get(n % 10, 'th')
  10.     return f'{n}{suffix}'
Advertisement
Add Comment
Please, Sign In to add comment