#47418 [Fbk->Opn]: number_format misbehaving with some values

From: Date: Fri, 12 Feb 2010 11:55:25 +0000
Subject: #47418 [Fbk->Opn]: number_format misbehaving with some values
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-147460@lists.php.net to get a copy of this message
 ID:               47418
 User updated by:  cu19 at gmx dot de
 Reported By:      cu19 at gmx dot de
-Status:           Feedback
+Status:           Open
 Bug Type:         *Math Functions
-Operating System: WinXP SP3
+Operating System: Win7
-PHP Version:      5.3CVS-2009-02-17 (CVS)
+PHP Version:      5.3.1
 New Comment:

That's nothing different, but exactly the same problem as I described
from the beginning.

This is the function that I currently use as a workaround. It works
fine, but I would prefer php doing this itself...

function my_number_format ($number, $decimals=0, $dec_point=',',
$thousands_sep='.') {
	$string = number_format($number, $decimals, $dec_point,
$thousands_sep);
	$pos = strlen($string) - 1;
	$increase = false;
	while ($pos >= 0) {
		if (($increase) and (is_numeric($string{$pos}))) {
			$string{$pos} = chr(ord($string{$pos}) + 1);
			$increase = false;
		}
		if ($string{$pos} == ':') {
			$string{$pos} = '0';
			$increase = true;
		}
		$pos--;
	}
	if ($increase)
		$string = '1' . $string;
	return $string;
}


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

[2010-02-12 11:37:38] pajoye@php.net

For the garbage problem, that's something else. 

Can you provide a reproduce script (with data) please?

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

[2010-02-12 11:31:41] tdietsche at comcast dot net

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.

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

[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] 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:'

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

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



Thread (24 messages)

« previous php.bugs (#147460) next »