Converting floats to ints with intval

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

 



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

--
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