Converting floats to ints with intval
| From: | Paul Waring | Date: | Thu, 06 May 2010 10:24:32 +0000 |
| Subject: | Converting floats to ints with intval | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-304943@lists.php.net to get a copy of this message | ||
I've got the following script which demonstrates a problem I'm having with floating point numbers and intval:
$times100 = (-37.12 * 100);
print $times100 . "\n";
$intval100 = intval($times100);
print $intval100 . "\n";
print ($intval100 / 100) . "\n";
I expect the output to be:
-3712
-3712
-37.12
However, the actual output I'm getting (on several systems, so it's not just my machine) is:
-3712
-3711
-37.11
Is there a reason for this, and a better way I should be doing it? Basically I need to add up a list of floats, representing currency values, and see if they're equal to another float, but I need to convert them to ints first because I can't guarantee than there won't be something after the second decimal place.
Thanks,
Paul
--
Paul Waring
http://www.pwaring.com