2

Is there a way to round smaller currency sub-units to regular ones, so as to minimize the rounding error? I've come across things like stochastic rounding but I don't know how I would apply it to my case (and I just know how much slack I'm going to get for that).

My use case is as follows:

User wanted to buy 20 XYZ at 1.1387. The total price should be 22.77.

In my system, a large order can be broken down into smaller orders:

Amount | Rate | Price

9.50 | 1.1387 | 10.82

9.50 | 1.1387 | 10.82

1.00 | 1.1387 | 1.14

The total amounts to 22.78 due to standard rounding (0-4 down, 5-9 up), whereas it should have been 22.77, which means I tell my user I have deducted 0.01 more than they owed me.

I've looked into using a smaller sub-unit, as recommended here, but I still have to round at some point, even if it is the last point in my calculations, which would cause confusion for users when things don't add up (Client requires strict adherence to currency decimals).

EDIT: I ended up keeping track of the nonsignificant figures and accumulating them until they become significant enough to merit rounding up, i.e. greater than or equal to 0.01 in this case.

ygh
  • 123

1 Answers1

3

Rounding errors add up. That is why in a series of calculations you have to pay close attention to when you round and only do it when necessary. The more you round, the bigger your error can be. From what I gather from your problem round for every specific item, instead rounding for the whole bill. Obviously this can give you slightly different results. There is not mathematical way to circumvent that problem, but there might be design solutions:

a) You can just accept, that rounding errors will add up for every item. This is a very small discount or extra charge and no one will care. In some European countries that don't use 1 and 2 cents any more this is regularly done when you pay cash and everyone is fine with it.

b) If you want to round more correctly, so just once at the end, and you don't want to confuse your customers, you should explain how you got to the result. You could offer an extra column in the order that uses more decimal spaces or just put a little asterisk on the order with a short explanation why the resulting bill might be a few cents off.

don-joe
  • 827
  • Makes total sense, but I wonder if there is some kind of way to math-ninja it out. – ygh Oct 08 '19 at 12:44