#48455 [Bgs]: sprintf error when multiplying float and displaying digit
| From: | aldo at totalaldo dot com | Date: | Tue, 02 Jun 2009 18:04:19 +0000 |
| Subject: | #48455 [Bgs]: sprintf error when multiplying float and displaying digit | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-137982@lists.php.net to get a copy of this message | ||
ID: 48455
User updated by: aldo at totalaldo dot com
Reported By: aldo at totalaldo dot com
Status: Bogus
Bug Type: Strings related
Operating System: FreeBSD 7.2-RELEASE
PHP Version: 5.2.9
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2009-06-02 17:52:23] 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 17:51:03] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=48455&edit=1