0

I have the following TypeScript function in an Angular app:

countTotal() {
    this.total = this.num1 + this.num2
}

Where num1 value is 110.84 and num2 value is 5.54. I got these values by adding a watch of this.num1 and this.num2 in Chrome debugger respectively. num1 and num2 are of type number.

The result of this addition is 116.38000000000001 instead of 116.38. I need this value to be 116.38. I know that I can round to 2 decimal places but I need to understand why JavaScript is behaving this way.

seedg
  • 21,692
  • 10
  • 42
  • 59

1 Answers1

0

That is not the problem with JavaScript itself, but with the way floating point numbers are implemented in the computer. The representation of floating point numbers in JavaScript follows the format as specified in IEEE-754.

You can learn more about it here https://modernweb.com/what-every-javascript-developer-should-know-about-floating-points/

Yaroslav Bai
  • 1,056
  • 11
  • 14