我想将 26.955 四舍五入为 26.96
我的意思是,如果我有数字 0-4,我想将它们四舍五入 如果他们是 5-9 我想把他们四舍五入
我考虑过 parseFloat(number).toFixed(2)
但是它返回 26.95,我需要 26.96
我使用了 Math.round
但这也不起作用,我看到了 Math.round10
但它告诉我这个函数不存在所以我不知道如何解决我的问题。
更新:我并不总是在点后有 3 位数字,我有比 26.956736489 更多的数字
你提到的关于 .fixed(2) 的重复讨论我是说它不能工作一半甚至 它不是重复的
最佳答案
尝试
Math.round(29.955*100)/100; //29.96
或者很酷的函数式方法
function round(n) {
n = Math.pow(10,n);
return function(num) {
return Math.round(num*n)/n;
}
}
var round2 = round(2);
var num = 29.9555;
console.log(round2(num));
关于javascript - 点后如何在 javascript 无限数字中做四舍五入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26806234/