IntegerRankFormat(num)
Last updated December 23, 2002
Version: 1 | Requires: ColdFusion 5 | Library: StrLib
Description:
Takes an integer and gives it back to you formatted as a "rank". 1 becomes "1st", 2 becomes "2nd", 199329 becomes "199,329th" -- you get the idea.
Return Values:
Returns a string.
Example:
<cfoutput>#IntegerRankFormat(num)#<br></cfoutput>
</cfloop>
Parameters:
| Name | Description | Required |
|---|---|---|
| num | Number to format. | Yes |
Full UDF Source:
<cfscript>
/**
* Turn 1 into 1st, 2 into 2nd, etc.
*
* @param num Number to format. (Required)
* @return Returns a string.
* @author Nathan Dintenfass (nathan@changemedia.com)
* @version 1, December 23, 2002
*/
function IntegerRankFormat(number){
//grab the last digit
var lastDigit = right(number,1);
//grab the last two digits
var lastTwoDigits = right(number,2);
//use numberFormat() to put in commas for number larger than 999
number = numberFormat(number);
//11, 12, and 13 are special cases, so deal with them
switch(lastTwoDigits){
case 11:{
return number & "th";
}
case 12:{
return number & "th";
}
case 13:{
return number & "th";
}
}
//append the correct suffix based on the last number
switch(lastDigit){
case 1:{
return number & "st";
}
case 2:{
return number & "nd";
}
case 3:{
return number & "rd";
}
default:{
return number & "th";
}
}
}
</cfscript>
Search CFLib.org
Latest Additions
Tayo Akinmade added
arrayTrim
11 day(s) ago
Will Belden added
longTime
17 day(s) ago
James Sleeman added
quickSort
27 day(s) ago
Ben Forta added
GetHostAddress
30 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)