gpt4 book ai didi

javascript - 将数字的小数位四舍五入到 0.05 的下一个倍数

转载 作者:行者123 更新时间:2023-12-05 00:34:25 29 4
gpt4 key购买 nike

我想将数字的小数位四舍五入到下一个 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com