Bug #68589 [Opn->Nab]: sprintf decimal number formatting
| From: | aharvey@php.net | Date: | Thu, 11 Dec 2014 17:43:39 +0000 |
| Subject: | Bug #68589 [Opn->Nab]: sprintf decimal number formatting | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-189019@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68589&edit=1
ID: 68589
Updated by: aharvey@php.net
Reported by: vardhane at gmail dot com
Summary: sprintf decimal number formatting
-Status: Open
+Status: Not a bug
Type: Bug
Package: Strings related
PHP Version: 5.6.3
Block user comment: N
Private report: N
New Comment:
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://www.floating-point-gui.de/
Thank you for your interest in PHP.
Previous Comments:
------------------------------------------------------------------------
[2014-12-11 06:41:33] vardhane at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.sprintf
---
<?php
$format = '%10.2f';
$args = 103790.3550;
$result = sprintf ($format, $args);
?>
Test script:
---------------
Hello Team,
When I am using sprintf to format my decimal 2 decimal places it is giving weird results. I tried
the following,
0.405 = 0.40
0.415 = 0.41
0.425 = 0.43
0.435 = 0.43
0.445 = 0.44
0.455 = 0.46
0.465 = 0.46
0.475 = 0.48
0.485 = 0.49
0.495 = 0.49
Please Help!
Expected result:
----------------
103790.36
Actual result:
--------------
103790.35
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68589&edit=1