Microsoft KB Archive/191434

= PRB: Parseint Returns 0 for Some Strings with Leading Zeros =

Article ID: 191434

Article Last Modified on 3/3/2005

-

APPLIES TO


 * Microsoft JScript 3.0
 * Microsoft Internet Explorer 4.0 128-Bit Edition
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 4.01 Service Pack 1

-



This article was previously published under Q191434



SYMPTOMS
When a string value that contains leading zeros is passed to the parseInt JScript function, parseInt will return 0 for "08" and "09".



CAUSE
In JavaScript placing a leading 0 in front of a number designates that number as octal. In octal, there is no 08 or 09. Therefore, these numbers are interpreted as 0.



RESOLUTION
You must remove the leading zeros before calling parseInt. Here is the code for the workaround: function parseVal(val) {  while (val.charAt(0) == '0') val = val.substring(1, val.length);

return val; }



STATUS
This behavior is by design.



Steps to Reproduce Behavior
The following Web page demonstrates this problem. This Web page contains a text box. When focus is shifted away from the edit box by pressing the TAB key, the parseInt function is called with the value that was entered into the text box. The value returned from parseInt is then displayed in an alert box. If you enter "08" or "09" into the text box, parseInt returns a value of 0.   Problem with parseInt     var val = parseInt(TXT1.value); alert(val);   </HTML> The following JScript code demonstrates the workaround for this problem. <HTML>  THEAD</TITLE>

<SCRIPT LANGUAGE="JScript"> function parseVal(val) {        while (val.charAt(0) == '0') val = val.substring(1, val.length);

return val; }  </SCRIPT> </HEAD>  <INPUT TYPE=TEXT name="TXT1"> <SCRIPT FOR="TXT1" EVENT=onchange> var val = parseVal(TXT1.value); alert(val); </SCRIPT> </BODY> </HTML>

Keywords: kbscript kbprb kbpending KB191434

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.