Below are two similar techniques for generating ordinal numbers such as 1st, 2nd, 3rd, from simple integers such as 1, 2, 3 respectively. I needed this for a fairly complex user interface where I wanted to setup a tool tip that said 'Select 1st choice:', 'Select 2nd choice', etc.
Below are both a method and a function. You only need one, not both. All that differs is how you call them. The documentation in both show the differences in sample uses.
Number.prototype.ordinal = function () {
//-------------------------------------------------------------------------
//-- O R D I N A L
//-------------------------------------------------------------------------
//-- Generic: Yes, for ECMAScript, ExtendScript, and JavaScript
//-------------------------------------------------------------------------
//-- Purpose: To take a number and return a string with that number
//-- converted to an ordinal number. Thus 1 becomes 1st. 2 becomes 2nd.
//-- 3 becomes 3rd. 12 becomes 12th.
//-------------------------------------------------------------------------
//-- Arguments: none, this is a method.
//-------------------------------------------------------------------------
//-- Calls: Nothing.
//-------------------------------------------------------------------------
//-- Returns: a string with an ordinal version of the number unless the
//-- original number is not deemed to be an integer.
//-------------------------------------------------------------------------
//-- Sample Uses:
//-- Number(13).ordinal() ;
//-- for ( i = 0 ; 100 >= i ; i++ ) {
//-- $.writeln( i.ordinal() );
//-- }
//-------------------------------------------------------------------------
//-- Notes: Tested for 0 through 200.
//-------------------------------------------------------------------------
//-- Modified: 2009.09.07 by Jon S. Winters of electronic publishing support
//-- original, undocumented concept un acknowledged but listed at:
//-- "http://querylog.com/q/javascript+string+ordinal"
//-- eps@electronicpublishingsupport.com
//-- For additional information on cardinal numbers, check out:
//-- http://en.wikipedia.org/wiki/Names_of_numbers_in_English#Ordinal_numbers
//-------------------------------------------------------------------------
//-- Start by varifying that the orginal is an integer
var asInt = parseInt ( this ) ;
if ( isNaN ( asInt ) ) { return this ; }
//-- Make a string version of the text to allow regular expression
//-- matching for the numbers.
var asString = this.toString();
//-- Do a series of if statements to concatinate the appropriate
//-- suffix to the number. All 1, 2, 3 except for 11, 12, 13
//-- have common suffixs. All others are th's.
//-- Check for items ending with 1 except for 11 and add st
if ( asInt == 1 || asString.match( new RegExp ( '[^1]1$' ) ) ) return asInt + 'st' ;
//-- Check for items ending with 2 except for 12 and add nd
if ( asInt == 2 || asString.match( new RegExp ( '[^1]2$' ) ) ) return asInt + 'nd' ;
//-- Check for items ending with 3 except for 13 and add rd
if ( asInt == 3 || asString.match( new RegExp ( '[^1]3$' ) ) ) return asInt + 'rd' ;
//-- All but these end with th. This includes 0.
return asInt + 'th';
}
function ordinal ( value ) {
//-------------------------------------------------------------------------
//-- O R D I N A L
//-------------------------------------------------------------------------
//-- Generic: Yes, for ECMAScript, ExtendScript, and JavaScript
//-------------------------------------------------------------------------
//-- Purpose: To take a number and return a string with that number
//-- converted to an ordinal number. Thus 1 becomes 1st. 2 becomes 2nd.
//-- 3 becomes 3rd. 12 becomes 12th.
//-------------------------------------------------------------------------
//-- Arguments: one, the numberical value. This should be an integer.
//-------------------------------------------------------------------------
//-- Calls: Nothing.
//-------------------------------------------------------------------------
//-- Returns: a string with an ordinal version of the number unless the
//-- original number is not deemed to be an integer.
//-------------------------------------------------------------------------
//-- Sample Use: ordinal(13)
//-------------------------------------------------------------------------
//-- Notes: Tested for 0 through 200.
//-------------------------------------------------------------------------
//-- Modified: 2009.09.07 by Jon S. Winters of electronic publishing support
//-- original, undocumented concept un acknowledged but listed at:
//-- "http://querylog.com/q/javascript+string+ordinal"
//-- eps@electronicpublishingsupport.com
//-- For additional information on cardinal numbers, check out:
//-- http://en.wikipedia.org/wiki/Names_of_numbers_in_English#Ordinal_numbers
//-------------------------------------------------------------------------
//-- Start by varifying that the orginal is an integer
//-- return the original value if it is not
var asInt = parseInt ( value ) ;
if ( isNaN ( asInt ) ) { return value ; }
//-- Make a string version of the text to allow regular expression
//-- matching for the numbers.
var asString = value.toString();
//-- Do a series of if statements to concatinate the appropriate
//-- suffix to the number. All 1, 2, 3 except for 11, 12, 13
//-- have common suffixs. All others are th's.
//-- Check for items ending with 1 except for 11 and add st
if ( asInt == 1 || asString.match( new RegExp ( '[^1]1$' ) ) ) return asInt + 'st' ;
//-- Check for items ending with 2 except for 12 and add nd
if ( asInt == 2 || asString.match( new RegExp ( '[^1]2$' ) ) ) return asInt + 'nd' ;
//-- Check for items ending with 3 except for 13 and add rd
if ( asInt == 3 || asString.match( new RegExp ( '[^1]3$' ) ) ) return asInt + 'rd' ;
//-- All but these end with th. This includes 0.
return asInt + 'th';
}