On Tue, April 18, 2006 10:19 am, Chris Boget wrote:
> What's going on here:
>
> $number = 5000000000.1234567890;
> echo sprintf( '%.05f', $number );
>
> result: 5000000000.12346
>
> $number = 5000000000000.1234567890;
> echo sprintf( '%.05f', $number );
>
> result: 5000000000000.12305
>
> $number = 5000000000000000.1234567890;
> echo sprintf( '%.05f', $number );
>
> result: 5000000000000000.00000
>
> Why isn't it formatting the decimals properly? And apparently losing
> some?
>
> Running PHP 4.3.11 on Windows Server 2003 box.

PHP stores float/decimal numbers internally in 4-byte (32-bit) values
using standard C float libraries.

Rounding things off is guaranteed to produce results like this.

Though the one that ends in .12305 is a bit odd...

Still, if you NEED any sort of verifiable accuracy or especially large
scale, you'll have to use a custom library that deals with decimal
numbers as strings and allows you to choose arbitrary precision.

You'll sacrifice speed, of course.  And RAM.  How much you sacrifice
depends on how much accuracy you need, to some degree, but it's also
just inherently slower and more resource-intensive to do all the
calculations as strings.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to