Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 757
09Q71AO534 Avatar asked Nov 21 '13 13:11

09Q71AO534


1 Answers

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?
like image 113
BalusC Avatar answered Oct 02 '22 19:10

BalusC