I have the next code for average.
function array_average2(){
$args = func_get_args();
if(isset($args[0])){
if(is_array($args[0])){
$ret = (array_sum($args[0]) / count($args[0]));
}else{
$ret = (array_sum($args) / func_num_args());
}
}else{
$ret = 0;
}
$ret2=0.01 * (int)($ret*100);
return $ret2;
}
i need php round to rezult next:
$ret=1.23 - i need 1
$ret=6.23 - i need 6
$ret=6.70 - i need 7
$ret=5.50 - i need 5.50
$ret=5.49 - i need 5
Conclusion if decimal is next to 0.50 to be next value, else previous but if it is fix 0.50 to stai. 5+6=5.50.. don't change
Well make use of the round()
in PHP and apply this logic
function array_average2(){
$args = func_get_args();
if(isset($args[0])){
if(is_array($args[0])){
$ret = (array_sum($args[0]) / count($args[0]));
}else{
$ret = (array_sum($args) / func_num_args());
}
}else{
$ret = 0;
}
$ret2=0.01 * (int)($ret*100);
$str = strval($ret);
if(strpos($str,'.50')!==false)
{
return $ret;
}
else
{
return round($ret2);
}
}
Probably not the best but it should work:
$string = (string)$ret;
if (substr($string, -3) != '.50') {
$ret = round($ret);
}
OR
$string = (string)$ret;
if (strrpos($string, '.50') !== 0) {
$ret = round($ret);
}