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: Bogus
Type: Bug
Package: Math related
Operating System: OSX
PHP Version: 5.3.9
Block user comment: N
Private report: N
New Comment:
Sorry, I meant money_format('%.2n', $float);
In all of these cases the number should be rounded to 70696.40.
Previous Comments:
------------------------------------------------------------------------
[2012-01-27 03:43:37] gregs at net-virtual dot com
With all due respect, you are not reading this.
sprintf('%.2f', $float);
number_format('%.2n', $float);
should *ROUND* these numbers, the behavior is incorrect
------------------------------------------------------------------------
[2012-01-26 22:13:57] [email protected]
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.
As you said - it's related to the way PHP (and almost all computers) store
floating point numbers.
------------------------------------------------------------------------
[2012-01-26 22:09:40] gregs at net-virtual dot com
This problem (if it is one) seems to extend to *printf* functions too:
$a = 8.930 + 70687.465;
$b = 70687.465 + 8.930;
$c = 70696.395000;
printf("A: %f, %.2f\n", $a, $a);
printf("B: %f %.2f\n", $b, $b);
printf("C: %f %.2f\n", $c , $c);'
Output:
A: 70696.395000, 70696.39
B: 70696.395000 70696.39
C: 70696.395000 70696.40
C version:
#include <stdio.h>
int main(void) {
float a, b, c, d;
a = 8.930 + 70687.465;
b = 70687.465 + 8.930;
c = 70696.395000;
printf("A: %f %.2f\n", a, a);
printf("B: %f %.2f\n", b, b);
printf("C: %f %.2f\n", c, c);
}
Output:
A: 70696.398438 70696.40
B: 70696.398438 70696.40
C: 70696.398438 70696.40
------------------------------------------------------------------------
[2012-01-26 19:24:24] gregs at net-virtual dot com
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.
------------------------------------------------------------------------
[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
------------------------------------------------------------------------
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
https://bugs.php.net/bug.php?id=60892
--
Edit this bug report at https://bugs.php.net/bug.php?id=60892&edit=1