note 98172 added to function.bcscale

From: Date: Sun, 30 May 2010 09:24:27 +0000
Subject: note 98172 added to function.bcscale
Groups: php.notes 
Request: Send a blank email to php-notes+get-169724@lists.php.net to get a copy of this message
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... ---- Server IP: 64.71.164.2 Probable Submitter: 76.25.100.26 ---- Manual Page -- http://www.php.net/manual/en/function.bcscale.php Edit -- https://master.php.net/note/edit/98172 Del: integrated -- https://master.php.net/note/delete/98172/integrated Del: useless -- https://master.php.net/note/delete/98172/useless Del: bad code -- https://master.php.net/note/delete/98172/bad+code Del: spam -- https://master.php.net/note/delete/98172/spam Del: non-english -- https://master.php.net/note/delete/98172/non-english Del: in docs -- https://master.php.net/note/delete/98172/in+docs Del: other reasons-- https://master.php.net/note/delete/98172 Reject -- https://master.php.net/note/reject/98172 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#169724) next »