Edit report at https://bugs.php.net/bug.php?id=60892&edit=1
ID: 60892 User updated 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: If anyone else runs across this, there is a good write-up here of the problem (with some proposals to fix it): https://wiki.php.net/rfc/rounding My take-away is that when using %f in any of the *printf* functions (which I presume money_format uses - what others I can only imagine), the only way to get true correctly rounded numbers is to explicitly round() the float before using it. Previous Comments: ------------------------------------------------------------------------ [2012-01-27 03:45:28] gregs at net-virtual dot com Sorry, I meant money_format('%.2n', $float); In all of these cases the number should be rounded to 70696.40. ------------------------------------------------------------------------ [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] johan...@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. 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. ------------------------------------------------------------------------ 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