One truck can transport one or more shipments. Each shipment has weight and belongs to one customer. How to calculate price for each customer?
Truck carries 2 shipments
- shipment 50,1125 kg
- shipment 37,3001307 kg
Carrier charged us 511,46 €. Now we need to calculate price per each shipment, so we can issue invoice for each customer.
Approach:
Total weight: 50,1125 + 37,3001307 = 87,4126307
shipment price: 50,1125 / 87,4126307 * 511,46 = 293,21322381846585930515874521095 ~ 293,21 €
shipment price: 37,3001307 / 87,4126307 * 511,46 = 218,24677618153414069484125478905 ~ 218,25 €
Total price: 293,21 + 218,25 = 511,46
Could there be any rounding problems (of course if some other shipment weights and carrier price is used)? Is there some other better algorithm/procedure to do this? I'm afraid that if right parameters occurs something like this could occur:
- shipment price - rounded to two decimals: 45,43 €
- shipment price - rounded to two decimals: 28,54 € Total: 73,97 €, but Carrier charged us for 73,98 € (1 cent difference)
If there are $2$ shipments, there should be no problem - if one is rounded down, the second one is rounded up. Except if we pay eg. $1.99\$$ for two identical shipments - then both are rounded up to $1.00\$$ (or down to $.99\$$, depending on the rounding rule) and finally we have $.01\$$ extra (or $.01\$$ missing).
On the other hand if we have more than 2 shipments, situations like that are common, eg. paying $1\$$ for three identical shipments makes $.33\$$ for each shipment and there is still $.01\$$ missing.