mlc*_*clm -4 php format decimal rounding ceil
我正在尝试检查变量内的数字的十进制数是否大于50或小于50。然后根据其是否大于或等于50,将十进制数四舍五入为99,如果较小则将其四舍五入为00 。
这是我的一段代码:
public function roundPrice($price)
{
return round( (ceil($price) - 0.01), 2);
}
Run Code Online (Sandbox Code Playgroud)
它使所有十进制数舍入到99。我只需要将50或更高的小数点四舍五入到99,而49或更少的小数则变为00。
我如何在PHP中实现呢?非常感谢,我被困在这里,不知道怎么做。
在偶然的机会中,OP实际上是指小数位,其中1.36变为1.00,1.60变为1.99。
可能更优雅的解决方案,但这是一个:
function roundPrice($price)
{
$intVal = intval($price);
if ($price - $intVal < .50) return (float)$intVal;
return $intVal + 0.99;
}
Run Code Online (Sandbox Code Playgroud)