Skip to content

Instantly share code, notes, and snippets.

@sudodoki
Created November 12, 2012 21:58
Show Gist options
  • Save sudodoki/4062257 to your computer and use it in GitHub Desktop.
Save sudodoki/4062257 to your computer and use it in GitHub Desktop.
Returning the original number and an ordinal suffix.
* @author Venkat K
* @see http://www.eggheadcafe.com/community/aspnet/3/43489/hi.aspx
* @param {Number} A positive number.
* @returns the original number and an ordinal suffix.
* @type String
Number.prototype.toOrdinal = function() {
var n = this % 100;
var suffix = ['th', 'st', 'nd', 'rd', 'th'];
var ord = n < 21 ? (n < 4 ? suffix[n] : suffix[0]) : (n % 10 > 4 ? suffix[0] : suffix[n % 10]);
return this + ord;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment