ColdFusion Add ordinal suffix to a number

Other languages include this functionality as part of their date formatting functions.  ColdFusion, for some reason, does not.

Either way, you may need to add an ordinal suffix to the a non-date related number.
<cfset thenumber = [WHATEVER YOUR NUMBER IS]>

<cfswitch expression="#thenumber Mod 10#">
	<cfcase value="1">
		<cfset thenumber = thenumber & "st">
	</cfcase>
	<cfcase value="2">
		<cfset thenumber  = thenumber  & "nd">
	</cfcase>
	<cfcase value="3">
		<cfset thenumber  = thenumber & "rd">
	</cfcase>
	<cfdefaultcase>
		<cfset thenumber = thenumber  & "th">
	</cfdefaultcase>
</cfswitch>
Posted by
Snippet Viewed 1781 times.

Share your ColdFusion code snippets:

  • Get some recognition & link back to your site.

 

Submit | Browse

Most Recent coldfusion snippets

Most Viewed