I have a BigDecimal
defined like this:
private static final BigDecimal sd = new BigDecimal(0.7d);
if i print it, i get the value:
0.6999999999999999555910790149937383830547332763671875
which causes some wrong calculations. Does anyone know a way to get the exact value of 0.7
as BigDecimal
? Change it to 0.71
would view the right result, but it shouldn't be like that
Use a String literal:
private static final BigDecimal sd = new BigDecimal("0.7");
If you use a double
, actually public BigDecimal(double val)
is called. The reason you do not get 0.7 is that it cannot be exactly represented by a double
. See the linked JavaDoc for more information.