I've run into trouble with rounding the results of calculations. I presume its to do with floating-point precision, but can't seem to get around it.
take the following:
$number = 227*0.175;
echo "number = $number<BR>";
echo "cast to float : ".(float) $number."<BR>";
echo "var_dump : ".var_dump($number)."<BR>";
echo "using round : ". round($number,2)."<BR>";
echo "using number format : ". number_format($number,2,".","")."<BR>";
echo "multiply by 100 and round to int: ".round($number*100)."<BR>";
echo "multiply by 100 and cast to int : ".(int)($number*100)."<BR>";
this gives me :
number = 39.725
cast to float : 39.725
float(39.725) var_dump :
using round : 39.72
using number format : 39.72
multiply by 100 and round to int: 3972
multiply by 100 and cast to int : 3972
As you can see the number (39.725) is rounded down to 39.72 in each case, instead of up. Is this due to floating points?
If I say $number = 39.725 then it rounds upwards to 39.73 (except for casting to int).
How do I get around this?
I'm trying to make sure all the figures work out as they do in our acconting software. They probably store pence rather than pounds but I'm not sure this would make any difference in this case. If I multiply by a float (0.175) then I'll end up with a float again, won't I.
HELP ME!!!