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

jsf, number-formatting

Solution

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:

- Does <f:convertNumber> use the right number separator when using patterns to format currency?

Problem

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: How can I achieve this?

Original source

Related problems