Skip to content

Instantly share code, notes, and snippets.

@leftis
Last active August 29, 2015 14:06
Show Gist options
  • Save leftis/8b43cda4ae8bfe4b4bf6 to your computer and use it in GitHub Desktop.
Save leftis/8b43cda4ae8bfe4b4bf6 to your computer and use it in GitHub Desktop.
Number.prototype.ordinate = function(){
var num = this,
numStr = num.toString(),
last = numStr.slice(-1),
len = numStr.length,
ord = '';
switch (last) {
case '1':
ord = numStr.slice(-2) === '11' ? 'th' : 'st';
break;
case '2':
ord = numStr.slice(-2) === '12' ? 'th' : 'nd';
break;
case '3':
ord = numStr.slice(-2) === '13' ? 'th' : 'rd';
break;
default:
ord = 'th';
break;
}
return num.toString() + ord;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment