Edit report at https://bugs.php.net/bug.php?id=61787&edit=1
ID: 61787
Comment by: ubuntu at spacetrace dot org
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:
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
Previous Comments:
------------------------------------------------------------------------
[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] [email protected]
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 )"
------------------------------------------------------------------------
[2012-04-22 20:40:22] [email protected]
There is no efficient way to "notice the error"
If you want slow and accurate floating point manipulation it is available via
the
arbitrary precision extensions like bcmath and gmp.
------------------------------------------------------------------------
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