Edit report at https://bugs.php.net/bug.php?id=61787&edit=1
ID: 61787
Updated by: rasmus@php.net
Reported by: ubuntu at spacetrace dot org
Summary: money_format round error
Status: Not a bug
Type: Bug
Package: Math related
Operating System: Linux ubuntu 11.04
PHP Version: 5.3.10
Block user comment: N
Private report: N
New Comment:
Did you visit the site we provided that explains this? What you think is 7.005 is
actually represented as 7.004999999999999999 whereas 8.005 is
8.005000000000000000001
That's what limited precision means. Go read http://www.floating-point-gui.de/
for a complete explanation.
Previous Comments:
------------------------------------------------------------------------
[2012-04-21 10:11:02] ubuntu at spacetrace dot org
so why does php round 7.005 down to 7.00 while it rounds 8.005 correctly up to 8.01 ?
and why from 16 on again wrong and from 32 on again correct, from 64 on again wrong but now
not a change from 128 on?
------------------------------------------------------------------------
[2012-04-20 16:04:57] johannes@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://www.floating-point-gui.de/
Thank you for your interest in PHP.
.
------------------------------------------------------------------------
[2012-04-20 13:38:17] ubuntu at spacetrace dot org
Description:
------------
if you round with money_format, some small numbers with 0.005 are rounded down instead of up.
Numbers bigger than 63.5 are all rounded falsly down like with floor()
Test script:
---------------
for($i=0;$i<70;$i++) {
echo($i.'.005 =>'.money_format('%.2i',$i+0.005).'<br>');
if($i<10)
echo($i.'.00500000000000001=>'.money_format('%.2i',$i+0.00500000000000001).'<br>');
}
Expected result:
----------------
0.005 =>0.01
0.00500000000000001=>0.01
1.005 =>1.01
1.00500000000000001=>1.01
2.005 =>2.01
2.00500000000000001=>2.01
3.005 =>3.01
3.00500000000000001=>3.01
4.005 =>4.01
4.00500000000000001=>4.01
5.005 =>5.01
5.00500000000000001=>5.01
6.005 =>6.01
6.00500000000000001=>6.01
7.005 =>7.01
7.00500000000000001=>7.01
8.005 =>8.01
8.00500000000000001=>8.01
9.005 =>9.01
9.00500000000000001=>9.01
10.005 =>10.01
11.005 =>11.01
12.005 =>12.01
13.005 =>13.01
14.005 =>14.01
15.005 =>15.01
16.005 =>16.01
...
Actual result:
--------------
0.00500000000000001=>0.01
1.005 =>1.00
1.00500000000000001=>1.01
2.005 =>2.00
2.00500000000000001=>2.00
3.005 =>3.00
3.00500000000000001=>3.00
4.005 =>4.00
4.00500000000000001=>4.00
5.005 =>5.00
5.00500000000000001=>5.00
6.005 =>6.00
6.00500000000000001=>6.00
7.005 =>7.00
7.00500000000000001=>7.00
8.005 =>8.01
8.00500000000000001=>8.01
9.005 =>9.01
9.00500000000000001=>9.01
10.005 =>10.01
11.005 =>11.01
12.005 =>12.01
13.005 =>13.01
14.005 =>14.01
15.005 =>15.01
16.005 =>16.00
17.005 =>17.00
18.005 =>18.00
19.005 =>19.00
20.005 =>20.00
21.005 =>21.00
22.005 =>22.00
23.005 =>23.00
24.005 =>24.00
25.005 =>25.00
26.005 =>26.00
27.005 =>27.00
28.005 =>28.00
29.005 =>29.00
30.005 =>30.00
31.005 =>31.00
32.005 =>32.01
33.005 =>33.01
34.005 =>34.01
35.005 =>35.01
36.005 =>36.01
37.005 =>37.01
38.005 =>38.01
39.005 =>39.01
40.005 =>40.01
41.005 =>41.01
42.005 =>42.01
43.005 =>43.01
44.005 =>44.01
45.005 =>45.01
46.005 =>46.01
47.005 =>47.01
48.005 =>48.01
49.005 =>49.01
50.005 =>50.01
51.005 =>51.01
52.005 =>52.01
53.005 =>53.01
54.005 =>54.01
55.005 =>55.01
56.005 =>56.01
57.005 =>57.01
58.005 =>58.01
59.005 =>59.01
60.005 =>60.01
61.005 =>61.01
62.005 =>62.01
63.005 =>63.01
64.005 =>64.00
65.005 =>65.00
66.005 =>66.00
67.005 =>67.00
68.005 =>68.00
69.005 =>69.00
from 64 on all numbers are rounded down like floor()
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=61787&edit=1