Created
September 26, 2012 21:30
-
-
Save mattbontrager/3790712 to your computer and use it in GitHub Desktop.
Returning a numeric suffix based on the number passed.
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
/** 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