freelanceprogrammers.org Forum Index » PHP

strange behaviour with sprintf


View user's profile Post To page top
slickhickus001 Posted: Thu Sep 08, 2005 7:17 pm


Joined: 09 May 2005

Posts: 6
strange behaviour with sprintf
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
";
echo sprintf("%011d",$sum);
echo "
";

$sum = 78.25*100;
echo "$sum
";
echo sprintf("%011d",$sum);
echo "
";
?>
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
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help