Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parseInt("08") and parseInt("09") are zero??? 1

Status
Not open for further replies.

OsakaWebbie

Programmer
Feb 11, 2003
628
JP
I have code that clones a section of the DOM and increments a two-digit number at the end of the IDs, names, etc. It works fine except when it gets to "08" - it seems that parseInt("08") and parseInt("09") are zero rather than what they should be. I thought there was something in the rest of my code that was tripping it up, but I kept removing other stuff, including all my jQuery, incrementing and converting back to strings with padding, and even loops, but it never got well - check out this extremely stupid code:
Code:
<html><head>
<script type="text/javascript">
window.onload = function() {
text = "parseInt('01')="+parseInt("01")+"<br>";
text += "parseInt('02')="+parseInt("02")+"<br>";
text += "parseInt('03')="+parseInt("03")+"<br>";
text += "parseInt('04')="+parseInt("04")+"<br>";
text += "parseInt('05')="+parseInt("05")+"<br>";
text += "parseInt('06')="+parseInt("06")+"<br>";
text += "parseInt('07')="+parseInt("07")+"<br>";
text += "parseInt('08')="+parseInt("08")+"<br>";
text += "parseInt('09')="+parseInt("09")+"<br>";
text += "parseInt('10')="+parseInt("10")+"<br>";
text += "parseInt('11')="+parseInt("11")+"<br>";
text += "parseInt('12')="+parseInt("12")+"<br>";
text += "parseInt('13')="+parseInt("13")+"<br>";
document.getElementById('content').innerHTML = text;
}
</script>
</head><body>
<p id="content"></p>
</body></html>
You can try it yourself if you don't believe me, but the result is:
parseInt('01')=1
parseInt('02')=2
parseInt('03')=3
parseInt('04')=4
parseInt('05')=5
parseInt('06')=6
parseInt('07')=7
parseInt('08')=0
parseInt('09')=0
parseInt('10')=10
parseInt('11')=11
parseInt('12')=12
parseInt('13')=13
What in the world is going on? [banghead]
 
Hi

OsakaWebbie said:
it seems that parseInt("08") and parseInt("09") are zero rather than what they should be.
The 0 prefix is the notation for octal numbers, just as the 0x prefix is the notation for hexadecimal numbers. And in octal there are 8 digits : 0..7.

Force [tt]parseInt()[/tt] to ignore the notation by explicitly specifying the base :
Code:
[COLOR=darkgoldenrod]parseInt[/color][teal]([/teal][green][i]'08'[/i][/green][teal],[/teal][purple]10[/purple][teal])[/teal]
[COLOR=darkgoldenrod]parseInt[/color][teal]([/teal][green][i]'09'[/i][/green][teal],[/teal][purple]10[/purple][teal])[/teal]


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top