Skip to content

Instantly share code, notes, and snippets.

@lesleh
Created January 18, 2015 17:09
Show Gist options
  • Select an option

  • Save lesleh/41a30ca4e0a22c5ef82a to your computer and use it in GitHub Desktop.

Select an option

Save lesleh/41a30ca4e0a22c5ef82a to your computer and use it in GitHub Desktop.
Get the suffix for a given number, e.g. 2nd, 3rd, 4th.
/*
1st
2nd
3rd
4th
11th
12th
21st
22nd
121st
122nd
1023rd
*/
module.exports = function(num) {
var tens = (num % 100 / 10).toFixed(0);
var units = num % 10;
if(tens == 1) {
return 'th';
}
switch(units) {
case 1:
return 'st';
case 2:
return 'nd';
case 3:
return 'rd';
default:
return 'th';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment