Results 1 to 8 of 8

Thread: parseInt('08') returns 0

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769

    Question parseInt('08') returns 0

    Hi,

    I am having a real problem with this. I want to convert a string '08' to an integer 8, but the parseInt function seems to return 0 for this string... It also seems to do the same with '09'.

    All values less than 8 work fine though .. ?

    Does anyone know of a function that converts these strings correctly?

  2. #2
    Hyperactive Member bsw2112's Avatar
    Join Date
    Nov 2001
    Location
    ottawa, canada
    Posts
    292
    you can try using eval() but I am not sure if that is the best
    way but it seems to be working.

    <html>

    <head>
    <title></title>
    </head>

    <body>
    <script>
    var a = '08';
    var b = eval('08');
    b = b * 2
    document.write(b);
    </script>
    </body>
    </html>

    regards

    bsw2112

  3. #3
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    bsw2112 you are correct, infact if you wanted to really test your number you could use parseInt(eval('08')) which will make whatever is in the quotes a number and then force it as an integer.

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769
    Thats all good and well, but why does this happen. It should convert to integer

  5. #5
    New Member
    Join Date
    Jul 2002
    Posts
    14
    Originally posted by cpradio
    bsw2112 you are correct, infact if you wanted to really test your number you could use parseInt(eval('08')) which will make whatever is in the quotes a number and then force it as an integer.

    -Matt
    The problem is because 08 is not a number, and parseInt() returns the first thing it could find that resembles one. 0 is a number to it, but 08 is not, so it returns 0
    -JavaScript Kit- JavaScript tutorial and scripts!
    -CodingForums.com- Web coding and development forums

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769
    I'm confused. If it does not see '08' as a number, why does it see '07' as the number 7?

  7. #7
    New Member
    Join Date
    Jul 2002
    Posts
    14
    Hmmm...if that's the case I have no idea why. My guess is perhaps 07 is a special keyword/identifier. It seems other numbers like 09 also return 0, not 9.
    -JavaScript Kit- JavaScript tutorial and scripts!
    -CodingForums.com- Web coding and development forums

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769
    actually, all the numers '00' -> '07' return their true number eg. 1, 2 etc. It is only '08' and '09' that seem to have a problem with parseInt()

Posting Permissions

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



Click Here to Expand Forum to Full Width