php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40304 Round() function strange issue
Submitted: 2007-02-01 10:02 UTC Modified: 2007-02-01 10:21 UTC
From: tjouanne at hotmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 4.4.4 OS: Linux and windows
Private report: No CVE-ID: None
 [2007-02-01 10:02 UTC] tjouanne at hotmail dot com
Description:
------------
round number with round() function give strange result:
9.815,609.815,4690.815,14609.815,6.735,56.735,1056.735,11056.735
produce
9.82
609.82
4609.81
14609.82
6.74
56.74
1056.73
11056.74

Why it's become 81 or 73 instead of 82 or 74

Reproduce code:
---------------
<?
echo (round("9.815", 2)."<BR>");
echo (round("609.815", 2)."<BR>");
echo (round("4609.815", 2)."<BR>");
echo (round("14609.815", 2)."<BR>");
echo (round("6.735", 2)."<BR>");
echo (round("56.735", 2)."<BR>");
echo (round("1056.735", 2)."<BR>");
echo (round("11056.735", 2)."<BR>");
?>

Expected result:
----------------
9.82
609.82
4609.82
14609.82
6.74
56.74
1056.74
11056.74

Actual result:
--------------
9.82
609.82
4609.81
14609.82
6.74
56.74
1056.73
11056.74

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-02-01 10:13 UTC] derick@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.

.
 [2007-02-01 10:21 UTC] tjouanne at hotmail dot com
Do you know if there is a function in php who works correctly for this need?
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 05 01:01:35 2025 UTC