parseFloat( ) Global Function

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
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:

  • 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.

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



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net