Double vs. BigDecimal?

前端 未结 6 2209
梦谈多话
梦谈多话 2020-11-22 02:19

I have to calculate some floating point variables and my colleague suggest me to use BigDecimal instead of double since it will be more precise. Bu

6条回答
  •  伪装坚强ぢ
    2020-11-22 02:30

    There are two main differences from double:

    • Arbitrary precision, similarly to BigInteger they can contain number of arbitrary precision and size
    • Base 10 instead of Base 2, a BigDecimal is n*10^scale where n is an arbitrary large signed integer and scale can be thought of as the number of digits to move the decimal point left or right

    The reason you should use BigDecimal for monetary calculations is not that it can represent any number, but that it can represent all numbers that can be represented in decimal notion and that include virtually all numbers in the monetary world (you never transfer 1/3 $ to someone).

提交回复
热议问题