0

Hi I have two variables in JS Like:

var a = 223620.42
var b = 1200.1234

I am using Calculation Like:

var c = parseFloat(a) + parseFloat(b);

So the result should be = 224820.5434

But it returning 224820.54340000002

Please Suggest me what I am doing wrong here. Thanks in advance

jasonscript
  • 6,039
  • 3
  • 28
  • 43
Sunil Rawat
  • 709
  • 10
  • 41

2 Answers2

0

Just Round-off the last Values. See the Code Below:

var a = 223620.42
var b = 1200.1234
var c = parseFloat(a) + parseFloat(b);
var numb = c.toFixed(4);
alert(numb);

hope it will solve you problem.

Mediasoft
  • 271
  • 2
  • 7
  • 18
0

Try Using "toPrecision" method, and specify the desired length of the number.

var a = 223620.42 var b = 1200.1234 var c = parseFloat(a) + parseFloat(b);

c= c.toPrecision(3)

Nishant
  • 1,635
  • 1
  • 11
  • 24
  • it giving result c = 2.2e+5. I want 224820.5434 – Sunil Rawat Jul 20 '15 at 08:44
  • "toPrecision" will yield scientific notation if there are more integer digits in the number than the specified precision. or you can go with "toFixed". Check this http://stackoverflow.com/questions/3337849/difference-between-tofixed-and-toprecision – Nishant Jul 20 '15 at 09:15