You are here: Re: Missing 0.0000001 - driving me crazy - help! « PHP Programming Language « IT news, forums, messages
Re: Missing 0.0000001 - driving me crazy - help!

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/

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация