Edit report at https://bugs.php.net/bug.php?id=61787&edit=1

 ID:                 61787
 Updated by:         ras...@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: No
+Block user comment: Yes
 Private report:     N

 New Comment:

Please stop. This is not a PHP-specific issue. This is just how floating point 
works in computers. You will need to learn how to deal with it. For example, 
compile this C program:

#include <stdio.h>
int main(char *argv[], int argc) {
        printf("%01.2f\n",7.005);
        printf("%01.2f\n",8.005);
}

cc f.c -o f

then run it:

./f

you get:

7.00
8.01

Which is exactly the same as the PHP results. PHP is a thin layer over C.


Previous Comments:
------------------------------------------------------------------------
[2012-04-26 10:07:22] ubuntu at spacetrace dot org

The same bug is in 
sprintf("%01.2f",7.005);
which results in 
7.00 
while 
sprintf("%01.2f",8.005);
results in 
8.01

------------------------------------------------------------------------
[2012-04-26 09:38:19] ubuntu at spacetrace dot org

could you please set the Status back to "Bug"

all people i talked to, said, that this should be solved. cause it causes 
follow-up bugs in your applications that are very hard to find, cause they 
happen so seldom

------------------------------------------------------------------------
[2012-04-22 23:15:04] ubuntu at spacetrace dot org

how does the function round(7.005,2) do the trick to get 7.01? is it so much 
slower than how money_format does it?

------------------------------------------------------------------------
[2012-04-22 23:01:38] ras...@php.net

No, that won't work. Often the value comes from expressions that always 
generate 
lots of digits. eg. number_format('%.2i', 1/3)
What would you expect that to do? You wold get a notice every single time even 
though there may not be any relevant loss of precision.

There are just certain things you need to eventually learn when you start 
programming.

------------------------------------------------------------------------
[2012-04-22 22:45:48] ubuntu at spacetrace dot org

maybe an efficient way would be to see if the desired amount of digits is less 
than the amount of digits in the float.

in my example where i tried to show 1.005 with only 2 fractional digits, the 
notice could be like:
"Error: input float exceeds maximum number of fractional digits"

this would save thousands of hours of debugging worldwide.

i am so keen on this issue, cause it cost me already about 100 hours of work to 
find out what was the cause, why in my project my bills every now and then 
where not correctly rounded.
it is a stupid bug really hard to track and i am sure am not the only one that 
had this problem. 

if php would throw a notice, that would save lots of hazzle for 
not-so-experiensed programmers.

how can you know, that you shouldn't use floats in money_format. this is a kind 
of secret knowledge right now especially cause the manual says: "string 
money_format ( string $format , float $number )"

------------------------------------------------------------------------


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=61787


-- 
Edit this bug report at https://bugs.php.net/bug.php?id=61787&edit=1

Reply via email to