Created
January 18, 2015 17:09
-
-
Save lesleh/41a30ca4e0a22c5ef82a to your computer and use it in GitHub Desktop.
Get the suffix for a given number, e.g. 2nd, 3rd, 4th.
This file contains hidden or 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
| /* | |
| 1st | |
| 2nd | |
| 3rd | |
| 4th | |
| 11th | |
| 12th | |
| 21st | |
| 22nd | |
| 121st | |
| 122nd | |
| 1023rd | |
| */ | |
| module.exports = function(num) { | |
| var tens = (num % 100 / 10).toFixed(0); | |
| var units = num % 10; | |
| if(tens == 1) { | |
| return 'th'; | |
| } | |
| switch(units) { | |
| case 1: | |
| return 'st'; | |
| case 2: | |
| return 'nd'; | |
| case 3: | |
| return 'rd'; | |
| default: | |
| return 'th'; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment