|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54094 Sprintf change integer with %d
Submitted: 2011-02-24 18:06 UTC Modified: 2011-02-24 18:16 UTC
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: mallsbill at gmail dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.2.17 OS: Linux debian 2.6.26-2-686
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: mallsbill at gmail dot com
New email:
PHP Version: OS:


 [2011-02-24 18:06 UTC] mallsbill at gmail dot com
From manual page:

with some integer obtain after an operation from a float, sprintf('%d', $val) return a different value

Test script:
$var1 = 4.77*100;
echo $var2 = sprintf("%d", $var1);

Expected result:
should return 477

Actual result:
return 476

works when cast in string
$var1 = 4.77*100;
echo $var2 = sprintf("%d", (string)$var1);


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-24 18:16 UTC]
-Status: Open +Status: Bogus
 [2011-02-24 18:16 UTC]
Floating point math is not exact.  4.77 * 100 cannot be accurately represented so 
it ends up being 476.9999999999999999 and when you cast that to an integer the way 
you are doing you get 476. You can read more about it here:

In your case add a round() to the appropriate precision on your floating point 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Oct 20 01:01:23 2020 UTC