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);
The examples below all return 1.618:
parseFloat("1.6180");
parseFloat("0.016180E+2");If the variable cannot be converted to a string, the parseFloat() function returns NaN:
parseFloat({foo: "bar"}); // NaNThe 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.
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-2011 Dive Into JavaScript