Re: Loss of precision in intval()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxxxxxxxxx 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.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux