是否 <f:convertNumber>使用模式格式化货币时使用正确的数字分隔符?

2022-01-17 00:00:00 numbers currency converter java jsf-2

假设我有以下 <f:convertNumber> 来格式化货币类型数字:

Suppose I have the following <f:convertNumber> to format a currency-type number:

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

在英语中,一百万是 1,000,000,但在德国,应该是 1.000.000.我的问题是:如果我使用上述模式,JSF 会知道指定语言环境的数字格式并自动使用正确的分隔符吗?

In English, a million would be 1,000,000 but in Germany, it should be 1.000.000. My question is: If I use the above pattern, will JSF be aware of the number format of the specified locale and automatically use the correct separator?

如果不是,如果您能告诉我如何格式化货币数字并同时指定正确的数字分隔符,我将不胜感激.

If not, I'd be very grateful if you could show me how I can format a currency number and at the same time specify the right number separator.

推荐答案

了解和理解 <f:convertNumber> 标签使用 DecimalFormat 在幕后.您可以在 其 javadoc 中找到所有模式字符.

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.

您似乎期望 DecimalFormat 模式字符 . 也实际用于最终格式.这是不真实的.它实际上只是模式字符(如使用的dMy等简单日期格式).基本上,模式字符 , 告诉 DecimalFormat 打印由给定语言环境指定的分组分隔符",等效地,模式字符 .告诉 DecimalFormat 打印由给定语言环境指定的小数分隔符".

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.

实际上,实际打印的字符取决于给定的语言环境.对于英语语言环境 (locale="en"),打印的分组分隔符"恰好也是 ,,但对于德语语言环境 (locale="de") 正在打印的分组分隔符"确实是 ..

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 ..

与具体问题无关,如果pattern,type属性完全没有作用 属性被指定.type 属性在此示例中基本上是多余的,可以安全地删除而不会产生任何副作用.

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.

如果您删除 pattern 属性,那么它实际上会被使用,并且您会看到正在使用货币的默认模式,这与 ¤# 相同,##0.00 用于英语语言环境,¤ #,##0.00 用于德语语言环境.还要注意表示货币符号的模式字符不是$,而是¤.货币符号可以通过 currencySymbol 属性单独设置.因此,货币的正确方法是:

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="$" />

再次,请参阅之前链接的 DecimalFormat javadoc.

Again, see the DecimalFormat javadoc as linked before.

相关文章