CardinalToOrdinal(cardinalString)
Last updated May 26, 2003
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:
#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
Tayo Akinmade added
arrayTrim
4 day(s) ago
Will Belden added
longTime
9 day(s) ago
James Sleeman added
quickSort
19 day(s) ago
Ben Forta added
GetHostAddress
22 day(s) ago
Top Rated
EksporSQLData
Rated 5.0, 16 time(s)
backupDatabase
Rated 5.0, 13 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)