A realistic example where using BigDecimal for currency is strictly better than using double

前端 未结 8 2090
野趣味
野趣味 2020-12-13 06:29

We know that using double for currency is error-prone and not recommended. However, I\'m yet to see a realistic example, where BigDecimal

8条回答
  •  我在风中等你
    2020-12-13 06:34

    Using BigDecimal would be most necessary when dealing with high value digital forms of currency such as cyprtocurrency (BTC, LTC, etc.), stocks, etc. In situations like these a lot of times you will be dealing with very specific values at 7 or 8 significant figures. If your code accidentally causes rounding error at 3 or 4 sig figs then the losses could be extremely significant. Losing money because of a rounding error is not going to be fun, especially if it's for clients.

    Sure, you could probably get away with using a Double for everything if you made sure to do everything right, but it would probably be better to not take the risk, especially if you're starting from scratch.

提交回复
热议问题