You are currently offline, waiting for your internet to reconnect

PRB: Parseint Returns 0 for Some Strings with Leading Zeros

This article was previously published under Q191434
This article has been archived. It is offered "as is" and will no longer be updated.
When a string value that contains leading zeros is passed to the parseInt()JScript function, parseInt will return 0 for "08" and "09".
In JavaScript placing a leading 0 in front of a number designates thatnumber as octal. In octal, there is no 08 or 09. Therefore, these numbersare interpreted as 0.
You must remove the leading zeros before calling parseInt. Here is the codefor the workaround:
function parseVal(val){   while (val.charAt(0) == '0')      val = val.substring(1, val.length);   return val;}				
This behavior is by design.
More information

Steps to Reproduce Behavior

The following Web page demonstrates this problem. This Web page contains atext box. When focus is shifted away from the edit box by pressing the TABkey, the parseInt() function is called with the value that was entered intothe text box. The value returned from parseInt() is then displayed in analert box. If you enter "08" or "09" into the text box, parseInt() returnsa value of 0.
<HTML><HEAD>   <TITLE>Problem with parseInt</TITLE></HEAD><BODY>   <INPUT TYPE=TEXT name="TXT1">   <SCRIPT FOR="TXT1" EVENT=onchange>      var val = parseInt(TXT1.value);      alert(val);   </SCRIPT></BODY></HTML>				
The following JScript code demonstrates the workaround for this problem.
<HTML><HEAD>   <TITLE>THEAD</TITLE>   <SCRIPT LANGUAGE="JScript">      function parseVal(val)      {         while (val.charAt(0) == '0')            val = val.substring(1, val.length);         return val;      }   </SCRIPT></HEAD><BODY>   <INPUT TYPE=TEXT name="TXT1">   <SCRIPT FOR="TXT1" EVENT=onchange>      var val = parseVal(TXT1.value);      alert(val);   </SCRIPT></BODY></HTML>				

Article ID: 191434 - Last Review: 10/26/2013 01:43:00 - Revision: 3.0

Microsoft Internet Explorer 4.01 Service Pack 1

  • kbnosurvey kbarchive kbpending kbprb kbscript KB191434