Possible Duplicate:
Workarounds for JavaScript parseInt octal bug
well i have this http://jsfiddle.net/gMDfk/1/ The alert returns 0 The code in jsfiddle works perfect when value is equal to eight or nine.. Wtf is going on here?
Possible Duplicate:
Workarounds for JavaScript parseInt octal bug
well i have this http://jsfiddle.net/gMDfk/1/ The alert returns 0 The code in jsfiddle works perfect when value is equal to eight or nine.. Wtf is going on here?
add a , 10
to parseInt:
parseInt( val, 10 );
which tells JS to treat is as a base-10 number. By default, anything starting with 0
is treated as an octal, base-8 number. Since 09
isn't a valid base-8 number, you'll get 0
Prefixing a number with a 0 means it's interpreted as octal by javascript. Try this:
alert(parseInt("010")); //shows "8"
You can fix it by passing 10
as a second param to parseInt
, this lets it know you want it parsed in decimal.
alert(parseInt("010", 10)); //shows "10"
Parseint should use radix parameter: parseint (value, radix). In your case, radix is 10. Otherwise, it will take it as octal.