Set decimal separator when using f:convertNumber

纵然是瞬间 提交于 2019-11-26 08:33:08

问题


I want to know how to set the default decimal separator on my JSF application. I have some <h:inputText> that I need to format as money, with 2 decimals. Right now the decimal separator used by default is the comma , and this gives me an error when I do some operations on save. I don\'t know if I can set the decimal separator to be used everytime that I use <f:convertNumber> tag.

I tried to use this:

<f:convertNumber pattern=\"########0.00\" groupingUsed=\"false\" />

but it still sets the comma as decimal separator.


回答1:


The default decimal separator depends on the locale used. You can set it in 2 ways:

  1. On a per-view basis by the locale attribute of the <f:view> tag:

    <f:view locale="#{bean.locale}">
    
  2. On a per-converter basis by the locale attribute of the <f:convertNumber> tag:

    <f:convertNumber locale="#{bean.locale}" />
    

It's unclear what locale you're targeting, but the use of . as fraction separator is typical for US dollars with a locale of en-US, for example. So you need to set it as such:

<f:convertNumber type="currency" currencySymbol="$" locale="en-US" />

It can also be obtained from a java.util.Locale bean property.

<f:convertNumber type="currency" currencySymbol="$" locale="#{bean.locale}" />

Note that I used type="currency", that's more self-documenting.

See also:

  • Does <f:convertNumber> use the right number separator when using patterns to format currency?
  • Localization in JSF, how to remember selected locale per session instead of per request/view


来源:https://stackoverflow.com/questions/7856152/set-decimal-separator-when-using-fconvertnumber

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!