Created
November 12, 2012 21:58
-
-
Save sudodoki/4062257 to your computer and use it in GitHub Desktop.
Returning the original number and an ordinal suffix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* @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