tags:

views:

155

answers:

3

How to parseInt "09" into 9 ?

+7  A: 

include the radix:

parseInt("09", 10);
Gabe Moothart
Thanks for the answer dude.
jessegavin
A: 

parseInt("09", 10);

or

parseInt(parseFloat("09"));

JonH
+1  A: 
parseInt("09",10);

returns 9 here.

It is odd.

alert(parseInt("09")); // shows 9. (tested with Opera 10)
JCasso
He's asking for JS
JonH
@JonH: Right. Thanks for warning. Since he wrote string instead of String I was mistaken.
JCasso
Depending on the browser and version parseInt("09") can return 0.It is a bug.
JonH
@JonH: can you please check this: http://www.w3schools.com/jsref/jsref_parseInt.asp document.write(parseInt("010") also displays 10 here.
JCasso
@JonH: It's not actually a bug. ECMAScript allows implementations to treat numbers with leading zeros as octal. Some implementations do, and some don't.
Matthew Crumley