Edit report at http://bugs.php.net/bug.php?id=52315&edit=1
ID: 52315 Comment by: wisent at yandex dot ru Reported by: wisent at yandex dot ru Summary: Error in type convert after specific round() operation Status: Bogus Type: Bug Package: Math related Operating System: Ubuntu 8.04 PHP Version: 5.2.13 New Comment: aharvey, the problem is not related with strings. I better should write the code like this: // this works bad: $sum = 4926.11; $sum = round($sum, 2) * 100; var_dump($sum); // float(492611) $sum = (integer) $sum; var_dump($sum); // int(492610) // the first numbers, the other round() operation, works good $sum = 4926.11; $sum = round($sum * 100); var_dump($sum); // float(492611) $sum = (integer) $sum; var_dump($sum); // int(492611) You probably haven't noticed the main point. After operations on variable with round() function variables have the same representation, float(492611). But the subsequent type conversion to integer returns different values, int(492610) and int(492611). Previous Comments: ------------------------------------------------------------------------ [2010-07-13 18:27:12] ahar...@php.net Floating point values have a limited precision. Hence a value might not have the same string representation after any processing. That also includes writing a floating point value in your script and directly printing it without any mathematical operations. If you would like to know more about "floats" and what IEEE 754 is, read this: http://docs.sun.com/source/806-3568/ncg_goldberg.html Thank you for your interest in PHP. ------------------------------------------------------------------------ [2010-07-12 14:40:58] wisent at yandex dot ru Description: ------------ First I perform specific round() operation. Then I convert the number from float to integer. The result is wrong, the number differs by 1 from what it should be. The problem appears not for every number. Test script: --------------- // this works bad: $sum = '4926.11'; $sum = round($sum, 2) * 100; var_dump($sum); // float(492611) $sum = (integer) $sum; var_dump($sum); // int(492610) // the same round() operation, the other number, works good $sum = '426.11'; $sum = round($sum, 2) * 100; var_dump($sum); // float(42611) $sum = (integer) $sum; var_dump($sum); // int(42611) // the first numbers, the other round() operation, works good $sum = '4926.11'; $sum = round($sum * 100); var_dump($sum); // float(492611) $sum = (integer) $sum; var_dump($sum); // int(492611) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=52315&edit=1