CFLib.org – Common Function Library Project

GetOrdinal(num)

Last updated November 5, 2003

author

Mark Andrachek

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Takes a number as an argument, and returns the 2 letter english text ordinal appropriate for the number. For example, the number 1 would return "st" for 1st, and so on.

Return Values:
Returns a string.

Example:

<CFSET myordinal = GetOrdinal(Day(Now()))>

<CFOUTPUT>
Today is the #Day(Now())#<SUP>#variables.myordinal#</SUP> of #MonthAsString(Month(Now()))#
</CFOUTPUT>

Parameters:

Name Description Required
num Number you wish to return the ordinal for. Yes

Full UDF Source:

/**
 * Returns the 2 character english text ordinal for numbers.
 * 
 * @param num 	 Number you wish to return the ordinal for. (Required)
 * @return Returns a string. 
 * @author Mark Andrachek (hallow@webmages.com) 
 * @version 1, November 5, 2003 
 */
function GetOrdinal(num) {
  // if the right 2 digits are 11, 12, or 13, set num to them.
  // Otherwise we just want the digit in the one's place.
  var two=Right(num,2);
  var ordinal="";
  switch(two) {
       case "11": 
       case "12": 
       case "13": { num = two; break; }
       default: { num = Right(num,1); break; }
  }

  // 1st, 2nd, 3rd, everything else is "th"
  switch(num) {
       case "1": { ordinal = "st"; break; }
       case "2": { ordinal = "nd"; break; }
       case "3": { ordinal = "rd"; break; }
       default: { ordinal = "th"; break; }
  }

  // return the text.
  return ordinal;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson