Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  8] [ 4]  / answers: 1 / hits: 73768  / 13 Years ago, fri, january 6, 2012, 12:00:00

Javascript's parseInt function does not seem to completely work.



parseInt(01) returns 1
parseInt(02) returns 2
parseInt(03) returns 3
parseInt(04) returns 4
parseInt(05) returns 5
parseInt(06) returns 6
parseInt(07) returns 7
parseInt(08) returns 0
parseInt(09) returns 0


You can't explain that. Give it a try. (jsFiddle)



Edit Since this question was asked and answered, the feature of defaulting to octal radix has been deprecated. [1] [2]


More From » javascript

 Answers
51

This is because if a number starts with a '0', it's treated as base 8 (octal).



You can force the base by passing the base as the 2nd parameter.



parseInt(09, 10) // 9


According to the docs, the 2nd parameter is optional, but it's not always assumed to be 10, as you can see from your example.


[#88182] Thursday, January 5, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kennedim

Total Points: 699
Total Questions: 85
Total Answers: 105

Location: Saint Helena
Member since Mon, Jan 16, 2023
1 Year ago
;