Forums > Javascript

parseInt


August 31, 2011 | 8:32 pm

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

August 31, 2011 | 9:23 pm

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

March 29, 2015 | 4:25 am

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.

March 30, 2015 | 5:12 am

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?

Viewing 4 posts - 1 through 4 (of 4 total)

Forums > Javascript