Skip to content
Advertisement

Revert a number to its original value in PHP

I have a problem in reverting the original amount of a customer.

A customer can amortize its balance in 3 months.

So, if the balance is 1,100.00

1,100.00 / 3 = 366.666666667 rounded up to 366.67

But then, he decided to cancel the amortization.

What I do is amortized amount * 3, which is:
366.67 * 3 = 1,100.01

Expected output should be 1,100.00

Thanks!

Advertisement

Answer

Your problem here is the original calculation, if you have 1,100.00 to pay and simply divide by 3 with rounding, your customer will actually pay 1,100.01!

The usual solution is to compensate this difference in the last (or first) amount to pay:

  • n-1 first payments: total / n -> for your example: 2 times 366.67
  • last payment: total - sum of precedent payments -> for your example: 1,100.00 - (2 * 366.67) so 366.66 to pay

Like this the total paid will always be right, whatever you apply flooring or rounding, and you can reverse any time by adding each value.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement