The JavaScript parseFloat Function

The JavaScript parseFloat global function returns the float value of a variable. It parses the string representation of that variable and returns a floating point number.


var float_val = parseFloat(value);


A string representing the value to be parsed

The examples below all return 1.618:


If the variable cannot be converted to a string, the parseFloat() function returns NaN:

parseFloat({foo: "bar"}); // NaN


The parseFloat function parses a string and returns a floating point number. It walks through the characters of the string and doesn't stop parsing until it encounters a character other than a sign (+ or -), a numeral (0–9), a decimal point or an exponent. The value parsed up to that point is then converted to a floating point number. Leading and trailing spaces are allowed.

As with the parseInt() function, the result of the parseFloat() function depends on the leftmost characters of the string.

If the first character of the string cannot be converted to a number, parseFloat returns NaN.

The isNaN() function can be used to determine if the result of the parseFloat() function is NaN.

See also

If you see a typo, want to make a suggestion or have anything in particular you'd like to know more about, please drop us an e-mail at hello at diveintojavascript dot com.

Copyright © 2010-2013 Dive Into JavaScript