Re: Problem with number_format

From: Date: Tue, 25 May 2004 15:07:04 +0000
Subject: Re: Problem with number_format
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-186897@lists.php.net to get a copy of this message
* Thus wrote John W. Holmes (holmes072000@charter.net): > From: <fku@freemail.it> > > > please suppose PHP 4.3.2 and $number=502,3550 > > > > number_format($number,2,'.',' ') returns 502.36. > > > > It seems ok, but if $number=253,0650 > > > > number_format($number,2,'.',' ') returns 253.06 instead of 253.07. > > > > Why? > > I've noticed this non-coherent approximation behaviour when > > 3rd decimal number is 5 and decimals are two many many > > times. > > Isn't there some "rule" where you round up on odd numbers and don't round > up > on even, otherwise it comes out unevenly rounded up? > > So 0.15, 0.35, 0.55 will be rounded up to 0.2, 0.4, and 0.6, respectively, > whereas 0.25, 0.45, 0.65 will be rounded down to 0.2, 0.4, and 0.6, > respectively... > > Your test cases seem to fit this. Interesting theory, but php5 doesn't show that behaviour: <?php for($i =0; $i < 9; $i++) { $number = "253.0{$i}50"; echo $number, ': ', number_format($number, 2, '.', ' '), "\n"; } 253.0050: 253.01 253.0150: 253.02 253.0250: 253.03 253.0350: 253.04 253.0450: 253.05 253.0550: 253.06 253.0650: 253.07 253.0750: 253.08 253.0850: 253.09 Curt -- "I used to think I was indecisive, but now I'm not so sure."

« previous php.general (#186897) next »