时间:2021-07-01 10:21:17 帮助过:12人阅读
$n = 1.2;//$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);//1echo test(1.11);//1.5echo test(1.5);//2echo test(1.6);//2    function fn($num) {  
     return ceil($num * 2) / 2;  
 } 
$n = 1.2;//$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);//1echo test(1.11);//1.5echo test(1.5);//2echo test(1.6);//2    需求要说清楚
for($i=1; $i<2; $i+=0.1)  printf("%.1f : %s\n", $i, ceil($i * 2) / 2);  1.0 : 11.1 : 1.51.2 : 1.51.3 : 1.51.4 : 1.51.5 : 21.6 : 21.7 : 21.8 : 21.9 : 2
   function fn($num) {   
     return ceil($num * 2) / 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).'
';}function fn($num){	return ($ret=round($num, 0))>=$num? $ret : $ret+0.5;}  
需求要说清楚
for($i=1; $i<2; $i+=0.1)  printf("%.1f : %s\n", $i, ceil($i * 2) / 2);   1.0 : 11.1 : 1.51.2 : 1.51.3 : 1.51.4 : 1.51.5 : 21.6 : 21.7 : 21.8 : 21.9 : 2