Page 1 of 1

number_format() Is this a bug?

Posted: Wed May 28, 2008 8:54 pm
by Benjamin
This was creating a lot of problems for me today while trying to track down errors in 8 byte (64 bit) bitmaps.

Code: Select all

 
echo "18446744073709551615 = " . number_format('18446744073709551615', 0, '', '') . "\n";
 
Outputs:
18446744073709551615 = 18446744073709551616
:( Heads up I guess.

PHP Version 5.2.6

Re: number_format() Is this a bug?

Posted: Wed May 28, 2008 9:36 pm
by Chris Corbyn
Floating point errors probably. Very odd though.

Re: number_format() Is this a bug?

Posted: Sat May 31, 2008 8:22 am
by Ambush Commander
The string is being cast into a float, and losing precision. This is obvious with a printf:

Code: Select all

$v = (float) '18446744073709551615';
printf('%f', $v); // 18446744073709551616.00000027
Use BCMath to get around this.

Re: number_format() Is this a bug?

Posted: Wed Jun 04, 2008 2:59 am
by Maugrim_The_Reaper
It's a big integer - way past the 32bit limit. So it's a float, not an integer. Like AC said, you need BCMath, or preferably GMP (it's a great deal faster than BCMath).

Re: number_format() Is this a bug?

Posted: Wed Jun 04, 2008 4:06 am
by Benjamin
Yeah it's the largest unsigned 64 bit integer I believe. I ended up using the gmp family of functions. Apparently that's only (64 bit) number that I couldn't accurately reproduce on a 64 bit processor.