Skip to content

Instantly share code, notes, and snippets.

@kmaida
Created April 24, 2015 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmaida/8d4e627d64eab45ccb06 to your computer and use it in GitHub Desktop.
Save kmaida/8d4e627d64eab45ccb06 to your computer and use it in GitHub Desktop.
Get an ordinal from a number
/**
* 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