php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53949 Rounding error
Submitted: 2011-02-07 14:13 UTC Modified: 2011-02-07 15:58 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: pleszczynski at zubi dot pl Assigned:
Status: Wont fix Package: Unknown/Other Function
PHP Version: 5.2.17 OS: Linux
Private report: No CVE-ID: None
 [2011-02-07 14:13 UTC] pleszczynski at zubi dot pl
Description:
------------
PHP 5.2.13 & 5.2.17 both have this weird errors in rounding function. This happens quite often, I can supply other numbers as well.

Test script:
---------------
<?php
echo "       1.505:         " . round(1.505, 2) . "\n";
echo "      11.505:        " . round(11.505, 2) . "\n";
echo "     111.505:       " . round(111.505, 2) . "\n";
echo "     111.505:      " . round(1111.505, 2) . "\n";
echo "    1111.505:     " . round(11111.505, 2) . "\n";
echo "   11111.505:    " . round(111111.505, 2) . "\n";
echo "  111111.505:   " . round(1111111.505, 2) . "\n";
echo " 1111111.505:  " . round(11111111.505, 2) . "\n";
echo "11111111.505: " . round(111111111.505, 2) . "\n";

Expected result:
----------------
       1.505:         1.51
      11.505:        11.51
     111.505:       111.51
     111.505:      1111.51
    1111.505:     11111.51
   11111.505:    111111.51
  111111.505:   1111111.51
 1111111.505:  11111111.51
11111111.505: 111111111.51


Actual result:
--------------
       1.505:         1.51
      11.505:        11.51
     111.505:       111.51
     111.505:      1111.51
    1111.505:     11111.51
   11111.505:    111111.51
  111111.505:   1111111.5
 1111111.505:  11111111.51
11111111.505: 111111111.51


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-07 14:21 UTC] delphists at apollo dot lv
Getting invalid results for values between 1048576.505 and 1342176.505:
<?php
for ( $n = 1048574.505; $n <= 1342178.505; ++$n ) {
    $rounded = (string)round($n, 2);
    if ( preg_match('#.51$#', $rounded) ) {
        echo "Got correct result with n = {$n}\n";
    }
}
?>

Result:
Got correct result with n = 1048574.505
Got correct result with n = 1048575.505
Got correct result with n = 1342177.505
Got correct result with n = 1342178.505
 [2011-02-07 15:58 UTC] cataphract@php.net
-Status: Open +Status: Wont fix
 [2011-02-07 15:58 UTC] cataphract@php.net
This issue only affects PHP 5.2, which is not supported anymore.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC