gpt4 book ai didi

java - 以印度编号格式显示货币

转载 作者:IT老高 更新时间:2023-10-28 20:34:11 38 4
gpt4 key购买 nike

我有一个关于格式化卢比货币(印度卢比 - INR)的问题。

通常像 450500 这样的值会被格式化并显示为 450,500。在印度,相同的值显示为 4,50,500

例如,这里的数字表示为:

1
10
100
1,000
10,000
1,00,000
10,00,000
1,00,00,000
10,00,00,000

请参阅 Indian Numbering System

分隔符在两位数之后,最后一组除外,以千为单位。

我在互联网上搜索过,有人要求使用区域设置 en_GB 或模式 #,##,##,##,##0.00

我使用以下标记在 JSTL 上进行了尝试:

<fmt:formatNumber value="${product.price}" type="currency" 
pattern="#,##,##,##,###.00"/>

但这似乎并不能解决问题。

最佳答案

不幸的是,在标准 Java SE 上 DecimalFormat不支持可变宽度组。因此,它永远不会完全按照您的意愿格式化值:

If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####".

Java 中的大多数数字格式化机制都基于该类,因此继承了这个缺陷。

ICU4J(International Components for Unicode 的 Java 版本)提供了 NumberFormat 确实支持这种格式的类:

Format format = com.ibm.icu.text.NumberFormat.getCurrencyInstance(new Locale("en", "in"));
System.out.println(format.format(new BigDecimal("100000000")));

这段代码会产生这样的输出:

Rs 10,00,00,000.00

注意:com.ibm.icu.text.NumberFormat 扩展java.text.NumberFormat类(因为它已经扩展了 ICU 内部的基类),但它确实扩展了 java.text.Format类,具有 format(Object)方法。

请注意 Android version of java.text.DecimalFormat类是在底层使用 ICU 实现的,并且 确实 以与 ICU 类本身相同的方式支持该功能(即使摘要错误地提到它支持)。

关于java - 以印度编号格式显示货币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5379231/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com