php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48455 sprintf error when multiplying float and displaying digit
Submitted: 2009-06-02 17:51 UTC Modified: 2009-06-02 18:04 UTC
From: aldo at totalaldo dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.2.9 OS: FreeBSD 7.2-RELEASE
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: aldo at totalaldo dot com
New email:
PHP Version: OS:

 

 [2009-06-02 17:51 UTC] aldo at totalaldo dot com
Description:
------------
php -r 'echo sprintf("%6d", 34.98*100);'

output:  3497

Reproduce code:
---------------
php -r 'echo sprintf("%6d", 34.98*100);'


Expected result:
----------------
  3498

Actual result:
--------------
  3497

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-06-02 17:52 UTC] rasmus@php.net
Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is, read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.


 [2009-06-02 18:04 UTC] aldo at totalaldo dot com
Thank you for your prompt reply. I was aware of the limitations of 
Floating point representations.. Hopefully, anyone using similar code 
will now use another technique. 

php -r 'echo sprintf("%6s", 34.98*100);'  outputs the expected result of 
3498.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon May 12 13:01:27 2025 UTC