#47418 [Bgs->Fbk]: number_format misbehaving with some values
| From: | scottmac@php.net | Date: | Tue, 17 Feb 2009 11:59:57 +0000 |
| Subject: | #47418 [Bgs->Fbk]: number_format misbehaving with some values | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-133903@lists.php.net to get a copy of this message | ||
ID: 47418
Updated by: scottmac@php.net
Reported By: cu19 at gmx dot de
-Status: Bogus
+Status: Feedback
Bug Type: *Math Functions
Operating System: WinXP SP3
PHP Version: 5.3CVS-2009-02-17 (CVS)
Previous Comments:
------------------------------------------------------------------------
[2009-02-17 11:20:31] scottmac@php.net
scott@skinny [~] $ php -r 'var_dump(number_format(3.9, 2));'
string(4) "3.90"
Can't reproduce this on Linux.
------------------------------------------------------------------------
[2009-02-17 10:35:08] cu19 at gmx dot de
I don't think it's a problem with limited precision but rather about
the function itself. I think a float should be able to represent 3
digits without a problem. The hex representation of the float that is to
be formatted is 0x40799998, which equals to about 3.8999996.
I think number_format should be able to round this to '3.90' instead of
displaying it as '3.8:'
------------------------------------------------------------------------
[2009-02-17 10:18:32] scottmac@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://docs.sun.com/source/806-3568/ncg_goldberg.html
Thank you for your interest in PHP.
It's the way floating point numbers work in computers, the canned
response gives you a link to read about this some more.
------------------------------------------------------------------------
[2009-02-17 10:13:49] cu19 at gmx dot de
After some additional recherche, it seems to me that the value is first
internally represented as '3.89'. Then, the function checks the
truncated digit which is larger than 5. So the last digit in the result
is increased by one. In ASCII, after the '9' comes the ':', so when the
last char of '3.89' is increased by one, it becomes '3.8:'. So when
increasing the last digit there must be checked if it's smaller than 9.
What I don't understand is why it only occurs sometimes...
Don't know the PHP sources and where to look for this, so I kindly ask
someone to look after this function.
------------------------------------------------------------------------
[2009-02-17 09:48:15] cu19 at gmx dot de
In fact the result is 3.8:
9.8: was a typo
------------------------------------------------------------------------
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
http://bugs.php.net/47418
--
Edit this bug report at http://bugs.php.net/?id=47418&edit=1