parseInt

ytsek's icon

Hi,

I don't get it. I got the following script:
test="05A";
post ("test is ");
post (test,"n");
post ( parseInt(test.slice(0,2)), "n");
test="08A";
post ("test is ");
post (test,"n");
post ( parseInt(test.slice(0,2)), "n");

This gives me the following output:
js: test is 05A
js: 5
js: test is 08A
js: 0

Why doesn't the second output give me 8?

Cheers

ytsek's icon

Never mind. This:
post ( parseInt(test,10), "n");
works

Denis's icon

I just had the same problem:
parseInt('01'), parseInt('02'), etc. give properly 1, 2, etc.
until parseInt('08') and parseInt('09') that give 0.

my turnaround is to specify the radix, like parseInt('08',10) and parseInt('09',10); then it works.
I find it confusing but the default radix depends on the input, see
http://www.w3schools.com/jsref/jsref_parseint.asp definition and usage.

Peter Castine's icon

Starting the text representation of an integer with a spurious zero is, by longstanding convention, a flag to interpret the integer as an octal representation. Since the digits '8' and '9' aren't used in octal, the parser is likely to get confused.

What happens if you drop the leading zero?