PDA

View Full Version : ColdFusion samples for formatting numbers with NumberFormat



Brian
July 9th, 2009, 09:58 AM
It is easy to control the way numbers are formatted in ColdFusion using the NumberFormat function. Here is a sample, where we seed the number, and then display it with three different formats using the NumberFormat function:


<cfset TestNum = -12345678.9>
<cfoutput>
#NumberFormat(TestNum, ',')#<BR>
#NumberFormat(TestNum, '$,')#<BR>
#NumberFormat(TestNum, '()$,_________.00')#<BR>
</cfoutput>

The resulting display would be:


-12,345,679
-$12,345,679
$( 12,345,678.90)

The first line just tells it to display the number, with every 3rd decimal place separated by with a comma.

The second line does the same as the first line, but additionally displays a $ to the left of the number.

The third line builds on the first two lines, by having the number how with () to indicate a negative number instead of the default - sign, and also has a specific set of place holder's using the _ and also requires the cents to be formatted, including the .00 placeholders if needed.

GuidoTSarducci
February 28th, 2012, 05:24 PM
What is the best datatype to use for a telephone number and how do you format
it in ColdFusion to appear like this (999) 999-9999 ?

jakeh
February 28th, 2012, 05:57 PM
Hi,

You would just store the phone number as a normal number, then format it after retrieving it from the database:


<cfset phone=9189189118>

<cfoutput>
<cfscript>
areacode = left(phone, 3);
firstthree = mid(phone, 4, 3);
lastfour = right(phone, 4);

</cfscript>
phone_formatted = "(#areacode#)#firstthree#-#lastfour#"
</cfoutput>


A more elegant solution would be to create your own function for this as shown at this tutorial (http://tutorial9.easycfm.com/) from easycfm.com.

GuidoTSarducci
March 1st, 2012, 09:52 AM
I followed your "elegant solution" advice and created a udl and it works great! Thanx!