Java 如何打印格式化的 BigDecimal 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3395825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print formatted BigDecimal values?
提问by Roman
I have a BigDecimal
field amount
which represents money, and I need to print its value in the browser in a format like $123.00
, $15.50
, $0.33
.
我有一个代表钱的BigDecimal
字段amount
,我需要在浏览器中以$123.00
, $15.50
,等格式打印其值$0.33
。
How can I do that?
我怎样才能做到这一点?
(The only simple solution which I see myself is getting floatValue
from BigDecimal
and then using NumberFormat
to make two-digit precision for the fraction part).
(我自己看到的唯一简单的解决方案是floatValue
从中获取BigDecimal
然后NumberFormat
用于使分数部分达到两位数精度)。
采纳答案by Luca Molteni
public static String currencyFormat(BigDecimal n) {
return NumberFormat.getCurrencyInstance().format(n);
}
It will use your JVM's current default Locale
to choose your currency symbol. Or you can specify a Locale
.
它将使用您的 JVM 当前默认值Locale
来选择您的货币符号。或者您可以指定一个Locale
.
NumberFormat.getInstance(Locale.US)
For more info, see NumberFormat
class.
有关更多信息,请参阅NumberFormat
类。
回答by demongolem
Another way which could make sense for the given situation is
对给定情况有意义的另一种方法是
BigDecimal newBD = oldBD.setScale(2);
I just say this because in some caseswhen it comes to money going beyond 2 decimal places does not make sense. Taking this a step further, this could lead to
我之所以这么说,是因为在某些情况下,超过 2 位小数的钱是没有意义的。更进一步,这可能会导致
String displayString = oldBD.setScale(2).toPlainString();
but I merely wanted to highlight the setScale method (which can also take a second rounding mode argument to control how that last decimal place is handled. In some situations, Java forces you to specify this rounding method).
但我只是想强调 setScale 方法(它也可以采用第二个舍入模式参数来控制最后一个小数位的处理方式。在某些情况下,Java 强制您指定此舍入方法)。
回答by Jeff_Alieffson
To set thousand separator, say 123,456.78
you have to use DecimalFormat:
要设置千位分隔符,假设123,456.78
您必须使用DecimalFormat:
DecimalFormat df = new DecimalFormat("#,###.00");
System.out.println(df.format(new BigDecimal(123456.75)));
System.out.println(df.format(new BigDecimal(123456.00)));
System.out.println(df.format(new BigDecimal(123456123456.78)));
Here is the result:
结果如下:
123,456.75
123,456.00
123,456,123,456.78
Although I set #,###.00
mask, it successfully formats the longer values too.
Note that the comma(,) separator in result depends on your locale. It may be just space( ) for Russian locale.
虽然我设置了#,###.00
掩码,但它也成功地格式化了较长的值。请注意,结果中的逗号 (,) 分隔符取决于您的语言环境。对于俄罗斯语言环境,它可能只是 space()。
回答by Dima
BigDecimal(19.0001).setScale(2, BigDecimal.RoundingMode.DOWN)
回答by Fluch
BigDecimal pi = new BigDecimal(3.14);
BigDecimal pi4 = new BigDecimal(12.56);
System.out.printf("%.2f",pi);
// prints 3.14
// 打印 3.14
System.out.printf("%.0f",pi4);
// prints 13
// 打印 13
回答by volkovs
Similar to answer by @Jeff_Alieffson, but not relying on default Locale
:
类似于@Jeff_Alieffson 的回答,但不依赖于 default Locale
:
Use DecimalFormatSymbols
for explicit locale:
使用DecimalFormatSymbols
显式的语言环境:
DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(new Locale("ru", "RU"));
Or explicit separator symbols:
或显式分隔符:
DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
decimalFormatSymbols.setDecimalSeparator('.');
decimalFormatSymbols.setGroupingSeparator(' ');
Then:
然后:
new DecimalFormat("#,##0.00", decimalFormatSymbols).format(new BigDecimal("12345"));
Result:
结果:
12 345.00