SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot HotDog's Avatar
    Join Date
    May 2000
    Location
    Salt Lake City, UT
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been programming an Intranet site for the company I work for and have come across a very interesting javascript bug. Most people will never run into this problem, but if you do, here is the bug and a work-around I've come up with.

    The bug is this:

    If you try and parseInt a string number of either 08 or 09, it will return a value of 0 instead of 8 or 9. If you try any other number (01 - 07), it will return the proper number (e.g. parseInt("01") will return 1, not 0).

    But the thing is, is that if you just have "8" or "9" without the zeros, it will parse the string correctly.

    Try it out for yourself...

    Code:
    <html>
    <head>
    <script language="javascript1.5" type="text/javascript">
    var calendar_month = "08" //change this number to 01-07 or to 8 or 9 to work, 08 & 09 do not
    var test = parseInt(calendar_month)
    alert("parsed date is " + test + " and the Non-parsed date is " + calendar_month);
    </script>
    </head>
    <body>
    
    </body>
    </html>
    In this example, I was trying to parse a user inputed month so I could compare it to the current date. The month date is recognized as a string so I had to convert it to a number for my function to work. The work around I found is to put an if...else function to check to see if the string was a 08 or 09 and if so, to change the variable that held month value to just 8 or 9.

    Code:
    <script language="javascript1.5" type="text/javascript">
    var calendar_month = "08" //change this number to 01-07 to work, 08 & 09 do not
    if (calendar_month == "08") calendar_month = "8";
    if (calendar_month == "09") calendar_month = "9";
    var test = parseInt(calendar_month)
    alert("parsed date is " + test + " and the Non-parsed date is " + calendar_month);
    </script>
    This works all fine and dandy. The only problem with this work-around is that this bugs "works???" no matter how many zeros are in front of an 8 or 9, (0000008 will bug out the same as 08). So if you have a situation where you may have more then 1 zero in front, and you don't know how many, you will have to build a more complex checker.

    As I said, 99% of you will never need to know this, but if you ever do, here it is.

    I don't know if there are any other combinations that causes this bug. I only know of the 08 and 09. I checked it with IE 5.5, NS4.7 and NS6. All reproduce the same bug every time.
    Joe Eliason
    Just a dog learnin' PHP from cat.

  2. #2
    SitePoint Zealot HotDog's Avatar
    Join Date
    May 2000
    Location
    Salt Lake City, UT
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've done some dinking around with this and have found that if a zero is in front of any number other than 01-07, it will return a false value. For example, if you have "017", it will return 15, not 17. Also, if a zero is in front of any number starting with an 8 or 9, it will return a value of zero (08054 will return 0).

    Only 01, 02, 03, 04, 05, 06 or 07 will return the number without the zero properly. All other combinations will return a bogus value.

    But as I said, it isn't too often that you'll need to parse a stringged number that starts with a zero.

    I also do not know if it is designed to work this way or if it truly is a bug.
    Joe Eliason
    Just a dog learnin' PHP from cat.

  3. #3
    I believe you have my stapler. scrubz's Avatar
    Join Date
    Feb 2001
    Location
    Van down by the river
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This actually isn't a bug, it's the way parseInt works. Let's say n = 08. Since n begins with a zero, parseInt parses the the rest of n as an octal number (octals being based on 8, that's why numbers 0-7 would work correctly).

    One workaround is to specify the radix argument, which specifies the base of the number you're parsing. Like this:

    parseInt(n,16)

    So 08 and 09 would now work. BUT, 11 and 12 won't work correctly.

    With all of that said, why not just use parseFloat? It will work for all of your months 1-12.

  4. #4
    SitePoint Zealot HotDog's Avatar
    Join Date
    May 2000
    Location
    Salt Lake City, UT
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are so very correct. And if I'd have looked at my "JavaScript for Dummies" book I'd have seen that, well sort of. I did re-read up on the parseFloat and parseInt (pages 257-259) part after your post, and it really didn't say anything about the leading zeros issue. But it does explain the "radix" issue that parseInt has and parseFloat does not. I knew that you could parse in different redixes, I just didn't think that having a leading zero made any difference. Soooo, taking my new found knowledge, I did the following:

    Code:
    var test = parseInt(calendar_month,10)
    Works peachy. So does substituting parseInt with parseFloat. Thanks for the pointer.
    Joe Eliason
    Just a dog learnin' PHP from cat.

  5. #5
    I believe you have my stapler. scrubz's Avatar
    Join Date
    Feb 2001
    Location
    Van down by the river
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem, good to hear it. Strangely enough, I did run across this issue a while back, and it had me slightly baffled for a while. I love surprises.

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not having tested it, I don't know if this would be faster or slower, but another option would be to strip out all the zeros with a regular expression:
    Code:
    calendar_month = month_string.replace(/^0*/,'');
    This will remove all, if any, zeros, but only at the beginning of the string.
    <<< johnny chidiac
    johnnydaboy@mac.com


Bookmarks

Posting Permissions

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