Posted By

kif on 08/16/06


Tagged

number ordinal


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

johnloy


Ordinal Number


 / Published in: JavaScript
 

1 -> 1st, 2 -> 2nd ...

  1. Number.prototype.ordinal = function () {
  2. return this + (
  3. (this % 10 == 1 && this % 100 != 11) ? 'st' :
  4. (this % 10 == 2 && this % 100 != 12) ? 'nd' :
  5. (this % 10 == 3 && this % 100 != 13) ? 'rd' : 'th'
  6. );
  7. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: bardonw on October 10, 2012

A really good definition for this function is found on Chris West's blog at http://gotochriswest.com/blog/2012/09/28/javascript-number-getordinalfor/.

You need to login to post a comment.