php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41065 number_format() inconsistant rounding
Submitted: 2007-04-12 16:15 UTC Modified: 2007-04-12 16:26 UTC
From: michel dot gervais at hrsdc-rhdsc dot gc dot ca Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 4.4.6 OS: win2k
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: michel dot gervais at hrsdc-rhdsc dot gc dot ca
New email:
PHP Version: OS:

 

 [2007-04-12 16:15 UTC] michel dot gervais at hrsdc-rhdsc dot gc dot ca
Description:
------------
number_format(2009.00 * 0.2550,2); = 512.29 <= NOT rounded

----------------
Similar to Bug #22712  << whch has been marked as "bogus/closed" has actuall errors, as last poster mentions.

<?
      echo number_format(63.745, 2, '.', "");
      echo number_format(64.745, 2, '.', "");
?>

Result:

63.74
64.75
---------------


Reproduce code:
---------------
number_format(2009.00 * 0.2550,2); = 512.29 << NOT rounded
Should be 519.30
The following 2 lines "are" rounded.

number_format(5949.00 * 0.2550,2); = 1,517.00 << is rounded
number_format(721.00 * 0.2550,2); = 183.86    << is rounded

for some reason it doesn't round 512.29 to 512.30
but it works fine for 
1,517.00 => 1,516.99
183.86 => 183.855
------------------------------------------------------------
Code
<? echo number_format(5949.00 * 0.2550,2); ?>
<? echo number_format(2009.00 * 0.2550,2); ?>
<? echo number_format(721.00 * 0.2550,2); ?>


Expected result:
----------------
1,517.00
512.30
183.86



Actual result:
--------------
1,517.00
512.29
183.86



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-12 16:26 UTC] tony2001@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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 13 05:01:28 2024 UTC