CFLib.org – Common Function Library Project

IntegerRankFormat(num)

Last updated December 23, 2002

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

 
Rated 0 time(s). Average Rating: 0

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:

view plain print about
<cfloop list="1,2,3,4,5,11,12,13,45,1864203,19992,56311" index="num">
    <cfoutput>#IntegerRankFormat(num)#<br></cfoutput>
</cfloop>

Parameters:

Name Description Required
num Number to format. Yes

Full UDF Source:

view plain print about
<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>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
11 day(s) ago

Will Belden Will Belden added
longTime
17 day(s) ago

James Sleeman James Sleeman added
quickSort
27 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
30 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson