Parses a string argument and returns an integer of the specified radix or base.
Core function
Syntax
parseInt( string[, radix] )
Parameters
Parameter | Description |
---|---|
string | A string that represents the value you want to parse. |
radix | An integer that represents the radix of the return value. |
Description
parseInt is a top-level function and is not associated with any object.
The parseInt function parses its first argument, a string, and attempts to return an integer of the specified radix (base). For example, a radix of 10 indicates to convert to a decimal number, 8 octal, 16 hexadecimal, and so on. For radixes above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), A through F are used.
If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.
If the radix is not specified or is specified as 0, MetaScript assumes the following:
- If the input string begins with "0x", the radix is 16 (hexadecimal).
- If the input string begins with "0", the radix is eight (octal).
- If the input string begins with any other value, the radix is 10 (decimal).
If the first character cannot be converted to a number, parseInt returns NaN.
For arithmetic purposes, the NaN value is not a number in any radix. You can call the isNaN function to determine if the result of parseInt is NaN. If NaN is passed on to arithmetic operations, the operation results will also be NaN.
Examples
The following examples all return 15:
The following examples all return NaN:
Even though the radix is specified differently, the following examples all return 17 because the input string begins with "0x".