php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45978 round() errors.
Submitted: 2008-09-03 02:09 UTC Modified: 2008-09-03 11:18 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: firealwaysworks at gmail dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.6 OS: Linux
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: firealwaysworks at gmail dot com
New email:
PHP Version: OS:

 

 [2008-09-03 02:09 UTC] firealwaysworks at gmail dot com
Description:
------------
In this example the multiplication of two floats yields a number with 5 significant figures.  The round() function incorrectly handles this number only when it is working with the answer to a mathematical operation.  It is important to note that the round() function is working properly for static values. 

I found this because I am writing financial software in PHP.   I have absolutely no doubt that this bug would cause our company to loose money.

Reproduce code:
---------------
print 21.33*1.015."<br>";
print round(21.33*1.015,2)."<br>";
print round(21.33*1.015,4)."<br>";
print round(21.64995,4);

Expected result:
----------------
21.64995
21.65
21.65
21.65

Actual result:
--------------
21.64995
21.65
21.6499
21.65

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-03 05:16 UTC] mattwil@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.

The result of 21.33*1.015 is stored as slightly less than 21.64995 (21.649949999999997 on my Windows system), different than when you write the literal static value. The result of 21.33*1.015 when printing it (happens differently than round() function, so results may vary) or rounding 5-14 decimal places, those extra internal 9s are "rolled over."
 [2008-09-03 05:50 UTC] firealwaysworks at gmail dot com
Cool,  but this is still an inconsistency.  Any string value of the number is treated is differently then when passed to round().   This is still a very serious problem for us because we are storing all of this information in a SQL database,  the number will be lost when we build the query. 

I think you'll tell me that you don't care if people loose money when they use your platform and that you'll tell me to use someone Else's code,  like BC Math.   I think that relying on 3rd party extensions to fix your mistakes is unprofessional.

Peace
 [2008-09-03 11:18 UTC] johannes@php.net
It's the CPU which is "broken", read the link provided.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jun 14 00:01:33 2025 UTC