note 98172 deleted from function.bcscale by danbrown

From: Date: Tue, 01 Jun 2010 14:56:55 +0000
Subject: note 98172 deleted from function.bcscale by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-169798@lists.php.net to get a copy of this message
Note Submitter: Thriault ---- None of the bc functions round the resulting number to the scale specified, but instead truncate the resulting number to the correct scale. The calculation includes all digits regardless of the scale, however, so the following equals 3 instead of 2: <?php bcadd('1.4000000000000000001', '1.5999999999999999999', 0); ?> Knowing this allows the round function that mwgamera posted to be shortened to one line. <?php function bcround($number, $scale = 0) { return bcadd($number, '0.' . str_repeat('0', $scale) . '5', $scale); } ?> ..or for those of you who don't want a function, all you need to do is bcadd(yournumber, '0.5', 0) to round to a whole digit, bcadd(yournumber, '0.05', 1) to round to the tens place, bcadd(yournumber, '0.005', 1) to round to the hundreds place, and so forth...

« previous php.notes (#169798) next »