php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68589 sprintf decimal number formatting
Submitted: 2014-12-11 06:41 UTC Modified: 2014-12-11 17:43 UTC
From: vardhane at gmail dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.6.3 OS:
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: vardhane at gmail dot com
New email:
PHP Version: OS:

 

 [2014-12-11 06:41 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-12-11 17:43 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2014-12-11 17:43 UTC] aharvey@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://www.floating-point-gui.de/

Thank you for your interest in PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 09:01:30 2024 UTC