parseInt('09') = 0 ?!

# Thread: parseInt('09') = 0 ?!

1. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## parseInt('09') = 0 ?!

In client-side JavaScript, when I do:<BR>alert(&#039;parseInt(09) = &#039; + parseInt(&#039;09&#039;));<BR><BR>I get:<BR>parseInt(09) = 0<BR><BR>***?!

2. Senior Member
Join Date
Dec 1969
Posts
7,686

## RTFM

hehe<BR><BR>JScript <BR><BR>parseInt Method<BR>Returns an integer converted from a string.<BR><BR>parseInt(numString, [radix]) <BR>Arguments<BR>numString <BR>Required. A string to convert into a number. <BR>radix <BR>Optional. A value between 2 and 36 indicating the base of the number contained in numString. If not supplied, strings with a prefix of &#039;0x&#039; are considered hexadecimal and strings with a prefix of &#039;0&#039; are considered octal. All other strings are considered decimal.

3. Senior Member
Join Date
Dec 1969
Posts
342

## RE: parseInt('09') = 0 ?!

If you pass parseInt a number with a 0 in front (as in this case) it thinks it&#039;s an octal number. As there&#039;s no such digit as 9 in octal, it is treated as a zero.<BR><BR>To overcome this, you have to explicitly specify the base (or the "radix" according to MSDN?), as follows:<BR>alert(&#039;parseInt(09,10) = &#039; + parseInt(&#039;09&#039;,10));<BR><BR>HTH

4. Senior Member
Join Date
Dec 1969
Posts
16,931

## Not fair...

...Doug sent me the link and I was coming here to shout RTFM...<BR><BR>:-)<BR><BR>Craig.

5. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## Woohoo!

You are exactly correct and funny, too.<BR><BR>Thanks!<BR><BR>(p.s. Craig figured it out via IM before you did. Naanaanaana.)

6. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## It does. Thanks! (eop)

.

7. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## And, it goes without saying..

.. that it was deserved, too.<BR><BR>I even read (well, looked at) this page:<BR>http://www.devguru.com/Technologies/ecmascript/quickref/parseint.html<BR><BR>Which says the EXACT same thing.<BR><BR>Heh.

8. Senior Member
Join Date
Dec 1969
Posts
7,686

## So a crossposter too!!! <eop>

hehe

9. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## Heh! Not really.

I pointed him (and Bill H) in the direction of the post.<BR><BR>It was THEIR choice to not follow-up on the messageboard.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•