You need to format a non-currency number for output.

The decimalFormat() function is similar to the dollarFormat() function. You pass in a number, and it will return a formatted string. However, the string will only be formatted with two decimal places and a thousandths separator.

<cfset testNum = -537>
<cfoutput>#deciamalFormat(testNum)#</cfoutput>
-537.00

The numberFormat() function gives you much more control of your formatted output. Along with passing it a number to format, you also pass in a formatting mask. This mask can include information such as digit placeholders, commas, padding with 0?s, and many other options. One common use for this extra formatting is to better organize display of different sized numbers.

Take the following example without numberFormat():

<cfset testNum = -537>
<cfset testNum2 = 5735>
<cfset testNumTotal = testNum + testNum2>
<cfoutput>
#testNum#<br>
#testNum2#<br>
---------<br>
#testNumTotal#
</cfoutput>

Running this code produces this output:

-537
5735
---------
5198

And with numberFormat():

<cfset testNum = -537>
<cfset testNum2 = 5735>
<cfset testNumTotal = testNum + testNum2>
<cfoutput>
#numberFormat(testNum,?-$_,____.__?)#<br>
#numberFormat(testNum2,?-$_,____.__?)#<br>
----------<BR>
#numberFormat(testNumTotal,?-$_,____.__?)#
</cfoutput>

Running this code produces this output:

-$  537.00
 $5,735.00
----------
 $5,198.00

As you can see, the output from the numberFormat() block is much easier to read.

This question was written by Jeremy Petersen
It was last updated on January 9, 2006.

Categories

Numbers

Comments

comments powered by Disqus