note 84809 modified in function.round by danbrown
| From: | danbrown@php.net | Date: | Wed, 30 Jul 2008 23:12:41 +0000 |
| Subject: | note 84809 modified in function.round by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142964@lists.php.net to get a copy of this message | ||
In case anyone has a problem like me ever, were you are doing very large stat calculations on a
array and end up with floats with way to large of precision. A good way to round them all to N
length is below.
<?php
public function recursiveRound(array &$arr, $precision)
{
foreach($arr as $key => $val) {
if(is_array($val)) {
$this->recursiveRound($arr[$key], $precision);
} elseif(is_float($val)) {
$arr[$key] = round($arr[$key], $precision);
}
}
return $arr;
}
?>
--was--
In case anyone has a problem like me ever, were you are doing very large stat calculations on a
array and end up with floats with way to large of precision. A good way to round them all to N
length is below.
public function recursiveRound(array &$arr, $precision)
{
foreach($arr as $key => $val) {
if(is_array($val)) {
$this->recursiveRound($arr[$key], $precision);
} elseif(is_float($val)) {
$arr[$key] = round($arr[$key], $precision);
}
}
return $arr;
}
http://php.net/manual/en/function.round.php