ID: 47418 Comment by: tdietsche at comcast dot net Reported By: cu19 at gmx dot de Status: No Feedback Bug Type: *Math Functions Operating System: WinXP SP3 PHP Version: 5.3CVS-2009-02-17 (CVS) New Comment:
this sure IS a bug, scott_mac you need to wake up, if this is the best that php handles floating point then it is worthless. In reading a value of "19" from an excel spreadsheet via odbc, it formats it as "18.:0" which is pure garbage and a bug to me. How can you defend that? I can send my code if you want. Previous Comments: ------------------------------------------------------------------------ [2009-02-25 01:00:01] php-bugs at lists dot php dot net No feedback was provided for this bug for over a week, so it is being suspended automatically. If you are able to provide the information that was originally requested, please do so and change the status of the bug back to "Open". ------------------------------------------------------------------------ [2009-02-17 11:20:31] scott...@php.net sc...@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] scott...@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. ------------------------------------------------------------------------ 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