pp108 : Sprintf Function for Formatting Currency

Sprintf Function for Formatting Currency

This topic describes the sprintf function for formatting currency.


You can use the sprintf function to customize the currency format such that both the value and the denomination are displayed in various formats.

The following table lists the formats supported by the sprintf function for formatting currency:

Table 1. Sprintf Function for Formatting Currency

Format

Description

%dd.dd

Currency value including a decimal separator

%g(3,2)

Sets off two digits to the left of the decimal separator and then every third digit. The output, for example, could be 1,123,123,12.00.

%g(3)

Sets off digits to the left of the decimal separator into groups of three. The output, for example could be: 1,123,123,123.00.

%s(.)

Specifies period (.) to be the decimal separator

%S(,)

Specifies comma (,) to be the thousands separator

%n(-$n)

Specifies the negative currency mode where $ denotes the currency symbol and n the currency value

%p($n)

Specifies the positive currency mode where $ denotes the currency symbol and n the currency value

%c($)

Specifies $ to be the currency symbol



Note: The sprintf function for formatting currency can be used in the following way:

sprintf("%C(format)", Amount)


The following are the examples of the output generated by this function:

  • sprintf("%C(%ddd.dd%g(3,3))",123123123)will return the currency as 123,123,123.00
  • sprintf("%C(%g(3,2)%s(.))", 123123123)will return the currency as 1,231,231,23.00