java中的数字格式使用Lakh格式而不是百万格式

use*_*485 8 java formatting

我试过用NumberFormatDecimalFormat.即使我使用的是en-In区域设置,数字也会以西方格式进行格式化.有没有选项来格式化数字格式的数字呢?

恩-我想NumberFormatInstance.format(123456)1,23,456.00的,而不是123,456.00(例如,使用描述的系统这个维基百科页面).

Evg*_*eev 8

由于标准化Java格式化器是不可能的,我可以提供自定义格式化程序

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;
}
Run Code Online (Sandbox Code Playgroud)

产量

1,23,456.00
Run Code Online (Sandbox Code Playgroud)


Ian*_*rts 6

虽然标准Java数字格式化程序无法处理此格式,但ICU4J中DecimalFormat类可以.

import com.ibm.icu.text.DecimalFormat;

DecimalFormat f = new DecimalFormat("#,##,##0.00");
System.out.println(f.format(1234567));
// prints 12,34,567.00
Run Code Online (Sandbox Code Playgroud)


Ale*_*øld 2

这种格式是不可能的DecimalFormat。它只允许分组分隔符之间有固定数量的数字。

文档中:

分组大小是分组字符之间的恒定位数,例如 3 表示 100,000,000 或 4 表示 1,0000,0000。如果您提供具有多个分组字符的模式,则使用最后一个字符与整数结尾之间的间隔。所以“#,##,####,####”==“######,####”==“##,####,####”。

如果你想获得十万格式,你必须编写一些自定义代码。