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

java中的数字格式设置为使用Lakh格式而不是百万格式,java,formatting,Java,Formatting,我试过使用NumberFormat和DecimalFormat。尽管我在locale中使用了en,但数字的格式是西方格式的。是否有任何选项可以将数字改为10万个格式 例如,我希望NumberFormatInstance.format(123456)给出1,23456.00,而不是123456.00(例如,使用上面描述的系统)。这种格式不可能使用DecimalFormat。它只允许分组分隔符之间有固定位数 从: 分组大小是分组之间的固定位数 字符,例如3代表100000000或4代表1000000

我试过使用
NumberFormat
DecimalFormat
。尽管我在locale中使用了
en,但数字的格式是西方格式的。是否有任何选项可以将数字改为10万个格式


例如,我希望
NumberFormatInstance.format(123456)
给出
1,23456.00
,而不是
123456.00
(例如,使用上面描述的系统)。

这种格式不可能使用
DecimalFormat
。它只允许分组分隔符之间有固定位数

从:

分组大小是分组之间的固定位数 字符,例如3代表100000000或4代表100000000。如果你 提供具有多个分组字符的模式,间隔 在最后一个和整数末尾之间是 用过。所以“#,#,#,#,#,#,#,#,#,#,#,#,#,==”、#,#,#,#,#,#,#,#


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

由于标准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;
}

虽然标准Java数字格式化程序无法处理这种格式,但Java数字格式化程序可以


如果我不想要十进制怎么办??
1,23,456.00
import com.ibm.icu.text.DecimalFormat;

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