Closed
Bug 416257
Opened 16 years ago
Closed 16 years ago
parseInt breaks if the string to parse is 08 or 09
Categories
(Core :: JavaScript Engine, defect)
Tracking
()
RESOLVED
INVALID
People
(Reporter: vodka_carambar, Unassigned)
Details
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11 Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11 parseInt seems to be unable to parse certain strings. In particular strings than begin with a series of 0's and end in the numbers 8 or 9. If it attempts to parse 01 through 07 it's fine. When it tries to parse 08 or 09 it returns 0. Reproducible: Always Steps to Reproduce: This is a simple HTML page that can create the problem. Click the Test button. View the page in Firefox and it can re-create the error. ------------------------------- <html> <head> <script> <!-- function TestParseInt() { alert(parseInt("00")); alert(parseInt("01")); alert(parseInt("02")); alert(parseInt("03")); alert(parseInt("04")); alert(parseInt("05")); alert(parseInt("06")); alert(parseInt("07")); alert(parseInt("08")); alert(parseInt("09")); alert(parseInt("10")); alert(parseInt("11")); } --> </script> </head> <body> <input type='button' onclick='TestParseInt();' value='Test' /> </body> </html> Actual Results: Upon clicking on the Test button I got the following responses: 0 1 2 3 4 5 6 7 0 0 10 11 Expected Results: 0 1 2 3 4 5 6 7 8 9 10 11 I first encountered this bug about 3 weeks ago. In that instance the numbers being parsed was 000008 in an increment stage. Every time it would reach 000008 the counter would reset. I changed my code so this wouldn't be an issue but now it's cropped up in a different area.
Comment 1•16 years ago
|
||
Sounds like it's parsing it as an octal number. What do you get for "010"? Octal would say 8. This is not a bug, it's what the spec says for parseInt(). Strip leading zeroes to get the numbers to parse correctly, or use parseInt("08", 10) to force a decimal interpretation.
Status: UNCONFIRMED → RESOLVED
Closed: 16 years ago
Resolution: --- → INVALID
Comment 2•16 years ago
|
||
See spec of parseInt. (radix==second parameter of parseInt==base number).
> http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Functions:parseInt
> If the radix is not specified or is specified as 0, JavaScript assumes the following:
> * If the input string begins with "0x", the radix is 16 (hexadecimal).
> * If the input string begins with "0", the radix is eight (octal).
> This feature is deprecated.
> * If the input string begins with any other value, the radix is 10 (decimal).
As jag says, above is "Design" & SPEC.
You'd better to specify Base Number explicitly always.
By the way, in contrast to parseInt(), "var d=09;" sets decimal 9 in d, instead of saying "09" is invalid Octal Number. This is also design of JavaScript.
Confirmed 010 returns the value 8. Thank you for the response. :)
You need to log in
before you can comment on or make changes to this bug.
Description
•