为什么JavaScript不返回圆的数量
问题描述:
function addTwoDigits(n) {
var result;
result = Math.floor(n/10) + 10*(n/10-Math.floor(n/10));
console.log(result);
return result;
}
addTwoDigits(29);
``` 输出被10.9999999999999999999999999999999 我不知道为什么它不是11根据计算的正常方式,因为它应该已经被四舍五入,但它不。这背后有没有隐藏的计算机科学?
答
把Math.round()
function addTwoDigits(n) {
var result;
result = Math.floor(n/10) + 10*(n/10-Math.floor(n/10));
console.log(Math.round(result));
return result;
}
addTwoDigits(29);
希望这有助于。
+0
tks为节省您的时间,但我的意思是,我想知道为什么结果是10.99999999。我知道我们可以使用Math.round alreaduy:D – NewbieCodeGuy
检查此[柱](http://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-in-javascript)。希望这可以帮助。 – jmj