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
- Gemini 2.0 Flash: An outstanding multi-modal LLM with a sci-fi streaming mode - 11th December 2024
- ChatGPT Canvas can make API requests now, but it's complicated - 10th December 2024
- I can now run a GPT-4 class model on my laptop - 9th December 2024