Click to See Complete Forum and Search --> : parseInt('08') returns 0
shunt
Jun 28th, 2002, 04:02 AM
Hi,
I am having a real problem with this. I want to convert a string '08' to an integer 8, but the parseInt function seems to return 0 for this string... :confused: It also seems to do the same with '09'.
All values less than 8 work fine though .. ?
Does anyone know of a function that converts these strings correctly?
bsw2112
Jun 28th, 2002, 07:52 AM
you can try using eval() but I am not sure if that is the best
way but it seems to be working.
<html>
<head>
<title></title>
</head>
<body>
<script>
var a = '08';
var b = eval('08');
b = b * 2
document.write(b);
</script>
</body>
</html>
regards
bsw2112
cpradio
Jun 28th, 2002, 09:13 AM
bsw2112 you are correct, infact if you wanted to really test your number you could use parseInt(eval('08')) which will make whatever is in the quotes a number and then force it as an integer.
-Matt
shunt
Jul 1st, 2002, 12:53 AM
Thats all good and well, but why does this happen. It should convert to integer
georgec
Jul 10th, 2002, 05:48 PM
Originally posted by cpradio
bsw2112 you are correct, infact if you wanted to really test your number you could use parseInt(eval('08')) which will make whatever is in the quotes a number and then force it as an integer.
-Matt
The problem is because 08 is not a number, and parseInt() returns the first thing it could find that resembles one. 0 is a number to it, but 08 is not, so it returns 0 ;)
shunt
Jul 11th, 2002, 01:03 AM
I'm confused. If it does not see '08' as a number, why does it see '07' as the number 7?
georgec
Jul 11th, 2002, 05:30 PM
Hmmm...if that's the case I have no idea why. My guess is perhaps 07 is a special keyword/identifier. It seems other numbers like 09 also return 0, not 9.
shunt
Jul 12th, 2002, 02:01 AM
actually, all the numers '00' -> '07' return their true number eg. 1, 2 etc. It is only '08' and '09' that seem to have a problem with parseInt()
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.