如何从逗号更改的DecimalFormat的小数点分隔点/点?小数点、逗号、DecimalFormat

2023-09-11 20:18:21 作者:与孤独有染

我有BigDecimal的值转换成好的,可读的字符串这个有点疯狂的方法。

I have this little crazy method that converts BigDecimal values into nice and readable Strings.

private String formatBigDecimal(BigDecimal bd){
    DecimalFormat df = new DecimalFormat();
    df.setMinimumFractionDigits(3);
    df.setMaximumFractionDigits(3);
    df.setMinimumIntegerDigits(1);
    df.setMaximumIntegerDigits(3);
    df.setGroupingSize(20);
    return df.format(bd);
}

据然而,也产生了所谓的分组分隔符这就是所有我的价值观出来是这样的:

It however, also produces a so called grouping separator "," that makes all my values come out like this:

xxx,xxx

我需要的分隔符是一个点或一个点,而不是逗号。 是否有人对如何完成这一壮举的小线索?

I do need the separator to be a dot or a point and not a comma. Does anybody have a clue of how to accomplish this little feat?

我已经阅读这,特别的这个死刑,但现在我找不到得到这个工作的方式。 难道我处理这个错误的方式?是否有这样做的更优雅的方式?甚至一个解决方案,占不同的本地号码重presentations,因为逗号将是完美的欧洲标准。

I have read this and in particular this to death now but I cannot find a way to get this done. Am I approaching this the wrong way? Is there a much more elegant way of doing this? Maybe even a solution that accounts for different local number representations, since the comma would be perfect by European standards.

推荐答案

您可以通过设置一个区域设置或使用DecimalFormatSymbols.

You can change the separator either by setting a locale or using the DecimalFormatSymbols.

如果你想分组分隔符是一个点,您可以使用欧洲语言环境:

If you want the grouping separator to be a point, you can use an european locale:

NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
DecimalFormat df = (DecimalFormat)nf;

另外,您可以使用的DecimalFormatSymbols类来改变出现在由格式​​方法产生的格式的数字符号。这些符号包括小数点分隔符,分组分隔符,减号,和百分号,其中包括:

Alternatively you can use the DecimalFormatSymbols class to change the symbols that appear in the formatted numbers produced by the format method. These symbols include the decimal separator, the grouping separator, the minus sign, and the percent sign, among others:

DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.'); 
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);
 
精彩推荐
图片推荐