0
四捨五入我需要一個可以圓從定義的自定義範圍...與指定的自定義範圍
例如功能:
0.01 - 0.39 = round to 0
0.40 - 0.74 = round to 0.5
0.75 - 0.99 = round to 1
if number is 2.33 it should show as 2.0
if number is 2.67 it should show as 2.5
if number is 2.89 it should show as 3.0
我試過的東西,但不能在所有的工作...
代碼:
$range1 = range(0.01, 0.39, 0.01);
$range2 = range(0.40, 0.74, 0.01);
$range3 = range(0.75, 0.99, 0.01);
function round_range($num) {
global $range1, $range2, $range3;
if (in_array($num, $range1))
return round($num, 0, PHP_ROUND_HALF_DOWN);
else if (in_array($num, $range2))
return round($num, 0, PHP_ROUND_HALF_DOWN);
else if (in_array($num, $range3))
return round($num, 0, PHP_ROUND_HALF_DOWN);
else
return "error";
}
echo round_range(0.35);