parseFloat (JavaScript)
Parses a string and returns a floating point number.
Defined in
Top-level functions (JavaScript)Syntax
parseFloat(string)
Parameter | Description |
---|---|
string |
A string that represents a floating point number in fixed or exponential notation. Truncation occurs at a character that is not correct floating point notation. Leading and trailing spaces are legal. |
Usage
If the first character ofstring
is
not legal for a floating point number, the result is the value NaN.
See isNaN (JavaScript).Examples
This example creates floating point numbers and tests them to make sure they are valid numbers.function p(stuff) {
print("<<<" + stuff + ">>>");
}
var x = parseFloat("3.14");
if (isNaN(x))
p("x is not a number");
else
p("x = " + x);
// <<<x = 3.14>>>
var x = parseFloat("314e-2");
if (isNaN(x))
p("x is not a number");
else
p("x = " + x);
// <<<x = 3.14>>>
var x = parseFloat("0.0314e+2");
if (isNaN(x))
p("x is not a number");
else
p("x = " + x);
// <<<x = 3.14>>>
var x = parseFloat("f0.0314e+2");
if (isNaN(x))
p("x is not a number");
else
p("x = " + x);
// <<<x is not a number>>>