Ever written a giant case statement to add "-st", "-nd", "-rd", "-th" to display dates like "March 7th" or "September 1st"?
Converting numbers like 1, 2, 7, 19 into "1st", "2nd", "7th", "19th" is called ordinalizing.
ordinalize(number)
Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
Instead of manually add special cases, check if your language, library, or framework has a helper built-in.
For example, in Rails, you can call the ordinalize
method on a number or use the long_ordinal
date format.
> 3.ordinalize
=> "3rd"
> Date.today.to_s(:long_ordinal)
=> "September 29th, 2021"
Happy hacking!
Top comments (0)