我试过了 round()
echo round(94,-1); // 90
它返回 90 ,但我想要 95
1。四舍五入到 next 5的倍数,排除当前数
行为:50个输出55,52个输出55
roundUpToAny($ n,$ x = 5){
return round(($ n + $ x / 2 )/ $ X)* $ X;
}
2。四舍五入为5的倍数,包括当前数字
行为:50个输出50,52个输出55,50.25个输出50
函数roundUpToAny($ n,$ x = 5){
return(round($ n )%$ x === 0)? ($ n):round(($ n + $ x / 2)/ $ x)* $ x;
}
3。四舍五入到整数,然后到5的最接近倍数
行为:50个输出50,52个输出55,50.25个输出55
函数roundUpToAny($ n,$ x = 5){
return(ceil($ n)%$ x == = 0)? ceil($ n):round(($ n + $ x / 2)/ $ x)* $ x;
}
1. Round to the next multiple of 5, exclude the current number
Behaviour: 50 outputs 55, 52 outputs 55
function roundUpToAny($n,$x=5) {
return round(($n+$x/2)/$x)*$x;
}
2. Round to the nearest multiple of 5, include the current number
Behaviour: 50 outputs 50, 52 outputs 55, 50.25 outputs 50
function roundUpToAny($n,$x=5) {
return (round($n)%$x === 0) ? round($n) : round(($n+$x/2)/$x)*$x;
}
3. Round up to an integer, then to the nearest multiple of 5
function roundUpToAny($n,$x=5) {
return (ceil($n)%$x === 0) ? ceil($n) : round(($n+$x/2)/$x)*$x;
}
Add comment