Skip to content

Instantly share code, notes, and snippets.

@Stanback
Created August 23, 2016 20:14
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 Stanback/ad88d76c454f5c9962010f60abe2bee3 to your computer and use it in GitHub Desktop.
Save Stanback/ad88d76c454f5c9962010f60abe2bee3 to your computer and use it in GitHub Desktop.
Convert integer to an ordinal number (e.g. 1st, 2nd, 3rd, etc)
function getOrdinal(value) {
const suffixes = ['th', 'st', 'nd', 'rd'];
const normalized = value % 100;
return value + (suffixes[(normalized - 20) % 10] || suffixes[normalized] || suffixes[0]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment