Re: Loss of precision in intval()
| From: | David Duong | Date: | Wed, 01 Aug 2007 21:28:28 +0000 |
| Subject: | Re: Loss of precision in intval() | ||
| References: | 1 2 | Groups: | php.general php.general php.general |
| Request: | Send a blank email to php-general+get-260028@lists.php.net to get a copy of this message | ||
Roberto Mansfield wrote:
Internally, 75.82 can't be stored exactly, so 75.82 * 100 is probably 7581.999992 rather than the expected integer value of 7582. So intval is behaving properly. Sounds like you want intval(round($a)); tg-php@gryffyndevelopment.com wrote:Very weird and counter intuitive. Looking at the php manual, I see this: Converting to integer from floating point: "When converting from float to integer, the number will be rounded towards zero." But you'd think the multiplication would happen before the rounding. if you do: $a = ceil(75.82 * 100); you should get the proper answer. This is what I used for testing: <?php $x = 75.82; $y = 100; echo "x is " . gettype($x) . "<br>\n"; echo "y is " . gettype($y) . "<br>\n"; $a = ceil($x * $y); echo "a is " . gettype($a) . "<br>\n"; echo "intval(a) is " . gettype(intval($a)) . "<br>\n"; echo $a . " *** " . intval($a); ?> Not sure that really helps, but seems to be some kind of order of precedence issue. -TG = = = Original message = = = This sort of thing really isn't helpful... <?php $a = 75.82 * 100; echo intval($a); ?> ___________________________________________________________ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. Better yet: $a + 0, or (int)$a.