If I try this:I get 0 1 2 3 4 5 6 7 0 0 ... am I missing something? :eek:Code:<script>
for (i=0;i<10;i++)
document.write(parseInt("0"+i.toString()) + " ");
</script>
Printable View
If I try this:I get 0 1 2 3 4 5 6 7 0 0 ... am I missing something? :eek:Code:<script>
for (i=0;i<10;i++)
document.write(parseInt("0"+i.toString()) + " ");
</script>
just write this
Not sure what was going wrong in yours, m,aybe something to do with adding a string to an integerCode:for (i=0;i<10;i++)
document.write(0 + i + " ");
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.
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.
I wrote a function parseDecimal, hold on a minute.
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;
}
Ok, thank you for replies, I haven't expected octal conversion :).
And I also found the most simple solution... use parseFloat ;)