I\'m wondering if there is a quick and easy way to output ordinals given a number in python.
For example, given the number 1
, I\'d like to output
Except for 1st, 2nd, and 3rd, I think they all just add th... 4th, 5th, 6th, 11th, 21st ... oh, oops ;-)
I think this might work:
def ordinal(num):
ldig = num % 10
l2dig = (num // 10) % 10
if l2dig == 1:
suffix = 'th'
elif ldig == 1:
suffix = 'st'
elif ldig == 2:
suffix = 'nd'
elif ldig == 3:
suffix = 'rd'
else:
suffix = 'th'
return '%d%s' % (num, suffix)