0

While adding 2 numbers I am getting a lengthy decimal part

Pasting the code below.

Number(200.59) + Number(100) = 300.59000000000003 //Expected result 300.59

Kapil gopinath
  • 1,053
  • 1
  • 8
  • 18
  • 4
    that's how floating points will work , use `toFixed()` method – Ramanlfc Feb 29 '16 at 13:31
  • 1
    and also visit [this link](http://stackoverflow.com/questions/3439040/why-does-adding-two-decimals-in-javascript-produce-a-wrong-result), you will find your answer there. – LightNight Feb 29 '16 at 13:32

1 Answers1

0

You could round the number to keep only 2 decimals:

var input = 300.59000000000003
var result = (Math.round(input*100)/100);
alert(result);
trincot
  • 317,000
  • 35
  • 244
  • 286
vijay
  • 186
  • 2
  • 12