0

Why is it that trying to add 2.05 with 1.01 returns this value?

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

is using toFixed() the only way around it?

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

How can I avoid this behavior, so it won't get me by surprise in the inner calculations of my code.

warkentien2
  • 969
  • 13
  • 20

1 Answers1

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.

Sheerforce
  • 79
  • 9