Skip to content

Instantly share code, notes, and snippets.

@SpencerBingol
Created March 1, 2014 17:51
Show Gist options
  • Save SpencerBingol/9293981 to your computer and use it in GitHub Desktop.
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 )
// 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