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:
- Why is that happening?
- How can I prevent that?
- Any alternate function to round to next nearest integer? (only if amount is floating value)