Thursday, September 19, 2013

Currency formatting with <f:convertNumber />

due to internationalization, the system should system the currency based on users' chosen currency or locale.
eg,
12345678.36 in US display as 12,345,678.36
12345678.36 in Danish (Denmark) display as 12.345.678,36
12345678.36 in German (Switzerland) display as 12'345'678.36

this could be easily handled by <f:convertNumber />, otherwise we might need to write a custom converter to handle this.

with <f:convertNumber />, we just need to pass in the system locale for the conversion.
<h:outputText id="balance" value="#{user.balance}">
    <f:convertNumber type="currency" locale="#{facesContext.viewRoot.locale}" />
</h:outputText>
with the above code, system will dynamically format the text value with system provided locale.

or we can prefix the locale,
<h:outputText id="balance" value="#{user.balance}">
    <f:convertNumber type="currency" locale="en_IN" />
</h:outputText>
additionally, click here to view all the available locale in java.

output:
zh_HK - HK$12,345,678.36
en_IN - Rs.12,345,678.36
de_CH - SFr. 12'345'678.36
en_US - $12,345,678.36
da_DK - kr 12.345.678,36


Done!!

LinkWithin

Related Posts Plugin for WordPress, Blogger...