BigDecimal adding wrong value

后端 未结 5 1969
谎友^
谎友^ 2020-11-27 08:15

I have a BigDecimal defined like this:

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

if i print it, i get the val

5条回答
  •  迷失自我
    2020-11-27 09:06

    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.

提交回复
热议问题