CFLib.org – Common Function Library Project

GetOrdinal(num)

Last updated November 05, 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;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson