[SERVER-20097] parseInt Returning Zero for parseInt('09') and parseInt('08') Created: 24/Aug/15  Updated: 25/Aug/15  Resolved: 25/Aug/15

Status: Closed
Project: Core Server
Component/s: JavaScript
Affects Version/s: 3.0.0
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: STEPHEN J ERSTAD Assignee: Max Hirschhorn
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:

print(parseInt('09'))

Sprint: QuInt 8 08/28/15
Participants:

 Description   

When using parseInt for the strings '09' and '08' zero is incorrectly returned. I haven't been able to reproduce this for other '0*' strings.



 Comments   
Comment by Max Hirschhorn [ 25/Aug/15 ]

The parseInt(string, radix) function takes a radix argument and it is strongly encouraged that one is always specified. A numeric string beginning with a leading zero may be interpreted as octal depending on what JS engine is being used and its version.

With MongoDB 3.0.6 (uses v8),

> parseInt('08')
0
> parseInt('08', 10)
8
> parseInt('0128')
10
> parseInt('0128', 10)
128

With MongoDB 3.1.7 (uses SpiderMonkey, ES5 compliant),

> parseInt('08')
8
> parseInt('08', 10)
8
> parseInt('0128')
128
> parseInt('0128', 10)
128

If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.

See the documentation on parseInt() for more details.

Closing this ticket as "works as designed".

Comment by STEPHEN J ERSTAD [ 24/Aug/15 ]

Note that this works as expected in V8 and I would expect it to mimic the JavaScript API. Not something like the Java integer parsing.

Generated at Thu Feb 08 03:53:08 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.