Number formatting in java to use Lakh format instead of million format(java中的数字格式使用Lakh格式而不是百万格式)
问题描述
我尝试过使用 NumberFormat 和 DecimalFormat.即使我使用的是 en-In 语言环境,数字也被格式化为西方格式.是否有任何选项可以将数字格式化为 lakhs 格式?
I have tried using NumberFormat and DecimalFormat. Even though I am using the en-In locale, the numbers are being formatted in Western formats. Is there any option to format a number in lakhs format instead?
Ex - 我希望 NumberFormatInstance.format(123456) 给出 1,23,456.00 而不是 123,456.00 (例如,使用描述的系统此维基百科页面).
Ex - I want NumberFormatInstance.format(123456) to give 1,23,456.00 instead of 123,456.00 (e.g., using the system described on this Wikipedia page).
推荐答案
由于标准的 Java 格式化程序是不可能的,我可以提供自定义格式化程序
Since it is impossible with standard the Java formatters, I can offer a custom formatter
public static void main(String[] args) throws Exception {
System.out.println(formatLakh(123456.00));
}
private static String formatLakh(double d) {
String s = String.format(Locale.UK, "%1.2f", Math.abs(d));
s = s.replaceAll("(.+)(...\...)", "$1,$2");
while (s.matches("\d{3,},.+")) {
s = s.replaceAll("(\d+)(\d{2},.+)", "$1,$2");
}
return d < 0 ? ("-" + s) : s;
}
输出
1,23,456.00
这篇关于java中的数字格式使用Lakh格式而不是百万格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:java中的数字格式使用Lakh格式而不是百万格式
基础教程推荐
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
