parseInt is not octal/binary by default #23

Closed
joaomoreno opened this Issue May 9, 2011 · 2 comments

Comments

Projects
None yet
3 participants
@joaomoreno

In section Always Specify the Second ‘radix’ Parameter When Using .parseInt(), it is said that

When parsing a string to an integer, it is considered good practice to specify the second 'radix' parameter - which determines to what base the string should be converted to. It is, by default, octal/binary - so you will run into some problems.

This is actually not true. ECMA-262 specifies that

The parseInt function produces an integer value dictated by interpretation of the contents of the string argument according to the specified radix. Leading white space in string is ignored. If radix is undefined or 0, it is assumed to be 10 except when the number begins with the character pairs 0x or 0X, in which case a radix of 16 is assumed. If radix is 16, number may also optionally begin with the character pairs 0x or 0X.

I do not, nevertheless, challenge the importance of always specifying the radix.

@taitems

This comment has been minimized.

Show comment
Hide comment
@taitems

taitems May 11, 2011

Owner

Clarified (but simplified) locally pending commit.

Owner

taitems commented May 11, 2011

Clarified (but simplified) locally pending commit.

@taitems taitems closed this May 11, 2011

@krukow

This comment has been minimized.

Show comment
Hide comment
@krukow

krukow Jan 11, 2012

But, later in ES3:

When radix is 0 or undefined and the string's number begins with a 0 digit not followed by an x or X,
then the implementation may, at its discretion, interpret the number either as being octal or as being
decimal. Implementations are encouraged to interpret numbers in this case as being decimal.

So the advice remains valid: always specify radix.

krukow commented Jan 11, 2012

But, later in ES3:

When radix is 0 or undefined and the string's number begins with a 0 digit not followed by an x or X,
then the implementation may, at its discretion, interpret the number either as being octal or as being
decimal. Implementations are encouraged to interpret numbers in this case as being decimal.

So the advice remains valid: always specify radix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment