Created
February 24, 2019 11:41
-
-
Save jonasraoni/ea0974af86889ac5abaeb78d109271c2 to your computer and use it in GitHub Desktop.
Number to ordinal (English)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function numberToOrdinal(n) { | |
const | |
number = Math.abs(n) || 0, | |
units = number % 10, | |
tens = ~~(number % 100 / 10), | |
suffix = new Map([ | |
[1, 'st'], | |
[2, 'nd'], | |
[3, 'rd'] | |
]); | |
return number + (number ? (tens !== 1 && suffix.get(units)) || 'th' : ''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment