#36008 [Opn->Fbk]: incorrect round() & number_format() result
| From: | iliaa@php.net | Date: | Sun, 07 Jan 2007 04:07:29 +0000 |
| Subject: | #36008 [Opn->Fbk]: incorrect round() & number_format() result | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-107257@lists.php.net to get a copy of this message | ||
ID: 36008
Updated by: iliaa@php.net
Reported By: adi at rogers dot com
-Status: Open
+Status: Feedback
Bug Type: Math related
Operating System: win32 only
PHP Version: 5CVS-2006-06-14
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[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...
------------------------------------------------------------------------
[2006-10-30 13:30:58] dave at koales dot co dot uk
I think this is a Windows vs others thing. I have the same rounding
error using PHP 5.0.5 on Windows XP SP2.
The same problem does not exist on my webhost, who use PHP 4.3.9 and
Redhat Linux (need any more detail).
The code I used was:
echo "Correct VAT = " . round( 0.525, 2 ) . "\r\n";
echo "Wrong VAT = " . round( 3 * ( 17.5 / 100.0 ), 2 ) . "\r\n";
Outputs 0.53 for each on webhost (under Linux) and 0.52 for the second
test under Windows (0.53 for the first test).
I've also had very bizarre rounding errors with MySQL. The rounding
error came and went depending on how the calculation was constructed!
------------------------------------------------------------------------
[2006-10-10 21:44:21] adi at rogers dot com
Amen to that ;)
------------------------------------------------------------------------
[2006-10-10 07:45:36] troy20 at mail dot ru
Code:
for ($i = 1.075;$i <= 1.975;$i=$i+0.1)
{echo round($i,2);}
Result:
1.08, 1.18, 1.28, 1.38....
But if
echo round(1.275,2);
echo round(1.275*100);
we have 1.27 and 127.
Another "same" example:
for ($i = 1.075;$i <= 20.975;$i=$i+0.1)
{echo round($i,2);}
Win2003 + Apache 2.0.59 + php 5.1.6
------------------------------------------------------------------------
[2006-06-14 22:11:53] adi at rogers dot com
Regretfully, the same output:
5.2.0-dev
Wrong: 14.37
Wrong: 14.37
'Magically' correct: 14.38
'Magically' correct: 14.38
Converted to string and is now correct: 14.38
Converted to string and is now correct: 14.38
The 32 precision results are also the same [as the ones on January 15].
------------------------------------------------------------------------
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/36008
--
Edit this bug report at http://bugs.php.net/?id=36008&edit=1