NumberFormat

Formats a number with an optional format mask

Method Signature

NumberFormat(number=[number], mask=[string], locale=[string])

Arguments

Argument
Type
Required
Description
Default

number

number

true

The number to be formatted

mask

string

false

The formatting mask to apply

locale

string

false

An optional locale string to apply to the format

Examples

Format to 2 decimal places

<!--- 1234.00 ---><bx:output>1.234 ('__.00') ->  <!--- 1.23 --->#numberFormat( 1.234, "__.00" )#<br/> 
1234 ('__.00') -> #numberFormat( 1234, "__.00" )#<br/> </bx:output>

0 and 9 mask

<bx:output>
123 ('00000') -> #numberFormat( 123, "00000" )#<br/>
123 ('99999') -> #numberFormat( 123, "99999" )#<br/>
123 ('99.99999') -> #numberFormat( 123.12, "99.99999" )#<br/>
</bx:output>

_ mask

<bx:output>
123 ('_____') -> #numberFormat( 123, "_____" )#<br/>
123 ('_.___') -> #numberFormat( 123, "_.___" )#<br/>
11.10 ('__.000') -> #numberFormat( 11.10, "__.000" )#<br/>
</bx:output>

+ & - mask

<bx:output>
123 ('+') -> #numberFormat( 123, "+" )#<br/>
-123 ('-') -> #numberFormat( -123, "-" )#<br/>
</bx:output>

, comma

<bx:output>
123 (',') -> #numberFormat( 123, "," )#<br/>
123456 (',') -> #numberFormat( 123456, "," )#<br/>
</bx:output>

L,C mask

<bx:output>
1 ("L999") -> #NumberFormat( 1, "L999" )#<br/>
1 ("C000") -> #NumberFormat( 1, "C000" )#<br/>
</bx:output>

Two decimal places, decimal input

Run Example

numberFormat( 1.23, "__.00" );

Result: 1.23

Two decimal places, integer input

Run Example

numberFormat( 123, "__.00" );

Result: 123.00

Zero will pad zeros; nine doesn’t

Run Example

num1 = NumberFormat( 123, "00000" );
num2 = NumberFormat( 123, "99999" );
writeOutput( num1 & "/" & num2 );

Result: 00123/ 123

show positive/negative sign

Run Example

num1 = NumberFormat( 5, "+" );
num2 = NumberFormat( -5, "+" );
writeOutput( num1 & "/" & num2 );

Result: +5/-5

Formats to a dollar format (US)

You could use dollarFormat() or lsCurrencyFormat() instead

Run Example

numberFormat( 123456789, "_$,9.99" );

Result: 1,2,3,4,5,6,7,8,9.00$

Additional Examples

Run Example

dump( numberFormat( 1.234, "__.00" ) ); // 1.23
dump( numberFormat( 1234, "__.00" ) ); // 1234.00
// 0 and 9 mask
dump( numberFormat( 123, "00000" ) );
dump( numberFormat( 123, "99999" ) );
dump( numberFormat( 123.12, "99.99999" ) );
// _ mask
dump( numberFormat( 123, "_____" ) );
dump( numberFormat( 123, "_.___" ) );
dump( numberFormat( 11.10, "__.000" ) );
// + & - mask
dump( numberFormat( 123, "+" ) );
dump( numberFormat( -123, "-" ) );
// , comma
dump( numberFormat( 123, "," ) );
dump( numberFormat( 123456, "," ) );
// L,C mask
dump( NumberFormat( 1, "L999" ) );
dump( NumberFormat( 1, "C000" ) );

Last updated

Was this helpful?