Accuracy of double precision multiplication in java?

耗尽温柔 提交于 2019-12-01 17:28:00

The multiplication is working fine, but 2.2 cannot be represented exactly as a double. The closest doubles are:

  • 2.199999999999999733 (0x4001999999999999)
  • 2.200000000000000177 (0x400199999999999a)

Some software will print the latter value as 2.2, but that doesn't mean it's exact. It just means it's treated as "close enough".

If you are working with financials data dont use float or double just use java.math.BigDecimal

you can use BigDecimal, but make sure that you use the construct of BigDecimal(String) rather than BigDecimal(double). The difference between them is:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!