note 63627 added to function.intval
| From: | david at davidmosesNO dot SPAMca | Date: | Sun, 26 Mar 2006 04:16:14 +0000 |
| Subject: | note 63627 added to function.intval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-106619@lists.php.net to get a copy of this message | ||
Re: Ben Laurienti, Disturbing issue with intval()
This may make things a little more clear for you:
<?php
$amount = 19.99 * 100;
printf("%.13f", $amount); // Outputs : 1998.99999999999981998
?>
Remember that casting floats to ints rounds towards zero, so these are all functionally the same
producing 1998:
echo (int)$amount;
echo intVal($amount);
echo (int) 1998.99999999999981998;
Computers cannot store many floating point values to exact precision. For this reason it is
generally recommended to never work with currencies in floating point types, and use integers (work
in cents), or use some other method. Using floats will have all kinds of rounding and comparison
issues. 19.99 will not be the only number to surprise you!
----
Server IP: 66.163.161.117
Probable Submitter: 69.195.8.22
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.intval.php
Edit -- http://master.php.net/note/edit/63627
Del: integrated -- http://master.php.net/note/delete/63627/integrated
Del: useless -- http://master.php.net/note/delete/63627/useless
Del: bad code -- http://master.php.net/note/delete/63627/bad+code
Del: spam -- http://master.php.net/note/delete/63627/spam
Del: non-english -- http://master.php.net/note/delete/63627/non-english
Del: in docs -- http://master.php.net/note/delete/63627/in+docs
Del: other reasons-- http://master.php.net/note/delete/63627
Reject -- http://master.php.net/note/reject/63627
Search -- http://master.php.net/manage/user-notes.php