strange behaviour with sprintf

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

Not quite a question about objects, so sorry if off-topic, but I'm 
hoping one of you has come across this before...
A fellow developer found an obscure 'bug' in the sprintf function 
with the following code:
<?
$sum = 78.24*100;
echo "$sum\n";
echo sprintf("%011d",$sum);
echo "\n";

$sum = 78.25*100;
echo "$sum\n";
echo sprintf("%011d",$sum);
echo "\n";
?>
That results in the following output:
7824
00000007823
7825
00000007825

By rights, the first value should have resulted in
00000007824

There's a workaround:
instead of:
echo sprintf("%011d",$sum);
use
echo sprintf("%011s",strval($sum));
which seems to work in all test cases.

I've also tested on different platforms with different processors and 
different 4.3.x versions of PHP.

Has anyone seen this before, and if so, why does it fail for 78.24 
(and several other values)?

Thanks for your time,
Brian




------------------------ Yahoo! Groups Sponsor --------------------~--> 
Most low income households are not online. Help bridge the digital divide today!
http://us.click.yahoo.com/cd_AJB/QnQLAA/TtwFAA/saFolB/TM
--------------------------------------------------------------------~-> 

PHP Data object relational mapping generator - http://www.meta-language.net/ 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php-objects/

<*> To unsubscribe from this group, send an email to:
    php-objects-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 




[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux