How to covert value to int with radix
Description
parseInt()
provides a second argument: the radix.
Example
For hexadecimal format, the radix is 16.
console.log(parseInt("0xAE", 16));
The code above generates the following result.
The leading "0x" is not necessary by providing the hexadecimal radix:
console.log(parseInt("AE", 16));
var num2 = parseInt("AF"); //NaN
console.log(num2);
The code above generates the following result.
Most of the time you'll be parsing decimal numbers, so it's good to always include 10 as the second argument.
var num1 = parseInt("10", 2); //2 - parsed as binary
console.log(num1);/*from w ww . j ava 2 s . c o m*/
var num2 = parseInt("10", 8); //8 - parsed as octal
console.log(num2);
var num3 = parseInt("10", 10); //10 - parsed as decimal
console.log(num3);
var num4 = parseInt("10", 16); //16 - parsed as hexadecimal
console.log(num4);