Does use the right number separator when using patterns to format currency?

前端 未结 1 554
暗喜
暗喜 2020-12-06 14:45

Suppose I have the following to format a currency-type number:



        
相关标签:
1条回答
  • 2020-12-06 15:12

    It's important to know and understand that the <f:convertNumber> tag uses DecimalFormat under the covers. You can find all pattern characters in its javadoc.

    It seems that you expected that the DecimalFormat pattern characters , and . are also actually used in the final format. This is untrue. It are really merely pattern characters (like as d, M, y, etc as used bySimpleDateFormat). Basically, the pattern character , tells DecimalFormat to print the "grouping separator" as specified by the given locale and, equivalently, the pattern character . tells DecimalFormat to print the "decimal separator" as specified by the given locale.

    In effects, the actual character being printed depends on the given locale. For English locale (locale="en"), the "grouping separator" being printed is just coincidentally also ,, but for German locale (locale="de") the "grouping separator" being printed is indeed ..


    Unrelated to the concrete problem, the type attribute of <f:convertNumber> has totally no effect if the pattern attribute is specified. The type attribtue is basically superfluous in this example and can safely be removed without any side effects.

    If you remove the pattern attribute, then it'll actually be used and you'll see that the default pattern for a currency is being used, which is same as ¤#,##0.00 for English locale and ¤ #,##0.00 for German locale. Also note that the pattern character representing the currency symbol is not $, but ¤. The currency symbol can be set separately via currencySymbol attribute. So the correct approach for currencies would be:

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

    Again, see the DecimalFormat javadoc as linked before.

    0 讨论(0)
提交回复
热议问题