Using Functions to Format Output

In addition to the ToString( ) method of numeric variables there are two useful functions that we can use to format output.

The Format( ) Function

The syntax of the Format function is

Format(data, “format name”)

where data is a variable, property, constant, or expression and the "format name" is one of several recognized format styles. Some of which are.....

Format name

Description

General Number

Display number with no thousand separators and no fixed number of decimal positions.

Currency

Display number with thousand separators, if appropriate; display two digits to the right of the decimal separator. Output is based on system locale settings.

Fixed

Display at least one digit to the left and two digits to the right of the decimal separator. Does not display thousand separators.

Standard

Display number with thousand separators, at least one digit to the left and two digits to the right of the decimal separator.

Percent

Display number multiplied by 100 with a percent sign (%) appended to the right; always display two digits to the right of the decimal separator.

For example:

Given decAmount = 100

lblAmtOut.Text = Format(decAmount,”currency”)

Will display $100.00 in the label  

More examples

Function Call

Output

Format (12345.628, "Standard")

12,345.63

Format (12345.628, "Currency")

 $12,345.63

Format (1 / 4, "Standard")

0.25

Format (-1234, "Currency")

($1,234.00)

Format (-1234, "Standard")

-1,234.00

Format (".2", "Currency")

$0.20

 

The FormatCurrency( ) Function

The syntax of the FormatCurrency function is

FormatCurrency(data, [#Decimals])

where data is a Numeric variable, property, constant, or expression. The #Decimal is an OPTIONAL argument specifying the number of decimal position to display. It omitted 2 decimal positions will be displayed.

For example:

Given decAmount = 100

lblAmtOut.Text = FormatCurrency(decAmount)

Will display $100.00 in the label  

More Examples

Function Call

Output

FormatCurrency (12345.75)

$12,345.75

FormatCurrency (12345.628)

$12,345.63  (value rounded)

FormatCurrency (12345.628, 0)

$12,346  (value rounded)

FormatCurrency (345)

$345.00

FormatCurrency (-1234.56)

($1,234.56)