Skip to content

Instantly share code, notes, and snippets.

@mattbontrager
Created September 26, 2012 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattbontrager/3790712 to your computer and use it in GitHub Desktop.
Save mattbontrager/3790712 to your computer and use it in GitHub Desktop.
Returning a numeric suffix based on the number passed.
/** If your number is 534, and you want it to return 534th,
* Use it like so:
* Suffix.init(534);
**/
Suffix: {
init: function(theNum) {
var self = this,
toCheck,
theSuffix;
if (theNum) {
if (typeof theNum === 'number') {
theNum = theNum.toString();
}
toCheck = theNum.substr(theNum.length - 1);
if (theNum.length > 1) {
switch(toCheck) {
case '0':
theSuffix = 'th';
break;
case '1':
theSuffix = 'st';
break;
case '2':
theSuffix = 'nd';
break;
case '3':
theSuffix = 'rd';
break;
default:
theSuffix = 'th';
break;
}
}
else {
switch(toCheck) {
case '0':
theSuffix = '';
break;
case '1':
theSuffix = 'st';
break;
case '2':
theSuffix = 'nd';
break;
case '3':
theSuffix = 'rd';
break;
default:
theSuffix = 'th';
break;
}
}
if (theSuffix) {
return theSuffix;
}
}
else {
Pulse.OnError('no number was passed');
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment