Edit report at https://bugs.php.net/bug.php?id=60892&edit=1
ID: 60892
Comment by: gregs at net-virtual dot com
Reported by: gregs at net-virtual dot com
Summary: money_format returning inconsistent results
Status: Open
Type: Bug
Package: Math related
Operating System: OSX
PHP Version: 5.3.9
Block user comment: N
Private report: N
New Comment:
Also I should add that if I do this:
$a = 8.930 + 70687.465;
instead of this:
$a = 70687.465 + 8.930;
It works. The round() function seems to behave correctly in either case. I
cannot tell from this behavior if the problem is in number_format (which may
not
be calling round(), but doing its own flawed rounding) or if it something
deeper
in how PHP is storing floats/doubles.
Previous Comments:
------------------------------------------------------------------------
[2012-01-26 14:54:08] gregs at net-virtual dot com
Here is a easier to read version of the test code (I also added one more):
$a = 70687.465 + 8.930;
$b = 70696.395;
$c = 70687.464 + 8.936;
$d = 70687.464 + 8.931;
printf("A: %s %s %.9f\n", $a, money_format("%.2n", $a), $a);
printf("B: %s %s %.9f\n", $b, money_format("%.2n", $b), $b);
printf("C: %s %s %.9f\n", $c, money_format("%.2n", $c), $c);
printf("D: %s %s %.9f\n", $d, money_format("%.2n", $d), $d);
Output:
A: 70696.395 70696.39 70696.395000000
B: 70696.395 70696.40 70696.395000000
C: 70696.4 70696.40 70696.400000000
D: 70696.395 70696.40 70696.395000000
------------------------------------------------------------------------
[2012-01-26 14:47:43] gregs at net-virtual dot com
Description:
------------
php -r '$a = 70687.465 + 8.930;$b = 70696.395;$c = 70687.464 + 8.936;printf("A:
%s
%s %.9f\n", $a, money_format("%.2n", $a), $a);printf("B: %s %s %.9f\n", $b,
money_format("%.2n", $b), $b);printf("C: %s %s %.9f\n", $c,
money_format("%.2n",
$c), $c);'
Output:
A: 70696.395 70696.39 70696.395000000
B: 70696.395 70696.40 70696.395000000
C: 70696.4 70696.40 70696.400000000
Why is A not getting rounded up to 70696.40?
Test script:
---------------
php -r '$a = 70687.465 + 8.930;$b = 70696.395;$c = 70687.464 + 8.936;printf("A:
%s %s %.9f\n", $a, money_format("%.2n", $a), $a);printf("B: %s %s %.9f\n", $b,
money_format("%.2n", $b), $b);printf("C: %s %s %.9f\n", $c,
money_format("%.2n", $c), $c);'
Expected result:
----------------
A: 70696.395 70696.40 70696.395000000
B: 70696.395 70696.40 70696.395000000
C: 70696.4 70696.40 70696.400000000
Actual result:
--------------
A: 70696.395 70696.39 70696.395000000
B: 70696.395 70696.40 70696.395000000
C: 70696.4 70696.40 70696.400000000
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60892&edit=1