Ordinal

A python quick method to get your numbers postfixed with two letters in order to get an ordinal string version of a number. This is helpful when you want to display number in a more human way. The method also handles special case of number being or ending with 11, 12 and 13.

Pyton Ordinal

ORDINAL_LIST = ["th","st","nd","rd","th","th","th","th","th","th"]

def ordinal(value):
  if (value % 100 in (11,12,13)):
    suffix = "th"
  else:
    suffix = ORDINAL_LIST[ value % 10 ]
  return "%s%s" % (value, suffix)

examples

>>> ordinal(113)
"113th"
>>> ordinal(11)
"11th"
>>> ordinal(1)
"1st"
>>> ordinal(191)
"191st"
>>> ordinal(400)
"400th"