java的double的精度问题

来源:互联网 发布:新的网络社交软件 编辑:程序博客网 时间:2024/05/28 05:13

单纯使用double进行加、减操作,会发生精度偏差。

这时我们可以使用BigDecimal。

将需要加、减的数,用BigDecimal包装起来。

加法:

totalCharge.add(new BigDecimal(charge))

减法:

totalCharge.subtract(new BigDecimal(charge))


原创粉丝点击