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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parseInt("08")= 0 ? 2

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
parseInt("08") and parseInt("09") gives 0.
from "01" to "07" parseInt() works fine.
Any reason?
 
not sure why that happens, this works though:

t='08'
k=new Number(t)
alert(k) jared@aauser.com
 
you have to specify the base (not sure of the english word here !! i mean : binary, decimal, octo-something, hex ... is it radix ??)
the proper syntax is : parseInt(string, base) and if you don't supply the base part here is how it goes :
If the input string begins with &quot;0x&quot;, the radix is 16 (hexadecimal). <---- your case !
If the input string begins with &quot;0&quot;, the radix is eight (octal).
If the input string begins with any other value, the radix is 10 (decimal).
If the first character cannot be converted to a number, parseInt returns NaN.

ok this part (in blue) was stoleen from devedge doc :-/

 
Good answer iza. I don't think bepuzzled is in hex though. If he's going from 0-7 just fine, he's in octal. Am I wrong? I thought hex went:
Code:
0 = 0
1 = 1
2 = 2
.
.
.
9 = 9
10 = A
11 = B
12 = C
13 = D
14 = E
15 = F

If bepuzzled were in hex he/she should have been able to get to nine before noticing something was wrong. Kevin
slanek@ssd.fsi.com
 
you're right kevin, if 0->7 are fine and 8+ is not, it's likely to be in octal !!!! so it was the 2nd case : it starts with &quot;0&quot; --> octal ! thanx :)
 
Still a pretty darn good answer though! Kevin
slanek@ssd.fsi.com
 
i've been stuck on this for a while myself (a few month ago ...) !! this is so tricky and i was so happy to discover the workaround that i felt to tell it to the whole world ;-) thanx to tek-tips i can share it with a few people :)
 
Thanks everyone for giving precious time to this problem! Thankyou Iza for digging out that very convincing explanation... That was some piece of work!

This is one helluva forum!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top