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