note 109731 deleted from function.round by googleguy

From: Date: Sun, 09 Dec 2012 08:13:21 +0000
Subject: note 109731 deleted from function.round by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-192405@lists.php.net to get a copy of this message
Note Submitter: kogut at kogumelo dot com dot br ---- I've got this strange precision problem: <?php $decimals = 1; $n=5.95; echo "<br>n=",$n," ==> ",round($n, $decimals); // 5.95 ==> 6 ! $n = ( 11.9 ) / 2; echo "<br>n=",$n," ==> ",round($n, $decimals); // 5.95 ==> 6 ! $n = ( 2.7 + 9.2 ) / 2; echo "<br>n=",$n," ==> ",round($n, $decimals); // 5.95 ==> 5.9 ?? ?> To adjust it, i've done: <?php $n = ( 2.7 + 9.2 ) / 2; $n += pow (10, -( $decimals + 2)) * 1; echo "<br>n=",$n," ==> ",round($n, $decimals); // 5.951 ==> 6 ! ?>

« previous php.notes (#192405) next »