Created
March 1, 2014 17:51
-
-
Save SpencerBingol/9293981 to your computer and use it in GitHub Desktop.
[JS] Quick solution to needing an ordinal suffix on a number. ( st nd rd th )
This file contains 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
// adds st nd rd th to number | |
// returns suffix-appended string or FALSE | |
function ordinal_suffix (num) { | |
var val=parseInt(num); | |
if ( isNaN(val) ) return false; | |
var mod_ten = Math.abs(val) % 10; | |
var mod_hun = Math.abs(val) % 100; | |
if ( (mod_ten == 1) && (mod_hun != 11) ) return val + "st"; | |
else if ( (mod_ten == 2) && (mod_hun != 12) ) return val + "nd"; | |
else if ( (mod_ten == 3) && (mod_hun != 13) ) return val + "rd"; | |
else return val + "th"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment