|
-
Oct 18th, 2003, 03:01 PM
#1
Thread Starter
Hyperactive Member
[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?
-
Oct 20th, 2003, 09:37 AM
#2
Fanatic Member
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
-
Oct 20th, 2003, 12:07 PM
#3
Thread Starter
Hyperactive Member
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.
-
Oct 21st, 2003, 05:06 AM
#4
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.
-
Oct 21st, 2003, 05:07 AM
#5
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.
-
Oct 21st, 2003, 05:09 AM
#6
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.
-
Oct 21st, 2003, 09:21 AM
#7
Thread Starter
Hyperactive Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|