Re: Loss of precision in intval()
| From: | tg-php at gryffyndevelopment dot com | Date: | Wed, 01 Aug 2007 17:36:47 +0000 |
| Subject: | Re: Loss of precision in intval() | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-260012@lists.php.net to get a copy of this message | ||
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.