Skip to content

Instantly share code, notes, and snippets.

@redoPop
Created February 24, 2010 19:39
Show Gist options
  • Save redoPop/313766 to your computer and use it in GitHub Desktop.
Save redoPop/313766 to your computer and use it in GitHub Desktop.
JavaScript: integer suffixes (1st, 2nd, 3rd...)
var suffix = function(n) {
var d = (n|0)%100;
return d > 3 && d < 21 ? 'th' : ['th', 'st', 'nd', 'rd'][d%10] || 'th';
};
// suffix(1) => "st"
// suffix(102) => "nd"
// suffix(113) => "th"
@Ehesp
Copy link

Ehesp commented Apr 3, 2014

@dubbs - Yours does not function correctly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment