用 php 将十进制向上舍入到最接近的 10 的倍数



>假设我在变量$hours中有数字"13.57916667"。这是一个计算的小时,由分钟、秒到小时组成。

我希望小数四舍五入。例如,我希望$hours变为 13.6。我似乎在任何地方都找不到任何解决方案,即使在堆栈溢出上也是如此。提前感谢!

好吧,这个小片段应该可以做到:

echo round(13.579, 1);

如果您正在寻找精确的ceil()

function ceil_with_precision($value, $precision = 0) {
    return ceil($value * pow(10, $precision)) / pow(10, $precision);
}

对于 1 位小数的固定精度,这将变为:

ceil($value * 10 ) / 10;

$result = round($hours, 1);

参见 round()

试试这个

$roundednumber= round($number / 10, 0) * 10;
$hours = (float) "13.57916667";
$hours = ceil($hours * 10) / 10;

最新更新