note 73522 added to ref.bc
| From: | udochen at gmail dot com | Date: | Tue, 27 Feb 2007 19:48:25 +0000 |
| Subject: | note 73522 added to ref.bc | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-123930@lists.php.net to get a copy of this message | ||
Code below implements standard rounding on 5 or higer round up, else don't round. There
wasn't a round function for the BC functions, so here is a simple one that works. Same args as
round, except takes strings and returns a string for more BC operations.
----------------
function roundbc($x, $p) {
$x = trim($x);
$data = explode(".",$x);
if(substr($data[1],$p,1) >= "5") {
//generate the add string.
$i=0;
$addString = "5";
while($i < $p) {
$addString = "0" . $addString;
$i++;
}//end while.
$addString = "." . $addString;
//now add the addString to the original fraction.
$sum = bcadd($data[0] . "." . $data [1],$addString,$p+1);
//explode the result.
$sumData = explode(".",$sum);
//now, return the correct precision on the rounded number.
return $sumData[0] . "." . substr($sumData[1],0,$p);
} else {
//don't round the value and return the orignal to the desired
//precision or less.
return $data[0] . "." . substr($data[1],0,$p);
}//end if/else.
}//end roundbc.
----
Server IP: 64.71.164.2
Probable Submitter: 64.207.232.142 (proxied: 1.0 WIN-WEBFILTER)
----
Manual Page -- http://www.php.net/manual/en/ref.bc.php
Edit -- https://master.php.net/note/edit/73522
Del: integrated -- https://master.php.net/note/delete/73522/integrated
Del: useless -- https://master.php.net/note/delete/73522/useless
Del: bad code -- https://master.php.net/note/delete/73522/bad+code
Del: spam -- https://master.php.net/note/delete/73522/spam
Del: non-english -- https://master.php.net/note/delete/73522/non-english
Del: in docs -- https://master.php.net/note/delete/73522/in+docs
Del: other reasons-- https://master.php.net/note/delete/73522
Reject -- https://master.php.net/note/reject/73522
Search -- https://master.php.net/manage/user-notes.php