|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2001-02-13 18:21 UTC] mike at meltmedia dot com
Sometimes when trying to multiply a float and a int then print the result out with sprintf there is a error in the computation.
The following code produces the incorrect output:
<?
$val1 = 144.98;
$result = ($val1 * 100);
echo $result . "\n"; ## Correct output '14498'
echo sprintf ("%10d\n", $result); ## Incorrect output '14497'
?>
Not sure if there is something obvious that I am missing? Any help would be appreciated.
My config line:
./configure --with-mysql=/usr --with-apxs=/usr/sbin/apxs --with-gd=/usr/local --enable-trans-sid --enable-track-vars --enable-bcmath --with-ttf
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 19:00:01 2025 UTC |
its the good old binary/decimal rounding issue $result is internally something like e.g.14497.999999999998 when using plain echo rounding will be applied (see php.ini 'precision') while sprintf "%d" does not round but will cut of all decimals sprintf("%d",round($result)) should work find