Table of Contents

parseInt( ) Global Function Flash 5

extract an integer from a string, or convert numbers to base-10.
parseInt(stringExpression)parseInt(stringExpression, radix)

Arguments

stringExpression

The string from which an integer is to be extracted.

radix

An optional integer, between 2 and 36, specifying the base (or radix) of the integer contained in stringExpression. If not specified, the default radix depends on the contents of stringExpression (as described in the following Description).

Returns

The extracted integer value, as a base-10 number regardless of the original radix; or, if extraction was unsuccessful, the special numeric value NaN.

Description

The parseInt( ) function converts a string expression to an integer. It works only with strings that contain a valid string representation of an integer using the specified radix. The string must be of the following form:

Trailing characters that cannot be parsed as part of the preceding numeric form 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 digit in the supplied radix. For example, in the base-10 system, the letter F is not a valid digit, so the following expression yields the number 2:

parseInt("2F", 10);

But in the base-16 system (hexadecimal), the characters A, B, C, D, E, and F are valid numerals, so the following expression yields the number 47:

parseInt("2F", 16);  // 2F in hexadecimal is 47 in base-10

The radix argument of parseInt( ) specifies the base of the number as it exists in the string. In other words, the radix argument says to the interpreter, "Treat this string as a base-16 (hexadecimal) number," or, "Treat this string as a base-2 (binary) number."

The parseInt( ) function also interprets the prefix 0x to indicate a hexadecimal (a.k.a. hex) number, as if a radix of 16 was specified, and a leading 0 (zero) to indicate an octal number, as if a radix of 8 was specified:

parseInt("0xFF");    // Parsed as hex, (implicit radix of 16) yields 255
parseInt("FF", 16);  // Parsed as hex, (explicit radix of 16) yields 255
parseInt("FF", 10);  // Parsed as decimal, (explicit radix of 10) yields NaN
parseInt("0377");    // Parsed as octal, (implicit radix of 8) yields 255
                     //  because (3 * 64) + (7 * 8) + (7 * 1) = 255
parseInt("377", 8);  // Parsed as octal (explicit radix of 8), yields 255

An explicit radix overrides any implicit radix:

parseInt ("0xFF", 10)   // Parsed as decimal, yields 0
parseInt ("0x15", 10)   // Parsed as decimal, yields 0 (not 15, and not 21)
parseInt ("0377", 10)   // Parsed as decimal, yields 377

Note that the parseInt( ) function extracts integer values only, unlike parseFloat( ), which can also extract fractional values but can be used only with base-10 numbers.

Example

We use parseInt( ) primarily to extract integers from a string that contains both numbers and text or to remove the decimal place from a number (similar to Math.floor( )):

parseInt("Wow, 20 people were there");    // Yields NaN
parseInt("20 people were there");         // Yields 20
parseInt("1001", 2);                      // Yields 9 (1001 evaluated in binary)
parseInt(1.5);                            // Yields 1 (the number 1.5 is converted 
                                          // to the string "1.5" before the parseInt 
                                          // operation proceeds)

See Also

Math.floor( ), NaN, Number( ), parseFloat( ); Section 3.4.2; Section 4.2


Table of Contents