-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
  • 1,819
  • 2
  • 28
  • 49

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

None
  • 1
  • 30
  • 155
  • 213
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
  • 25,896
  • 16
  • 64
  • 96
1

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

Alfabravo
  • 7,354
  • 6
  • 45
  • 79
0

specify the base

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