Edit report at http://bugs.php.net/bug.php?id=36008&edit=1
ID: 36008
Comment by: rinseout at gmail dot com
Reported by: adi at rogers dot com
Summary: incorrect round() & number_format() result
Status: No Feedback
Type: Bug
Package: Math related
Operating System: win32 only
PHP Version: 5CVS-2006-06-14
Block user comment: N
New Comment:
Similar problem on Vista home premium, PHP 5.2.5:
<?php
$numbers = array(0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008,
0.009);
foreach ($numbers as $number)
print $number."->".number_format($number, 2, '.', ',')."<br>";
?>
0.001->0.00
0.002->0.00
0.003->0.00
0.004->0.00
0.005->0.01
0.006->0.01
0.007->0.01
0.008->0.01
0.009->0.01
as expected... however...
<?php
$numbers =
array(78.221,78.222,78.223,78.224,78.225,78.226,78.227,78.228,78.229);
foreach ($numbers as $number)
print $number."->".number_format($number, 2, '.', ',')."<br>";
?>
78.221->78.22
78.222->78.22
78.223->78.22
78.224->78.22
78.225->78.22
78.226->78.23
78.227->78.23
78.228->78.23
78.229->78.23
?
78.225->78.22
?
no fiddling with the precision parameter enabled me to fix this.
Previous Comments:
------------------------------------------------------------------------
[2007-03-16 15:36:15] me at nospam dot com
I don't have a simpler test case, but in doing more testing, I found
that the rounding apparently just doesn't work. When you print out a
number that was rounded, it will appear correctly, but when you try to
do math functions with other similar numbers (ie 6.08 - 6.08), I will
get a remainder of like 2.039283e-16. However, setting the type to each
variable as a string made it work correctly.
------------------------------------------------------------------------
[2007-01-15 01:00:00] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
------------------------------------------------------------------------
[2007-01-09 02:56:14] terrychangsharp at gmail dot com
I tested the code on PC + FreeBSD 6.1 + Apache 2.2.3 + PHP 5.2.0 and get
the same results. Actually, the seemingly coincident numbers $num and
$num_same_thing are in fact not the same thing: $num - $num_same_thing
yields -1.7763568394003E-015, not zero, so $num is smaller than 14.375
and round($num) is 14.37, not 14.38. Thus this "bug" is more like a
floating point error thing, not a real bug.
------------------------------------------------------------------------
[2007-01-07 04:07:29] [email protected]
Please try using this CVS snapshot:
http://snaps.php.net/php5.2-latest.tar.gz
For Windows:
http://snaps.php.net/win32/php5.2-win32-latest.zip
------------------------------------------------------------------------
[2006-10-31 00:39:37] adi at rogers dot com
Aww well it's too late for me to bug it for Windows Vista as we've
entered Release Candidate 2 :( I'll test this again on IIS7 with the
final version of Vista...
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/bug.php?id=36008
--
Edit this bug report at http://bugs.php.net/bug.php?id=36008&edit=1