ParseFloat function in JavaScript

Aarti Jain picture Aarti Jain · Oct 6, 2008 · Viewed 17.6k times · Source

When I am adding two textbox values that are 1.001 and 0.001 and then I do a parseFloat I get 1.0019999999. I want it 1.002 . Can you help me?

Answer

17 of 26 picture 17 of 26 · Oct 6, 2008

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.

The parameter (3 in this case) is the number of digits to show after the decimal point