0

I have the following simple math operation.

var a = 1.12345678 + 1;
console.log(a);

which results in

2.1234567799999997

why?

I expect the result to be

2.12345678
Aley
  • 8,540
  • 7
  • 43
  • 56
  • 1
    read this http://stackoverflow.com/questions/5037839/avoiding-problems-with-javascripts-weird-decimal-calculations – Paul Fitzgerald Dec 17 '15 at 23:26
  • 5
    Possible duplicate of [How to deal with floating point number precision in JavaScript?](http://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript) – Alex Dec 17 '15 at 23:28

2 Answers2

0

I would just recommend using +a.toFixed(8) JavaScript uses floating point precision, which is never 100% accurate

Scott Schwalbe
  • 430
  • 2
  • 4
0

Simple solution:

Math.round(( OPERATION ) * 1e12) / 1e12

Supports all browsers. toFixed() causes strange things to happen on IE.

Jerrybibo
  • 1,315
  • 1
  • 21
  • 27