09Q71AO534
09Q71AO534

Reputation: 4440

Display amount in format $###,###,###.## using f:convertNumber

I would like to display the amount in $12,050,999.00 format.

I tried as follows:

<h:outputText value="#{sampleBean.Amount}">
    <f:convertNumber pattern="###,###" currencySymbol="$" type="currency"/>
</h:outputText>

However, it didn't display the amount in the desired format. I got 12,050,999 instead.

The desired format is shown in the below image:

enter image description here

How can I achieve this?

Upvotes: 12

Views: 51120

Answers (1)

BalusC
BalusC

Reputation: 1108802

Your pattern is wrong for a currency. You should be using pattern="¤#,##0.00".

<f:convertNumber pattern="¤#,##0.00" currencySymbol="$" />

However, there's more at matter: in your original code you also specified the type attribute, which is correct, but this is mutually exclusive with the pattern attribute whereby the pattern attribute gets precedence.

You should actually be omitting the pattern attribute and stick to the type attribute.

<f:convertNumber type="currency" currencySymbol="$" />

Note that this uses the locale as available by UIViewRoot#getLocale() which is expected to be an English/US based locale in order to get the right final format for the USD currency. You'd like to explicitly specify it in either the <f:view>:

<f:view locale="en_US">

or in the locale attribute of the <f:convertNumber>:

<f:convertNumber type="currency" currencySymbol="$" locale="en_US" />

See also:

Upvotes: 28

Related Questions