note 101398 added to ref.bc
| From: | coops at osu1 dot php dot net | Date: | Tue, 14 Dec 2010 22:12:12 +0000 |
| Subject: | note 101398 added to ref.bc | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174921@lists.php.net to get a copy of this message | ||
The rounding function below by Charles is invalid (as it does not round negative numbers correctly)
and it does unnecessary/wrong input checking that does not work with some input.
This function is simple and rounds both negative and positive numbers:
<?php
/**
* Rounds number to specified precision.
* @param string $number
* @param integer $precision
* @see http://www.nanomvc.com/
* @return string
*/
public static function bcRound($number, $precision) {
return \bcadd($number, (\bccomp($number, "0") == -1? "-": "")
. "0." . \str_repeat("0", $precision) . "5", $precision);
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 87.96.186.214
----
Manual Page -- http://www.php.net/manual/en/ref.bc.php
Edit -- https://master.php.net/note/edit/101398
Del: integrated -- https://master.php.net/note/delete/101398/integrated
Del: useless -- https://master.php.net/note/delete/101398/useless
Del: bad code -- https://master.php.net/note/delete/101398/bad+code
Del: spam -- https://master.php.net/note/delete/101398/spam
Del: non-english -- https://master.php.net/note/delete/101398/non-english
Del: in docs -- https://master.php.net/note/delete/101398/in+docs
Del: other reasons-- https://master.php.net/note/delete/101398
Reject -- https://master.php.net/note/reject/101398
Search -- https://master.php.net/manage/user-notes.php