[英]Is there any condition where Bigdecimal with MathContext.DECIMAL32 does not provides correct result?
System.out.println("Result="+new BigDecimal(((63.19* 15) + (63.37* 5))).divide(new BigDecimal(15 + 5), MathContext.DECIMAL64).doubleValue());
Result=63.23499999999999 结果= 63.23499999999999
But with MathContext.DECIMAL32
we are getting correct result, see below: 但是,通过
MathContext.DECIMAL32
我们可以得到正确的结果,请参见下文:
System.out.println("Result="+new BigDecimal(((63.19* 15) + (63.37* 5))).divide(new BigDecimal(15 + 5), MathContext.DECIMAL32).doubleValue());
Result=63.235 结果= 63.235
The problem here not BigDecimal
, but the fact that (63.19* 15) + (63.37* 5)
is not 1264.7
but 1264.6999999999998
, because the former cannot be represented as a double
. 这里的问题不是
BigDecimal
,而是(63.19* 15) + (63.37* 5)
不是1264.7
而是1264.6999999999998
,因为前者不能表示为double
。
If you do 如果你这样做
new BigDecimal("1264.7").divide(new BigDecimal("20"), MathContext.DECIMAL64)
instead, you get the desired result. 相反,您将获得所需的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.