取整值取决于取整值

时间:2019-07-19 06:50:03

标签: php rounding

如果使用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
?>

1 个答案:

答案 0 :(得分:1)

您可以定义自己的舍入函数,在该函数中,通过从其自身减去下限数字,然后将其与您的限制进行比较,得到数字的分数。

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
}
相关问题