问题描述
我有一个关于格式化卢比货币(印度卢比 - INR)的问题.
I have a question about formatting the Rupee currency (Indian Rupee - INR).
通常像 450500
这样的值被格式化并显示为 450,500
.在印度,相同的值显示为 4,50,500
Typically a value like 450500
is formatted and shown as 450,500
. In India, the same value is displayed as 4,50,500
例如,这里的数字表示为:
For example, numbers here are represented as:
1
10
100
1,000
10,000
1,00,000
10,00,000
1,00,00,000
10,00,00,000
参考印度编号系统
分隔符在两位数字之后,最后一组除外,以千为单位.
The separators are after two digits, except for the last set, which is in thousands.
我在互联网上搜索过,人们要求使用区域设置 en_GB
或模式 #,##,##,##,##0.00
I've searched on the internet and people have asked to use the locale en_GB
or pattern #,##,##,##,##0.00
我使用以下标签在 JSTL 上尝试了这个:
I tried this on JSTL by using the following tag:
<fmt:formatNumber value="${product.price}" type="currency"
pattern="#,##,##,##,###.00"/>
但这似乎并没有解决问题.
But this does not seem to solve the issue.
推荐答案
遗憾的是标准 Java SE DecimalFormat
不支持可变宽度组.所以它永远不会像你想要的那样完全格式化值:
Unfortunately on standard Java SE DecimalFormat
doesn't support variable-width groups. So it won't ever format the values exactly as you want to:
如果您提供具有多个分组字符的模式,则最后一个和整数末尾之间的间隔是使用的那个.所以 "#,##,###,####"== "######,####"== "##,####,####"
.
Java 中的大多数数字格式化机制都基于该类,因此继承了这个缺陷.
Most number formatting mechanisms in Java are based on that class and therefore inherit this flaw.
ICU4J(Unicode 国际组件的 Java 版本)提供了一个 NumberFormat
执行的类 支持这种格式:
ICU4J (the Java version of the International Components for Unicode) provides a NumberFormat
class that does support this formatting:
Format format = com.ibm.icu.text.NumberFormat.getCurrencyInstance(new Locale("en", "in"));
System.out.println(format.format(new BigDecimal("100000000")));
此代码将产生以下输出:
This code will produce this output:
Rs 10,00,00,000.00
注意:com.ibm.icu.text.NumberFormat
类不扩展java.text.NumberFormat
类(因为它已经扩展了 ICU 内部基类),它确实但是扩展了java.text.Format
类,它具有 format(Object)
方法.
Note: the com.ibm.icu.text.NumberFormat
class does not extend the java.text.NumberFormat
class (because it already extends an ICU-internal base class), it does however extend the java.text.Format
class, which has the format(Object)
method.
请注意,java 的 Android 版本.text.DecimalFormat
类是在幕后使用 ICU 实现的,并且确实以与 ICU 类本身相同的方式支持该功能(即使摘要错误地提到它是不支持).
Note that the Android version of java.text.DecimalFormat
class is implemented using ICU under the hood and does support the feature in the same way that the ICU class itself does (even though the summary incorrectly mentions that it's not supported).
这篇关于以印度编号格式显示货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!