Convert String to Integer Method
The Convert String to Integer method converts an alphanumeric string to an integer. It returns an integer. If it cannot convert the value that the string argument contains to a number, then it returns the following value:
NaN
For more information, see NaN Numbers.
Format
parseInt(string [,radix])
The following table describes the arguments for the Convert String to Integer method.
Argument | Description |
---|---|
string |
The string that this method converts. |
radix |
The base of the number system that this method uses in the return value. For example, if you set the radix argument to 8, then it returns the value as an octal number. |
Usage
If you do not specify the radix argument or if the value that the radix argument contains is zero, then the Convert String to Integer method uses a value of 10 for the radix unless the value that the string argument contains begins with one of the following values:
The character pairs 0x or 0X. It uses a value of 16 for the radix.
A zero and a valid octal digit. It uses a value of 8 for the radix. Any number zero through seven is a valid octal digit.
This method handles the string in the same way as the Convert String to Floating-Point Number method. For more information, see Convert String to Floating-Point Number Method.
Example
The following example returns a result of negative 234:
var num = parseInt(" -234.37 profit");