In one of my wordpress theme function, this code is being used to calculate total billable amount: ceil(($cost * (1 + ($charges / 100))) * 100);
There is a little miscalculation happening for below scenario.
Scenario:
$charges = 9;
$cost = 100;
echo ceil(($cost * (1 + ($charges / 100))) * 100);
The above code outputs 10901 whereas it should be 10900.
It works fine for other scenarios like:
$charges = 4;
$cost = 90.7;
echo ceil(($cost * (1 + ($charges / 100))) * 100);
//outputs 9433, which is fine because manual calculation results 9432.8
Question:
The problem is that you are applying ceil to the outer expression. Try to rewrite it as:
$charges = 9;
$cost = 100;
echo ($cost + ceil($cost * $charges / 100)) * 100;
This outputs 10900 as expected.
UPDATE
As @cars10m suggested, simplifying the expression does help:
echo ceil($cost * 100 + $cost * $charges);
UPDATE 2
You can also use BCMath library to do precise math:
bcscale(6);
echo ceil(bcadd(bcmul($cost, 100), bcmul($cost, $charges)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With