0

为什么尝试将 2.05 与 1.01 相加会返回此值?

console.log(2.05+1.01); // -> 3.0599999999999996?

使用 toFixed() 是唯一的方法吗?

console.log((2.05+1.01).toFixed(2))

我怎样才能避免这种行为,所以它不会让我在我的代码的内部计算中感到惊讶。

4

1 回答 1

1

This is because javascript's floating point precision only goes up to 14 places. The only real ways around it are multiplying each number by a high number or doing what you said.

于 2015-09-02T00:59:42.623 回答