Table of Contents

parseFloat( ) Global Function Flash 5

extract a floating-point number from a string
parseFloat(stringExpression)

Arguments

stringExpression

The string from which a floating-point number is to be extracted.

Returns

The extracted floating-point number or, if extraction was unsuccessful, the special numeric value NaN.

Description

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:

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.

Usage

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.

Examples

parseFloat("14.5 apples");    // Yields 14.5
parseFloat(".123");           // Yields 0.123
var x = "15b, 4, 23, 9";
parseFloat(x);                // Yields 15

See Also

isNaN( ), NaN, Number( ); Section 3.4.2; Section 4.2


Table of Contents