Skip to content

Instantly share code, notes, and snippets.

@redoPop
Created February 24, 2010 19:39
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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"
@dubbs
Copy link

dubbs commented May 7, 2013

Thanks for the gist, I have an updated version here:
https://gist.github.com/dubbs/5535349

@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