|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-07-12 14:40 UTC] 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)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 07 22:00:01 2025 UTC |
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).