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

发布于 2024-10-18 08:32:21 字数 779 浏览 5 评论 0原文

我有一个疯狂的小方法,可以将 BigDecimal 值转换为漂亮且可读的字符串。

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);
}

然而,它还会产生一个所谓的分组分隔符 "," ,它使我的所有值都像这样出现:

xxx,xxx

我确实需要分隔符是一个点或一个点,而不是逗号。 有人知道如何完成这个小壮举吗?

我已阅读,特别是这个现在已经死了,但我找不到办法完成这个任务。 我是否以错误的方式处理这个问题?有没有更优雅的方法来做到这一点?甚至可能是考虑不同本地数字表示的解决方案,因为按照欧洲标准,逗号是完美的。

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

短暂陪伴 2024-10-25 08:32:21

您可以通过设置区域设置或使用 来更改分隔符小数格式符号

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

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

或者,您可以使用 DecimalFormatSymbols 类来更改 format 方法生成的格式化数字中出现的符号。这些符号包括小数点分隔符、分组分隔符、减号和百分号等:

DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.'); 
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);

currentLocale 可以从 Locale.getDefault() 获取,即:

Locale currentLocale = Locale.getDefault();

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;

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);

currentLocale can be obtained from Locale.getDefault() i.e.:

Locale currentLocale = Locale.getDefault();
生生不灭 2024-10-25 08:32:21

欧洲相当大。我不确定他们是否都使用相同的格式。但是这个这个答案会有帮助。

String text = "1,234567";
NumberFormat nf_in = NumberFormat.getNumberInstance(Locale.GERMANY);
double val = nf_in.parse(text).doubleValue();

NumberFormat nf_out = NumberFormat.getNumberInstance(Locale.UK);
nf_out.setMaximumFractionDigits(3);
String output = nf_out.format(val);

即使用正确的区域设置。

Europe is quite huge. I'm not sure if they use the same format all over. However this or this answer will be of help.

String text = "1,234567";
NumberFormat nf_in = NumberFormat.getNumberInstance(Locale.GERMANY);
double val = nf_in.parse(text).doubleValue();

NumberFormat nf_out = NumberFormat.getNumberInstance(Locale.UK);
nf_out.setMaximumFractionDigits(3);
String output = nf_out.format(val);

I.e. use the correct locale.

烟凡古楼 2024-10-25 08:32:21

这对我来说有效:

DecimalFormat df2 = new DecimalFormat("#.##");           
df2.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ENGLISH));

This worked in my case:

DecimalFormat df2 = new DecimalFormat("#.##");           
df2.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ENGLISH));
静若繁花 2024-10-25 08:32:21
public String getGermanCurrencyFormat(double value) {
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
    nf.setGroupingUsed(true);
    return "€ " + nf.format(value);
}
public String getGermanCurrencyFormat(double value) {
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
    nf.setGroupingUsed(true);
    return "€ " + nf.format(value);
}
渔村楼浪 2024-10-25 08:32:21

BigDecimal 似乎不尊重区域设置。

Locale.getDefault(); //returns sl_SI

斯洛文尼亚语言环境应有小数点逗号。我想我对数字有一些奇怪的误解。

a = new BigDecimal("1,2") //throws exception
a = new BigDecimal("1.2") //is ok

a.toPlainString() // returns "1.2" always

我编辑了消息中没有任何意义的部分,因为事实证明这是由于人为错误(忘记提交数据并且正在查看错误的内容)。

对于任何 Java .toString() 函数都可以说与 BigDecimal 相同。我想这在某些方面是好的。例如序列化或调试。有一个独特的字符串表示形式。

正如其他人提到的,使用格式化程序也可以正常工作。只需使用格式化程序,对于 JSF 前端也是如此,格式化程序可以正确完成工作并了解区域设置。

BigDecimal does not seem to respect Locale settings.

Locale.getDefault(); //returns sl_SI

Slovenian locale should have a decimal comma. Guess I had strange misconceptions regarding numbers.

a = new BigDecimal("1,2") //throws exception
a = new BigDecimal("1.2") //is ok

a.toPlainString() // returns "1.2" always

I have edited a part of my message that made no sense since it proved to be due the human error (forgot to commit data and was looking at the wrong thing).

Same as BigDecimal can be said for any Java .toString() functions. I guess that is good in some ways. Serialization for example or debugging. There is an unique string representation.

Also as others mentioned using formatters works OK. Just use formatters, same for the JSF frontend, formatters do the job properly and are aware of the locale.

葬﹪忆之殇 2024-10-25 08:32:21
String money = output.replace(',', '.');
String money = output.replace(',', '.');
柠檬 2024-10-25 08:32:21

这对我有用......

    double num = 10025000;
    new DecimalFormat("#,###.##");
    DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(Locale.GERMAN);
    System.out.println(df.format(num));

This worked for me...

    double num = 10025000;
    new DecimalFormat("#,###.##");
    DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(Locale.GERMAN);
    System.out.println(df.format(num));
江挽川 2024-10-25 08:32:21
DecimalFormat dFormat = new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.US));
DecimalFormat dFormat = new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.US));
不喜欢何必死缠烂打 2024-10-25 08:32:21

您可以在方法中返回字符串之前使用替换函数

return df.format(bd).replace(",", ".")

you could just use replace function before you return the string in the method

return df.format(bd).replace(",", ".")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文