parseFloat( ) Global Function | Flash 5 |
extract a floating-point number from a string |
The string from which a floating-point number is to be extracted.
The extracted floating-point number or, if extraction was unsuccessful, the special numeric value NaN.
The parseFloat( ) function converts a string to a floating-point number (a number with a fractional portion). It returns a useful value only for strings that contain a valid string representation of a floating-point number; otherwise, NaN is returned. The string must be of the following form:
A valid starting character. Valid characters are +, -, 0 through 9, e, E, and ., although optional leading whitespace is acceptable (and ignored).
Optional sign indicator (+ or -).
At least one digit from 0-9 and an optional decimal point.
Optional exponent starting with e or E followed by integer exponent, which can itself include an optional sign indicator (+ or -) — for example, "10e-3".
Trailing characters that cannot be parsed as part of the preceding numeric format are ignored but do not necessarily cause NaN to be returned. The number derived from the supplied string starts with the first nonblank character in the string and ends with the character before the first character that is not a legitimate character.
Because user-input data entered into text fields always belongs to the string datatype, we often use parseFloat( ) to extract numeric data from user-supplied text. Note that parseFloat( ) can extract numbers from strings that contain both numbers and nonnumeric characters, whereas Number( ) cannot.
parseFloat("14.5 apples"); // Yields 14.5 parseFloat(".123"); // Yields 0.123 var x = "15b, 4, 23, 9"; parseFloat(x); // Yields 15
isNaN( ), NaN, Number( ); Section 3.4.2; Section 4.2