ID:               47418
 Updated by:       scott...@php.net
 Reported By:      cu19 at gmx dot de
 Status:           Bogus
 Bug Type:         *Math Functions
 Operating System: WinXP SP3
 PHP Version:      5.3CVS-2009-02-17 (CVS)
 New Comment:

sc...@skinny [~] $ php -r 'var_dump(number_format(3.9, 2));'
string(4) "3.90"

Can't reproduce this on Linux.


Previous Comments:
------------------------------------------------------------------------

[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.

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

[2009-02-17 09:48:15] cu19 at gmx dot de

In fact the result is 3.8:
9.8: was a typo

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

[2009-02-17 09:45:57] cu19 at gmx dot de

Description:
------------
In my script im using number_format to format values read from a
MSSQL-DB via ODBC. Works fine, as long as the value is not equal to 1.9,
2.9, 3.9, 1.7 and probably some others.

I've made the following test script:
<?php
echo number_format(3.9, 2);
?>

Output is expected to be 3.90, but in fact is 9.8:
I don't know where this colon does come from, but for me it seems to be
an error in number_format. Using sprintf('%4.2f', 3.9) leads to the same
problem.

I'm using PHP 5.2.8 on Apache 2.2.9 on Windows XP MCE SP3. When Apache
just got restarted, the test script works fine, but when executing my
(quite large) script with several ODBC queries, it produces this error
and after execution also the test script fails.

I've tried using 5.3 snapshot and 5.2.8 snap, the error occurs also.
With PHP 5.2.6, the error didn't occur as far as I can remember. The
error doesn't occur, too, with CLI.

Any idea? Or is it about the Apache webserver?

Reproduce code:
---------------
echo number_format(3.9, 2);

Expected result:
----------------
3.90

Actual result:
--------------
3.8:


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


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

Reply via email to