Python snippet: ordinalth(n)
8th October 2003
Blogged so I don’t lose it (blogging as external memory):
def ordinalth(n):
""" 1 => 1st, 2 => 2nd etc """
last = n - n / 10 * 10
if last == 1:
return '%dst' % n
if last == 2:
return '%dnd' % n
if last == 3:
return '%drd' % n
return '%dth' % n
Update: See comments for improved version.
More recent articles
- My AI/LLM predictions for the next 1, 3 and 6 years, for Oxide and Friends - 10th January 2025
- Weeknotes: Starting 2025 a little slow - 4th January 2025
- I still don't think companies serve you ads based on spying through your microphone - 2nd January 2025