$n = 1.2;
//$n = 1.6;
$t = explode('.', "$n");
$n = $t[1]{0} < 5 ? $t[0] + .5 : round($n);
echo $n;
//$n = 1.6;
$t = explode('.', "$n");
$n = $t[1]{0} < 5 ? $t[0] + .5 : round($n);
echo $n;
function test($val){
$x=explode('.',sprintf("%.1f", $val));
$a=$x[0];
$d=$x[1];
if($d>0 && $d<5){
$r=$a+0.5;
}else{
$r=round($val);
}
return $r;
}echo test(1);//1
echo test(1.11);//1.5
echo test(1.5);//2
echo test(1.6);//2
return ceil($num * 2) / 2;
}
printf("%.1f : %s\n", $i, ceil($i * 2) / 2);
1.0 : 1
1.1 : 1.5
1.2 : 1.5
1.3 : 1.5
1.4 : 1.5
1.5 : 2
1.6 : 2
1.7 : 2
1.8 : 2
1.9 : 2
$arr = array(1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0,2.1);foreach($arr as $v){
echo $v.' -> '.fn($v).'<br>';
}function fn($num){
return ($ret=round($num, 0))>=$num? $ret : $ret+0.5;
}
1 -> 1
1.1 -> 1.5
1.2 -> 1.5
1.3 -> 1.5
1.4 -> 1.5
1.5 -> 2
1.6 -> 2
1.7 -> 2
1.8 -> 2
1.9 -> 2
2 -> 2
2.1 -> 2.5