CINXE.COM

PHP :: Bug #44223 :: round() sometimes returns value in E notation

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link rel='alternate' type='application/rss+xml' title='Math related Bug #44223 - RDF' href='rss/bug.php?id=44223'> <link rel='alternate' type='application/rss+xml' title='Math related Bug #44223 - RSS 2.0' href='rss/bug.php?id=44223&format=rss2'> <base href="https://bugs.php.net/"> <title>PHP :: Bug #44223 :: round() sometimes returns value in E notation</title> <link rel="shortcut icon" href="https://bugs.php.net/images/favicon.ico"> <link rel="stylesheet" href="https://bugs.php.net/css/style.css"> </head> <body> <table id="top" class="head" cellspacing="0" cellpadding="0"> <tr> <td class="head-logo"> <a href="/"><img src="images/logo.png" alt="Bugs" vspace="2" hspace="2"></a> </td> <td class="head-menu"> <a href="https://php.net/">php.net</a>&nbsp;|&nbsp; <a href="https://php.net/support.php">support</a>&nbsp;|&nbsp; <a href="https://php.net/docs.php">documentation</a>&nbsp;|&nbsp; <a href="report.php">report a bug</a>&nbsp;|&nbsp; <a href="search.php">advanced search</a>&nbsp;|&nbsp; <a href="search-howto.php">search howto</a>&nbsp;|&nbsp; <a href="stats.php">statistics</a>&nbsp;|&nbsp; <a href="random">random bug</a>&nbsp;|&nbsp; <a href="login.php">login</a> </td> </tr> <tr> <td class="head-search" colspan="2"> <form method="get" action="search.php"> <p class="head-search"> <input type="hidden" name="cmd" value="display"> <small>go to bug id or search bugs for</small> <input class="small" type="text" name="search_for" value="" size="30"> <input type="image" src="images/small_submit_white.gif" alt="search" style="vertical-align: middle;"> </p> </form> </td> </tr> </table> <table class="middle" cellspacing="0" cellpadding="0"> <tr> <td class="content"> <div id="bugheader"> <table id="details"> <tr id="title"> <th class="details" id="number"><a href="bug.php?id=44223">Bug</a>&nbsp;#44223</th> <td id="summary" colspan="5">round() sometimes returns value in E notation</td> </tr> <tr id="submission"> <th class="details">Submitted:</th> <td style="white-space: nowrap;">2008-02-22 22:20 UTC</td> <th class="details">Modified:</th> <td style="white-space: nowrap;">2008-03-13 20:01 UTC</td> <td rowspan="6"> </td> </tr> <tr id="submitter"> <th class="details">From:</th> <td>kjohnson &#x61;&#116; zootweb &#x64;&#111;&#x74; com</td> <th class="details">Assigned:</th> <td></td> </tr> <tr id="categorization"> <th class="details">Status:</th> <td>Not a bug</td> <th class="details">Package:</th> <td><a href="search.php?cmd=display&amp;package_name[]=Math+related">Math related</a></td> </tr> <tr id="situation"> <th class="details">PHP Version:</th> <td>5.2.5</td> <th class="details">OS:</th> <td>Linux 2.6.20.2</td> </tr> <tr id="private"> <th class="details">Private report:</th> <td>No</td> <th class="details">CVE-ID:</th> <td><em>None</em></td> </tr> </table> </div> <div class="controls"> <span id='control_0' class='control active'>View</span> <span id='control_1' class='control'><a href='bug.php?id=44223&amp;edit=1'>Developer</a></span> <span id='control_2' class='control'><a href='bug.php?id=44223&amp;edit=2'>Edit</a></span> </div> <div class="clear"></div> <div class='comment type_comment' ><a name="1203718833">&nbsp;</a><strong>[2008-02-22 22:20 UTC] kjohnson &#x61;&#116; zootweb &#x64;&#111;&#x74; com</strong> <pre class='note'>Description: ------------ For certain values, not necessarily large numbers, round() returns the value in exponential notation instead of the usual decimal format. Some of these &quot;special&quot; values are 1200000, 1400000, 2300000, 2400000. It seems to be just certain multiples of 100,000. Generate your own list with the code below. I suggest you start with input of: Start = 1000000 Increment = 100000 Iterations = 100 to 200 Reproduce code: --------------- &lt;html&gt;&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;&lt;body&gt; &lt;form method=&quot;post&quot; action=&quot;&lt;?=$_SERVER[&#039;PHP_SELF&#039;]?&gt;&quot;&gt; &lt;!-- some recommended values for next fields: 1000000, 100000, 200 --&gt; Start value:&lt;input type=&quot;text&quot; name=&quot;start&quot;&gt; &amp;nbsp;&amp;nbsp; Increment: &lt;input type=&quot;text&quot; name=&quot;increment&quot;&gt; &amp;nbsp;&amp;nbsp; Iterations: &lt;input type=&quot;text&quot; name=&quot;iterations&quot;&gt; &lt;input type=&quot;submit&quot;&gt; &lt;/form&gt; &lt;? if(count($_POST)) { echo &quot;&lt;p&gt;Start: &quot;, $_POST[&#039;start&#039;], &quot;; Increment: &quot;, $_POST[&#039;increment&#039;], &quot;&lt;/p&gt;&quot;; $tmp = $_POST[&#039;start&#039;]; echo round($tmp),&quot;&lt;br&gt;&quot;; for($i = 0; $i &lt; $_POST[&#039;iterations&#039;]; $i++ ) { $tmp += $_POST[&#039;increment&#039;]; echo round($tmp),&quot;&lt;br&gt;&quot;; } } ?&gt; &lt;/body&gt;&lt;/html&gt; Expected result: ---------------- All values to be returned in standard decimal format, e.g., &quot;1200000&quot;. Actual result: -------------- Certain values are returned in exponential format, e.g., &quot;1.2E+6&quot;. </pre> </div><h2>Patches</h2> <h2>Pull Requests</h2> <h2 style="border-bottom:2px solid #666;margin-bottom:0;padding:5px 0;">History</h2><div id='comment_filter' class='controls comments'><span id='type_all' class='control active' onclick='do_comment(this);'>All</span><span id='type_comment' class='control ' onclick='do_comment(this);'>Comments</span><span id='type_log' class='control ' onclick='do_comment(this);'>Changes</span><span id='type_svn' class='control ' onclick='do_comment(this);'>Git/SVN commits</span><span id='type_related' class='control ' onclick='do_comment(this);'>Related reports</span> </div> <div id='comments_view' style='clear:both;'> <div class='comment type_comment' ><a name="1203882666">&nbsp;</a><strong>[2008-02-24 19:51 UTC] <a href="//people.php.net/jani">jani@php.net</a></strong> <pre class='note'>Please try using this CVS snapshot: <a href="http://snaps.php.net/php5.2-latest.tar.gz" rel="nofollow">http://snaps.php.net/php5.2-latest.tar.gz</a> For Windows (zip): <a href="http://snaps.php.net/win32/php5.2-win32-latest.zip" rel="nofollow">http://snaps.php.net/win32/php5.2-win32-latest.zip</a> For Windows (installer): <a href="http://snaps.php.net/win32/php5.2-win32-installer-latest.msi" rel="nofollow">http://snaps.php.net/win32/php5.2-win32-installer-latest.msi</a> </pre> </div><div class='comment type_comment' ><a name="1203955681">&nbsp;</a><strong>[2008-02-25 16:08 UTC] kjohnson &#x61;&#116; zootweb &#x64;&#111;&#x74; com</strong> <pre class='note'>Thanks for your response! Sorry, I am not able to change my PHP version, so I can&#039;t try the CVS snapshot. </pre> </div><div class='comment type_comment' ><a name="1204829054">&nbsp;</a><strong>[2008-03-06 18:44 UTC] a &#x64;&#111;&#x74; u &#x64;&#111;&#x74; savchuk &#x61;&#116; gmail &#x64;&#111;&#x74; com</strong> <pre class='note'>bug is not fixed yet in php-5.2.5 $ ./sapi/cli/php ./test1.php | grep &#039;E+&#039; 1.2E+6 1.4E+6 2.3E+6 2.4E+6 2.8E+6 3.3E+6 3.4E+6 3.8E+6 4.1E+6 4.3E+6 4.6E+6 4.8E+6 5.1E+6 5.3E+6 5.6E+6 5.8E+6 6.1E+6 6.3E+6 6.6E+6 6.8E+6 7.1E+6 7.3E+6 7.6E+6 7.8E+6 8.1E+6 8.2E+6 8.6E+6 8.7E+6 9.1E+6 9.2E+6 9.6E+6 9.7E+6 test script is: $ cat ./test1.php &lt;?php for( $tmp = 0, $i = 0; $i &lt; 100; $i++ ) { $tmp += 100000; echo round($tmp),&quot;\n&quot;; } ?&gt; php version is: $ ./sapi/cli/php -v PHP 5.2.5 (cli) (built: Mar 2 2008 02:27:40) (DEBUG) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies </pre> </div><div class='comment type_comment' ><a name="1205410474">&nbsp;</a><strong>[2008-03-13 12:14 UTC] <a href="//people.php.net/jani">jani@php.net</a></strong> <pre class='note'>I don&#039;t see any bug here, it&#039;s perfectly normal to _print_ it that way since those are floats. </pre> </div><div class='comment type_comment' ><a name="1205410495">&nbsp;</a><strong>[2008-03-13 12:14 UTC] <a href="//people.php.net/jani">jani@php.net</a></strong> <pre class='note'>Hint: Try replacing the &#039;echo&#039; with var_dump().. </pre> </div><div class='comment type_comment' ><a name="1205438477">&nbsp;</a><strong>[2008-03-13 20:01 UTC] kjohnson &#x61;&#116; zootweb &#x64;&#111;&#x74; com</strong> <pre class='note'>Thanks for your response, Jani. OK, so it&#039;s not a bug. Would you agree that: 1. It is a change in behavior from PHP 4? 2. This change in behavior is undocumented? This behavior is not normal when compared to the behavior in PHP 4. I would not call erratic changes in the format of return values &quot;normal&quot; in any case, since I have never seen anything like this before in other languages. And, yes, it is only _printing_, but _printing_ counts. Here&#039;s an example of how _printing_ counts. My code that ran fine on PHP 4 suddenly threw an error on PHP 5. This code for a financial transaction used to work just fine, now it breaks for certain values when writing those values into xml, since the value no longer validates against the xsd when round() returns the value in exponential notation: &#039;&lt;AnnualIncome&gt;&#039; . round($income) . &#039;&lt;/AnnualIncome&gt;&#039; Thanks, again, for responding. </pre> </div></div> </td> </tr> </table> <script src='js/util.js'></script> <script src='https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script> <script src="js/jquery.cookie.js"></script> <script> function do_comment(nd) { $('#comment_filter > .control.active').removeClass("active"); $(nd).addClass("active"); $.cookie('history_tab', nd.id, { expires: 365 }); if (nd.id == 'type_all') { $('#comments_view > .comment:hidden').show('slow'); } else { $('#comments_view > .comment').each(function(i) { if ($(this).hasClass(nd.id)) { $(this).show('slow'); } else { $(this).hide('slow'); } }); } return false; } </script> <table class="foot" cellspacing="0" cellpadding="0"> <tr> <td class="foot-bar" colspan="2">&nbsp;</td> </tr> <tr> <td class="foot-copy"> <small> <a href="https://php.net/"><img src="images/logo-small.gif" align="left" valign="middle" hspace="3" alt="PHP"></a> <a href="https://php.net/copyright.php">Copyright &copy; 2001-2025 The PHP Group</a><br> All rights reserved. </small> </td> <td class="foot-source"> <small>Last updated: Thu Mar 20 13:01:32 2025 UTC</small> </td> </tr> </table> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10