我想将数字的小数位四舍五入到下一个 5 的倍数
我已经尝试过了,但它不适用于小数位
const roundOff = (num) =>{
let temp = parseFloat(num.toFixed(2)) // to make it 2 place decimal
return Math.ceil(temp / 5) * 5
}
roundOff(54.5678) // should give 54.60
roundOff(43.738) // should give 43.75
roundOff(89.982) // should give 90.00
roundOff(80.034) // should give 80.05
最佳答案
const roundOff = (num) =>{
return (Math.ceil(num * 20) / 20).toFixed(2)
}
console.log(roundOff(54.5678)) // should give 54.60
console.log(roundOff(43.738)) // should give 43.75
console.log(roundOff(89.982)) // should give 90.00
console.log(roundOff(80.034))
关于javascript - 将数字的小数位四舍五入到 0.05 的下一个倍数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68556022/