舍入值取决于舍入值

Round values depend rounding value

如果使用 round() ,如果数字大于 0.5 则向上舍入,如果数字小于 0.5 则向下舍入,我有一个条件,如果你高于 0.3 则向上舍入,小于 0.3 则向上舍入吃下。

<?php 
//normal round
echo round(3.4);         // 3
echo round(3.5);         // 4
echo round(3.6);         // 4

//round i want
echo round(3.34);         // 4
echo round(3.29);         // 3
echo round(3.3);          // 3
?>

您可以定义自己的 rounding-function,您可以通过从自身中减去底数来获得数字的分数,然后将其与您的限制进行比较。

function my_round($value, $round_up_from = 0.3) {
    $fraction = $value - floor($value);  // Get the fraction
    if ($fraction >= $round_up_from) {   // If the fraction is equal to or above threshold, ceil it
        return ceil($value);
    }
    return floor($value);                // Otherwise, floor it
}