Posted by Philip Ronan on 12/05/05 12:59
"mat" wrote:
> Hi all,
>
> This is weird and it is driving me nuts, please can someone explain how
> I can get it to return the correct answer:
>
> print '$_SESSION["a"]: '.$_SESSION["a"].'<br />';
> print '$_SESSION["c"]: '.$_SESSION["c"].'<br />';
> print $_SESSION["c"]." - ".$_SESSION["a"].' =
> '.($_SESSION["c"]-$_SESSION["a"]);
>
> ----------------- RETURNS -----------------------
>
> $_SESSION["allowance"]: 200
> $_SESSION["cart_value"]: 200.15
> 200.15 - 200 = 0.14999999999998
>
> When I test on my windows box it is correct, but not when I test on my
> Unix server.
Your Windows box is just rounding the number up for you.
0.15 is an infinitely long number in binary [1], just like 3/10 is equal to
0.3333333333... in decimal notation
Try using sprintf [2] to tidy up your results.
[1] 0.00100110011001100110011001100...
[2] http://php.net/sprintf
--
phil [dot] ronan @ virgin [dot] net
http://vzone.virgin.net/phil.ronan/
[Back to original message]
|