XtremeBaumer XtremeBaumer - 1 year ago 56
Java Question

BigDecimal adding wrong value

I have a

defined like this:

private static final BigDecimal sd = new BigDecimal(0.7d);

if i print it, i get the value:


which causes some wrong calculations. Does anyone know a way to get the exact value of
? Change it to
would view the right result, but it shouldn't be like that

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download