# Formatting Currencies ColdFusion Help

ColdFusion provides the Do11arFormat function to display a number as a currency formatted with two decimal places a comma as a thousands separator and a dollar sign. Negative values are placed in parentheses

Like Decimal Format Do11arFormat takes a single argument: Doll arFormat(Number). Therefore Do11arFormat(123456. 789) produces the following output:

\$123,456.79

Using a negative number such as Doll arFormat( -123456.789). produces a number surrounded by parentheses

(\$123,456.79)

Formatting Numbers

Coldl-usion provides two useful functions for formatting numeric values

FormatBaseN (Number I Base) Return” a number as a string in the specified base

NumberFormat (Number. Mask) Retu rns J number ,b .’ C ‘c;tl”T’. fc1rmatted string

Using FormatBaseN

FormatBaseN is a simple function: Given a number in base 10 and a base it returns the number in that base. Table 12.3 shows examples of the function and the results that are returned.

TAB l E 1 2 . 3:

Using NumberFormat

Number Format is a flexible function for outputting numeric values. Given a number and an optional mask a custom formatted number is returned. Without a mask the number is returned as an integer with a comma as a thousands separator

The mask can be built out of the following characters

_ or 9 Adigit placeholder used to define the number of digits to display and their placement. (Optional)
Specifies the location of the decimal point. (Required)
o Used to the left or right of the decimal point; forces padding with zeros. (Optional)
o Places parentheses around the number when it is less than zero. (Optional)
+ Places a plus sign in front of positive numbers and, a minus sign in front of negative
numbers. (Optional)
– Places a space in front of positive numbers and a minus sign in front of negative
numbers. (Optional)
• Separates thousands with commas. (Optional)
L Specifies left justification of the number within the width of the mask. Must. appear
as the first character of the mask. (Optional)
C’ Specifies center justification of the number within the width of the mask. Must
appear as the first character of the mask. (Optional)
S Places a dollar sign in front of the number. Must appear as the first character of the
” Separates left formatting from right formatting. (Optional) .
This may all sound complex, but it’s not. Let’s start with a simple example: You want
to display a number as a currency with up to five digits before the decimal place and

two after and you want decimal places to line up when multiple numbers are displayed in a column.

To format numbers as described, you need to use the \$_. _. mask in a series of numbers like this:

<PRE><CFOUTPUT> #NumberFormat(123.45,’S .__ ‘)#
#NumberFormat(2345. 567,’ S__ .__ ‘)#
#NumberFormat(3.4,’ 5_,_. __ ‘)#
#NumberFormat(45678,,’ S •__ ‘)#
</CFOUTPUT></PRE>

The result is shown in Figure 12.8

FIGURE 12.8

What happens if Yl;?\l don’t want all the dollar signs to line up at the left but instead toappear immediately next to each number? Allaire’s documentation indicates that this can be done by placing an underscore to the left of the dollar sign in the mask:

Used in our example above, this produces results like those in Figure 12.9

FIGURE 12.9

The same rule holds true when using + or -‘in your masks. A note is required with regard to aligning a list of positive and negative numbers: If no sign is specified for a mask (that is no + or – in the mask) then numbers will not line up in a column. For instance the following code:

<PRE><CFOUTPUT>
#NumberFormat(l23.45, ‘_._’)#
#NumberForma’:(-123.45, ‘_._’)#
</CFOUTPUT>

produces the following output

123.45
-123.45

To line up the decimal places you .can use a +in the mask (“+_._ “)as follows

+123.45
-123.45

or you can use a – in the mask (” -_._ d):

123.45
-123.45

Formatting HTML Code

At times it is necessary to display HTML code rather than allow it to be parsed by the browser. For instance normally the following HTML code:

<HR)
<Hl>This is a test</Hl)
Just Testing.
<HR>

produces output similar to that in Figure 12.10

FIGURE 12.10

What would happen though if you wanted to display a code sample and then show how it would appear on the same page? To display the HTML code rather than allow the browser to inte l.l:( It, you must escape the code.For instance the < character needs to be converted to &1t; and > to &gt. In our example this would mean that the HTML would need to be converted to the following

&1t ;HR&gt;
&It;Hl&gt;This ;s a test&lt;/Hl&gt;
Just Testing.
&1 t.;HR&gt

In addition you either need to add line breaks in the form of <BR> or surround the code by <PRE> and </PRE> to separate the lines in the final browser display.

In addition, you either need to add line breaks in the form of <BR> or surround the code by <PRE> and </PRE> to separate the lines in the final browser display.

HTMLCodeFormat Escapes the string (that is, converts < to &1t;, and so on), surrounds the string in <PRE> and </PRE>, and removes all carriage returns from the string

HTMLEditFormat Escapes the string and removes all carriage returns from the string Both functions take two arguments: a string as the first argument and an optional version indicating the version of HTML

can be one of the following values:

1 Indicates the latest version of HTML.

2.0 Indi cates HTML 2. (This is the default value when a version is not provided.) So, how does this all differ? If the HTML code you used in the preceding example is stored in the string variable Sampl eHTML,then HTMLCodeFormat(Sample HTML)produces output like that in Figure 12.11 whereas HTML Edit Format (SampleHTML) produces output like that in Figure 12.12.

FIGURE 12.11

FIGURE 12.12

ColdFusion also provides two functions for manipulating the carriage returns in a body of text: ParagraphFormat and Str; peR.

Paragraph Format takes a string that mayor may not include HLML code and converts single new lines into spaces and double new lines into <P> tags to create paragraph breaks. In this way, a body of text like the following

This is line one.
This is line two.

This is line three.

that normally would be displayed by a browser as

This is line one. This is line two. This is line three

can be converted into valid HTML:

This is line one. This is line two.
<p>
This is line three

To use Paraqraph format. simply provide a string as the single argument to the function: Paragraph Format(String) .

Stri peR does what the name suggests: it strips carriage returns out of a string of text. This can be useful when displaying text using <PRE>that has been entered in the TEXTAREAfield of a form. To use Str; pCR,simply provide a string as an argument: St r ipCR(Str; ng).

### Related ColdFusion Assignments

Posted on November 16, 2015 in Using Advance Query Techniques