0
$\begingroup$

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

    1. shipment 50,1125 kg
    1. 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

  1. shipment price: 50,1125 / 87,4126307 * 511,46 = 293,21322381846585930515874521095 ~ 293,21 €

  2. 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:

  1. shipment price - rounded to two decimals: 45,43 €
  2. shipment price - rounded to two decimals: 28,54 € Total: 73,97 €, but Carrier charged us for 73,98 € (1 cent difference)
  • 1
    A very simple example: the carrier charges an amount with an odd number of cents and the two shipments have equal weight. Depending on your rounding rule, either you round both shares up (one cent too much) or down (one cent too little).2017-01-26
  • 0
    @DavidK thank you for simple example. I tried with 2 shipments of weight: 1,5 kg and total sum: 2,33 €. 1.5/3*2,33 = 1,165 and like you pointed out you can round them to 1,17 or 1,16 which results in 1,17*2=2,34 != 2,33 (one cent to much).2017-01-30

1 Answers 1

1

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.

  • 0
    What you say is no problem is exactly what OP calls a problem. You have two identical shipments that either pay different freight or the total of all the customer freight charges does not match with what the supplier was charged.2017-01-26
  • 0
    @RossMillikan I think, that i've included this exception in my answer. On the other way - as the cargo is weighted with seven digits precission, situations when both shipments have to pay exactly ##.##5\$ will be extremely rare.2017-01-26