CFLib.org – Common Function Library Project

CardinalToOrdinal(cardinalString)

Last updated May 26, 2003
Download UDF

author

Howard Fore                                       Howard Fore

Version: 1 | Requires: ColdFusion 5 | Library: StrLib

Description:
Takes a cardinal number string and converts it to an ordinal number string. This is particularly useful when combined with the NumberAsString UDF.

Return Values:
Returns a string.

Example:

<cfoutput>
#CardinalToOrdinal("two hundred thirteen")#
</cfoutput>

Parameters:

Name Description Required
cardinalString Cardinal string to format. Yes

Full UDF Source:

<cfscript>
/**
* Convert cardinal number strings to ordinal number strings.
*
* @param cardinalString      Cardinal string to format. (Required)
* @return Returns a string.
* @author Howard Fore (me@hofo.com)
* @version 1, May 26, 2003
*/

function CardinalToOrdinal(cardinalString) {
var resultString = ""; // Generated result to return
var lastCardinal = ""; // Last word in cardinal number string
var TempNum = 0; // temp integer

cardinalSpecialStrings = "One,one,Two,two,Three,three,Four,four,Five,five,Six,six,Eight,eight,Nine,nine,Twelve,twelve";
ordinalSpecialStrings = "First,first,Second,second,Third,third,Fourth,fourth,Fifth,fifth,Sixth,sixth,Eighth,eighth,Ninth,ninth,Twelfth,twelfth";

cardinalString = trim(cardinalString);
lastCardinal = listLast(cardinalString," ");
resultString = ListDeleteAt(cardinalString,ListLen(cardinalString," ")," ");

// Is lastCardinal a special case?
TempNum = listFindNoCase(cardinalSpecialStrings,lastCardinal);
if (TempNum GT 0) {
    resultString = ListAppend(resultString,ListGetAt(ordinalSpecialStrings,TempNum)," ");
} else {
if (ListFindNoCase(Right(lastCardinal,2),"en"))
{
// Last word ends with "en", add "th"
resultString = ListAppend(resultString,lastCardinal & "th"," ");
}
if (ListFindNoCase(Right(lastCardinal,1),"d"))
{
// Last word ends with "d", add "th"
resultString = ListAppend(resultString,lastCardinal & "th"," ");
}
if (ListFindNoCase(Right(lastCardinal,1),"y"))
{
// Last word ends with "y", delete "y", add "ieth"
resultString = ListAppend(resultString, Left(lastCardinal,Len(lastCardinal) - 1) & "ieth"," ");
}
if (ListFindNoCase(Right(lastCardinal,3),"ion"))
{
// Last word ends with "ion", add "th"
resultString = ListAppend(resultString,lastCardinal & "th"," ");
}
}
return resultString;
}
</cfscript>

Search CFLib.org


Latest Additions

Jose Diaz-Salcedo Jose Diaz-Salcedo added
cfRssFeed
2 day(s) ago

Raymond Compton Raymond Compton added
structBlend
23 day(s) ago

Duncan Duncan added
IsZIPUK
23 day(s) ago

Todd Sharp Todd Sharp added
getTagContentAll
29 day(s) ago

Gerald Guido Gerald Guido added
ListReturnDuplicat...
1 month(s) ago

Created by Raymond Camden / Design by Justin Johnson