-2
function myFunction() {
    var c = parseFloat("10.81") +parseFloat("10.00");
alert(c);
}

//output is 20.810000000000002 why?

Manish Vij
  • 29
  • 1
  • 5

1 Answers1

0

You can use toFixed to get the precision to two digit floating number. In that case you will get the desired result:

function myFunction() {
  var c = parseFloat("10.81") +parseFloat("10.00");
  alert(c.toFixed(2));
}
//output is 20.810000000000002 why?

myFunction();
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62