Closed Bug 226642 Opened 21 years ago Closed 21 years ago

parseInt returns wrong value for a small floating point argument

Categories

(Rhino Graveyard :: Core, defect)

x86
Windows XP
defect
Not set
normal

Tracking

(Not tracked)

VERIFIED INVALID

People

(Reporter: edwin, Assigned: igor)

References

()

Details

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 For very small floating point numbers, parseInt returns the first non-zero digit instead of zero. Example: parseInt(0.0000000333) returns 3. Reproducible: Always Steps to Reproduce: 1. Open the JavaScript console. 2. type "parseInt(0.0000000333)" and press enter. Actual Results: The number "3" appeared. Expected Results: Show a zero. ECMA-262, paragraph 15.1.2.2: 14) If S contains any character that is not a radix-R digit, then let Z be the substring of S consisting of all characters before the first such character; otherwise, let Z be S.
I will look at it
Assignee: nboyd → igor
Marking as invalid: as required by ECMAScript, v3, 15.1.2.2, Rhino first converts its argument to string which for number 0.0000000333 gives "3.33e-8". Then the string is correctly interpreted as number 3. Note that parseInt("0.0000000333") gives 0 as expected. Here is full extract from the standard: 15.1.2.2 parseInt (string , radix) The parseInt function produces an integer value dictated by interpretation of the contents of the string argument according to the specified radix. Leading whitespace in the 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. Any radix-16 number may also optionally begin with the character pairs 0x or 0X. When the parseInt function is called, the following steps are taken: 1. Call ToString(string). 2. Let S be a newly created substring of Result(1) consisting of the first character that is not a StrWhiteSpaceChar and all characters following that character. (In other words, remove leading white space.) ...
Status: NEW → RESOLVED
Closed: 21 years ago
Resolution: --- → INVALID
So, it's a bug in the specification? The current situation leads to unpredictable and unexpected results. I'm sure more than 99% of the JavaScript developers don't know about this strange behavior of parseInt().
To Edwin Martin: Given that parseInt in MSIE behaves exactly as it does in SpiderMonkey (Mozilla JS engine) and Rhino, probably people are well aware about it. Plus the specification is explicit that the argument will be converted to string. If you need to account for possible numbers then a helper function would do the job: function myParseInt(x) { if (typeof x == "number") return Math.round(x); return parseInt(x); }
Verified Invalid - parseInt() takes a string, not a number. The same issue came up in bug 206511, "parseInt returns wrong value on very small numbers" Remember that JavaScript does implicit type conversions. For example, alert(5) does not result in an error - the 5 is converted to "5" implicitly, since alert() takes a string. The same is true of parseInt(). Any number such as 0.0000000333 will be converted to a string before the function processes it. The function is designed to find integers inside strings. From http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/fcns.html#1008379: The syntax of parseInt is parseInt(str [, radix]) parseInt parses its first argument, the string str, and attempts to return an integer of the specified radix (base), indicated by the second, optional argument, radix. For example, a radix of ten indicates to convert to a decimal number, eight octal, sixteen hexadecimal, and so on. For radixes above ten, the letters of the alphabet indicate numerals greater than nine. For example, for hexadecimal numbers (base 16), A through F are used. If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. If the first character cannot be converted to a number in the specified radix, it returns "NaN." The parseInt function truncates the string to integer values.
Status: RESOLVED → VERIFIED
Trageting as resolved against 1.5R5
Target Milestone: --- → 1.5R5
You need to log in before you can comment on or make changes to this bug.