Skip to content

Instantly share code, notes, and snippets.

@sv-in
Forked from furf/LICENSE.txt
Created October 16, 2012 11:10
Show Gist options
  • Save sv-in/3898695 to your computer and use it in GitHub Desktop.
Save sv-in/3898695 to your computer and use it in GitHub Desktop.
JS: Return the ordinal suffix for a number.
function(
a // number
){
return[
"th","st","nd","rd" // array of ordinal suffixes
][
(a=~~ // floor the value for usable index (integer)
(a<0?-a:a) // absolute value for usable index (positive)
%100) // mod 100 to ensure teens edge case is handled every century
>10&&a<14 // handle teens edge case where 11-13 return "th"
||(a%=10) // mod 10 to check ones column digit
>3 // all digits above 3 return "th"
?0 // return "th" for above cases
:a // return "th", "st", "rd" for others
]
}
function b(a){return["th","st","nd","rd"][(a=~~(a<0?-a:a)%100)>10&&a<14||(a%=10)>3?0:a]}
Copyright (c) 2011 Dave Furfero, http://furf.com
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{
"name": "ordinal",
"description": "Return the ordinal suffix for a number.",
"keywords": ["ordinal","number","suffix","string"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment