如何将 DecimalFormat 的小数分隔符从逗号更改为点/点?
我有一个疯狂的小方法,可以将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以通过设置区域设置或使用 来更改分隔符小数格式符号。
如果您希望分组分隔符是一个点,则可以使用欧洲语言环境:
或者,您可以使用 DecimalFormatSymbols 类来更改 format 方法生成的格式化数字中出现的符号。这些符号包括小数点分隔符、分组分隔符、减号和百分号等:
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:
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:
currentLocale can be obtained from Locale.getDefault() i.e.:
欧洲相当大。我不确定他们是否都使用相同的格式。但是这个 或 这个答案会有帮助。
即使用正确的区域设置。
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.
I.e. use the correct locale.
这对我来说有效:
This worked in my case:
BigDecimal 似乎不尊重区域设置。
斯洛文尼亚语言环境应有小数点逗号。我想我对数字有一些奇怪的误解。
我编辑了消息中没有任何意义的部分,因为事实证明这是由于人为错误(忘记提交数据并且正在查看错误的内容)。
对于任何 Java .toString() 函数都可以说与 BigDecimal 相同。我想这在某些方面是好的。例如序列化或调试。有一个独特的字符串表示形式。
正如其他人提到的,使用格式化程序也可以正常工作。只需使用格式化程序,对于 JSF 前端也是如此,格式化程序可以正确完成工作并了解区域设置。
BigDecimal does not seem to respect Locale settings.
Slovenian locale should have a decimal comma. Guess I had strange misconceptions regarding numbers.
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.
这对我有用......
This worked for me...
您可以在方法中返回字符串之前使用替换函数
you could just use replace function before you return the string in the method