“new BigDecimal(13.3D)” results in imprecise “13.3000000000000007105..”?

前端 未结 5 1310
栀梦
栀梦 2020-12-05 15:06

How is it that Java\'s BigDecimal can be this painful?

Double d = 13.3D;

BigDecimal bd1 = new BigDecimal(d);
BigDecimal bd2 = new BigDecimal(St         


        
5条回答
  •  眼角桃花
    2020-12-05 15:41

    This isn't the fault of BigDecimal - it's the fault of double. BigDecimal is accurately representing the exact value of d. String.valueOf is only showing the result to a few decimal places.

提交回复
热议问题