本文介绍了<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会知道指定locale的数字格式并自动使用正确的分隔符吗?

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.

推荐答案

了解并理解 标签使用 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 ..

与具体问题无关type属性完全没有效果,如果pattern 属性被指定.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.

这篇关于&lt;f:convertNumber&gt;使用模式格式化货币时使用正确的数字分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 12:58