|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2007-08-18 12:21 UTC] Makinen_Juha at hotmail dot com
[2007-08-19 10:12 UTC] johannes@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 20:00:01 2025 UTC |
Description: ------------ In the code below, i get wrong results with php 5.2.3 and 5.2.2. The code tries to round 3 digit numbers to 2 digit numbers. Round fails at 1.225 and calculated numbers at 4.264. Iam creating a big calculating software with this language and i hope this is my mistake :) Reproduce code: --------------- <?php for ( $i=0.01;$i<10;$i=$i+0.003 ) { echo $i." -> ".calcRound( $i )."<br>"; echo round($i,2)."<br>"; } function calcRound( $inNumber ) { $intnum = $inNumber*100; $intnum = (int) $intnum; $decnum = $inNumber*100; if ( $decnum - $intnum > 0.4 ) { $intnum++; } return $intnum / 100; } ?> Expected result: ---------------- ... 1.255 -> 1.26 (calculated round) 1.26 (round function) ... ... 4.264 -> 4.26 (calculated round) 4.26 (with round function) 4.267 -> 4.27 (calculated round) ... Actual result: -------------- ... 1.255 -> 1.26 (calculated round) 1.25 (round function) ... ... 4.264 -> 4.27 (calculated round) 4.26 (with round function) 4.2670000000001 -> 4.27 (calculated round) ...