When i am adding two text boxes values that are 1.oo1 and 0.001 and then i do a parsefloat i get 1.0019999999. I want it 1.002 . Can u help me?
+6
A:
This is a known problem: see accuraty problem and the minimisation of the accuracy problem: minimisation
Burkhard
2008-10-06 11:09:46
+7
A:
0.002 cannot be accurately represented as a base 2 number. Similar to the way that 1/3 can't be represented in base 10.
1/3 = 0.33333... recuring. To represent the number accurately in base 10, you would need an infinite number of decimal digits.
0.002 is a number that can be accurately represented in base 10 (as we see here), but not in base 2, as used by computers. To represent this number accurately, would take an infinite number of binary digits.
SpoonMeiser
2008-10-06 11:21:21
+10
A:
The Javascript Number class has a ToFixed() function that will get you what you want.
So you could do parseFloat("1.0019999").ToFixed(3) and that would give you "1.002".
17 of 26
2008-10-06 12:15:39
Thank you, 17 of 26. I didn't know about the toFixed() function.
pmg
2008-10-06 12:32:35
The toFixed() method returns a string(instead of number) representation of a number in fixed-point notation. So we should be careful while dealing with toFixed() method.
pramodc84
2010-01-25 11:41:04