-2

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?

Community
  • 1
  • 1
Chris P
  • 2,059
  • 4
  • 34
  • 68

4 Answers4

7

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

2

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"
cambraca
  • 27,014
  • 16
  • 68
  • 99
1

Parseint should use radix parameter: parseint (value, radix). In your case, radix is 10. Otherwise, it will take it as octal.

Alfabravo
  • 7,493
  • 6
  • 46
  • 82
0

specify the base

parseInt(some_id_value,10);
Deept Raghav
  • 1,430
  • 13
  • 14