php round up / ceil merge mysql AVG()
문제
or Show Average i have this :
$item = mysql_query("SELECT AVG(top) AS total FROM " . "$config_ccms_prefix" . "news where id='$id'");
while ($cms = mysql_fetch_assoc($item)) {
$avg = ceil(($cms[total]),0.5);
}
//......
example : i need to round up if $ccms[total] = 3.4 to 3.5 or 7.8 to 8 or 9.3 to 9.5 BUT not round 9.5 to 10 or 4.5 to 5 .Actually, I'm not sure it is possible.
$avg not work for me !!
해결책
You need to do this then:
$avg = ceil($cms['total']*2)/2; //result of
Results will be:
1.5 = ceil(1.1*2)/2; //CEIL(2.2) = 3.0 / 2 = 1.5;
1.5 = ceil(1.2*2)/2; //CEIL(2.4) = 3.0 / 2 = 1.5;
1.5 = ceil(1.3*2)/2; //CEIL(2.6) = 3.0 / 2 = 1.5;
1.5 = ceil(1.4*2)/2; //CEIL(2.8) = 3.0 / 2 = 1.5;
1.5 = ceil(1.5*2)/2; //CEIL(3.0) = 3.0 / 2 = 1.5;
2 = ceil(1.6*2)/2; //CEIL(3.2) = 4.0 / 2 = 2;
2 = ceil(1.7*2)/2; //CEIL(3.4) = 4.0 / 2 = 2;
2 = ceil(1.8*2)/2; //CEIL(3.6) = 4.0 / 2 = 2;
2 = ceil(1.9*2)/2; //CEIL(3.8) = 4.0 / 2 = 2;
2 = ceil(2*2)/2; //CEIL(4) = 4.0 / 2 = 2;
Cheers
다른 팁
Also that function can be helpful
// duplicates Excel's ceiling function
if (!function_exists('ceiling')) {
function ceiling($number, $significance = 1) {
return (is_numeric($number) &&
is_numeric($significance) ) ?
(ceil($number / $significance) * $significance) : false;
}
}
$data = array(1.2, 1.1, 3.5, 3.8, 4, 0.6, 4.1);
foreach ($data as $value) {
echo $value . ' | ' . ceiling($value, 0.5) . '<br/>';
}
This will result in
1.2 | 1.5
1.1 | 1.5
3.5 | 3.5
3.8 | 4
4 | 4
0.6 | 1
4.1 | 4.5
제휴하지 않습니다 StackOverflow