CFLib.org – Common Function Library Project

IntegerRankFormat(num)

Last updated December 23, 2002

author

Nathan Dintenfass

Version: 1 | Requires: CF5 | 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:

<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:

/**
 * 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";
        }
    }
}

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