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
- Qwen2.5-Coder-32B is an LLM that can code well that runs on my Mac - 12th November 2024
- Visualizing local election results with Datasette, Observable and MapLibre GL - 9th November 2024
- Project: VERDAD - tracking misinformation in radio broadcasts using Gemini 1.5 - 7th November 2024