Results 1 to 7 of 7

Thread: [JavaScript] parseInt BUG? "08","09"

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350

    Smile [JavaScript] parseInt BUG? "08","09"

    If I try this:
    Code:
    <script>
    for (i=0;i<10;i++)
     document.write(parseInt("0"+i.toString()) + " ");
    </script>
    I get 0 1 2 3 4 5 6 7 0 0 ... am I missing something?

  2. #2
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727
    just write this

    Code:
    for (i=0;i<10;i++)
     document.write(0 + i + " ");
    Not sure what was going wrong in yours, m,aybe something to do with adding a string to an integer

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350
    Well I just wanted to verify user input. I have 'Hours':'Minutes' text boxes and I think you normally enter value like "05" mins rather than "5".
    As long as I need to do some mathematical operations with this value, I tried to convert it to number, using parseInt. And I found, that for values "08" and "09" it returns 0.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    A string starting with "0" is treated as an octal number. "08" starts with "0". So parseInt takes all characters up to the first invalid one (that is not in the radix's digit set) and parses them. Octal digits have the set "01234567", so the "8" is invalid. The starting "0" is stripped too, which leaves "" as the string to be parsed. Which results in 0.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I wrote a function parseDecimal, hold on a minute.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Wrote this when I had the same problem.
    Code:
    function isStringNumber(s)
    {
    	var ret = true;
    	var i;
    	for(i=0;i<s.length;++i)
    		ret = ret && (s.charAt(i) == (""+parseInt(s.charAt(i))));
    	return ret;
    }
    
    // like parseInt, but always base 10
    function parseDecimal(s)
    {
    	var out = 0, i;
    	for(i=0;i<s.length;++i)
    	{
    		out *= 10;
    		out += parseInt(s.charAt(i));
    	}
    	return out;
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350

    Talking

    Ok, thank you for replies, I haven't expected octal conversion .

    And I also found the most simple solution... use parseFloat

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