Re: Loss of precision in intval()

From: Date: Wed, 01 Aug 2007 18:02:58 +0000
Subject: Re: Loss of precision in intval()
References: 1  Groups: php.general php.general php.general 
Request: Send a blank email to php-general+get-260016@lists.php.net to get a copy of this message
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.

« previous php.general (#260016) next »