php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #9248 float,int multiplication problem when using sprintf
Submitted: 2001-02-13 18:21 UTC Modified: 2001-02-13 18:45 UTC
From: mike at meltmedia dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 4.0.4pl1 OS: Redhat 6.2
Private report: No CVE-ID: None
 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-02-13 18:35 UTC] hholzgra@php.net
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
 [2001-02-13 18:45 UTC] andre@php.net
bogus
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Nov 17 19:01:35 2019 UTC