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.
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}" />
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" />
additionally, click here to view all the available locale in java.

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


No comments:

Post a Comment


Related Posts Plugin for WordPress, Blogger...