Created
April 24, 2015 14:46
-
-
Save kmaida/8d4e627d64eab45ccb06 to your computer and use it in GitHub Desktop.
Get an ordinal from a number
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
/** | |
* Get ordinal value | |
* | |
* @param n {number} if a string is provided, % will attempt to convert to number | |
* @returns {string} th, st, nd, rd | |
*/ | |
function getOrdinal(n) { | |
var ordArr = ['th', 'st', 'nd', 'rd']; | |
var modulus = n % 100; | |
return ordArr[(modulus - 20) % 10] || ordArr[modulus] || ordArr[0]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment