更新时间:2023-02-03 09:29:55
要准确 10.0001
,您需要使用String构造函数或 valueOf
(根据规范表示构造BigDecimal)双倍):
To get exactly 10.0001
you need to use the String constructor or valueOf
(which constructs a BigDecimal based on the canonical representation of the double):
BigDecimal bd = new BigDecimal("10.0001");
System.out.println(bd.toString()); // prints 10.0001
//or alternatively
BigDecimal bd = BigDecimal.valueOf(10.0001);
System.out.println(bd.toString()); // prints 10.0001
新BigDecimal(10.0001)$的问题c $ c>是参数是
double
,并且碰巧双打不能完全代表 10.0001
。所以 10.0001
被转换为最接近的可能的双倍,即 10.000099999999999766941982670687139034271240234375
这就是你的 BigDecimal
显示。
The problem with new BigDecimal(10.0001)
is that the argument is a double
and it happens that doubles can't represent 10.0001
exactly. So 10.0001
is "transformed" to the closest possible double, which is 10.000099999999999766941982670687139034271240234375
and that's what your BigDecimal
shows.
因此,使用双构造函数很少有意义。
For that reason, it rarely makes sense to use the double constructor.
您可以在此处阅读更多相关信息,将小数位移到一双
You can read more about it here, Moving decimal places over in a double