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

前端 未结 8 2027
野趣味
野趣味 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:55

    When you round double price = 0.615 to two decimal places, you get 0.61 (rounded down) but probably expected 0.62 (rounded up, because of the 5).

    This is because double 0.615 is actually 0.6149999999999999911182158029987476766109466552734375.

    0 讨论(0)
  • 2020-12-13 06:58

    You don't need an example. You just need fourth-form mathematics. Fractions in floating-point are represented in binary radix, and binary radix is incommensurable with decimal radix. Tenth grade stuff.

    Therefore there will always be rounding and approximation, and neither is acceptable in accounting in any way, shape, or form. The books have to balance to the last cent, and so FYI does a bank branch at the end of each day, and the entire bank at regular intervals.

    an expression suffering from round-off errors doesn't count'

    Ridiculous. This is the problem. Excluding rounding errors excludes the entire problem.

    0 讨论(0)
提交回复
热议问题