PRB: Parseint Returns 0 for Some Strings with Leading Zeros (191434)
The information in this article applies to:
- Microsoft JScript 3.0
- Microsoft Internet Explorer (Programming) 4.0
- Microsoft Internet Explorer (Programming) 4.01
- Microsoft Internet Explorer (Programming) 4.01 SP1
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.
Modification Type: | Minor | Last Reviewed: | 3/3/2005 |
---|
Keywords: | kbpending kbprb kbScript KB191434 |
---|
|