|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 14:00:01 2025 UTC |
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