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";
		}
	}
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson