parseInt with leading zero

parseInt with a string that begins by 0 makes the radix be octal and not decimal, there are two solutions:

The parseInt function takes a second argument determining the radix, if nothing is supplied and the string to parse begins by 0, the it is parsed in octal.

alert(parseInt(myStr,10)); //-- Where 10 is the radix
//-- Or
alert(parseFloat(myStr));//-- use parseFloat that parses everything as a decimal

Some browsers interpret a string with a zero before as an octal number when using the parseInt function, for exemple parsetIn("08") will return 0 instead of 8 because it thinks it is parsing an octal value instead of a string.


For a browser safe parseInt function it is better to add the base 10 parameter:


parsetInt("08",10); // will return 8
parseInt("08"); // will return 0;



This is a bug very hard to see, for exemple if you are parsing a date it will work if you are parsing a day before the 8th day of the month, or a month before the month of August, the 8th month