note 63627 deleted from function.intval by danbrown
| From: | danbrown@php.net | Date: | Thu, 16 Sep 2010 15:09:43 +0000 |
| Subject: | note 63627 deleted from function.intval by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172640@lists.php.net to get a copy of this message | ||
Note Submitter: david at davidmosesNO dot SPAMca
----
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!