我知道javascript的toFixed()方法将数字转换为字符串,并保留指定数量的小数位,就像下面的代码一样,将小数点后的数字设置为2。
我的问题是:当需要时,这个数字是否会被四舍五入。就像下面的代码中,我要把数字0.075转换成字符串。我认为四舍五入后的结果应该是0.08,因为“5进1”。然而,在第一个代码示例中看起来很奇怪,所以我感到困惑。
var a=0.25*0.3;
var res1 = a.toFixed(2);
console.log(res1);
//Output: a is 0.07 (Is there something wrong?)
var b=0.025*3;
var res2= b.toFixed(2);
console.log(res2);
//Output:b is 0.08
我知道这个问题可以通过使用Math.js来解决。但是,我想知道这个问题的原因是什么?是toFixed()方法的问题还是javascript中的数据类型的问题。
var b=0.025*3;
。 - radulfrb.toFixed(2)
应该返回0.08
而b = 0.0075
? - Nico Haaseb.toFixed(2);
is .08??? huh, I am not seeing that.0075
would be.01
- epascarello