如何做到一半甚至在javascript无限数字后点?

时间:2014-11-07 16:57:56

标签: javascript rounding

我希望将26.955舍入到26.96

我的意思是如果我有数字0-4我想要将它们围绕下来 如果他们是5-9我想要围捕他们

我考虑parseFloat(number).toFixed(2)然而它回报我26.95,我需要26.96 我使用了Math.round,但这也没有用,我看到了Math.round10,但它告诉我这个功能不存在,所以我不知道如何解决我的问题。

更新:在我拥有的数字超过26.956736489之后,我总是不会有3位数

你提到的重复谈论.fixed(2)我说它不起作用的一半甚至它不是重复的

2 个答案:

答案 0 :(得分: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));

答案 1 :(得分:-1)

使用它:

var num=26.955
Math.round(num* 100) / 100;
相关问题