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

parseInt driving me mad -> please help 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
hello peeps,

I was getting some bogus results when trying to validate a date field, i've finaly worked out what is giving the error but don't understand why, here is the logic...

I have a field , you enter 08/01/2007

I then do a split on it with "/" , ->
Code:
var dateArray = value.split("/");

if i display dateArray[0] is shows "08" , everything fine here, but then....

to ensure I am passing numbers to the date function I parseInt each section of the date array, like so...

Code:
var day = parseInt(dateArray[0]);

however if I then display (alert) the variable day it is = 0 (ZERO) huh why ?

if I enter 01,02,03 etc.. up to 07 , parseInt shows 1,2,3,4,5,6,7 HOWEVER 08 & 09 come back as = 0 , only those two come back as zero, why? they should come back as 8,9 , what on earth is going on?

A very confused 1DMF




"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
This
[tt]parseInt("08")[/tt]
would return 0 before it think it is of number of octal representation (base 8) (because it begins with "0"), and 8 is not in the admissible set. Hence it returns 0. In the same line of reasoning,
[tt]parseInt("07")[/tt]
would happily return 7.

Hence, to do it correctly for decimal, furnish the 2nd parameter.
[tt]parseInt("08",10);[/tt]
 
why on earth would a Integer function not automatically work on base 10?

seems a bit odd to me, but it worked, so than you!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Thanks! Why? Because there are some conventions on representing numbers. These two pairs of lines are thereby equivalent.
[tt]
alert(parseInt("0xabc",16));
alert(parseInt("0567",8));

alert(parseInt("0xabc"));
alert(parseInt("0567"));
[/tt]
 
I just feel it's odd because I was taught an Integer is a whole number based on decimalisation that's all.

DEC = 10 in latin!

But I guess it's just as crazy as our current calendar, technically December should be the tenth month and October the eigth, oh well, such is life - lol!



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
And it is, if you use

Code:
parseInt("8")

you will get an 8. In fact, parseInt works as an interpreter, returning a base 10 integer representation of the given argument.

If you put a 0x, parseInt will understand its and hexadecimal number you want to translate. With 0, it will be octal. Put nothing and you will get decimal.

Cheers,
Dian
 
I appreciate that now, before if I gave parseInt this
alert(parseInt("0xabc"));

I would have expected the result to be 0 , as the rest are leters.

What I also find odd is if you pass parseInt "abc" , you would get NaN , which I am assuming = Not a Number ,

So therefor if you passed parseInt("08") and it is thinking in Octal , shouldn't you get NaN back as 08 is "Not a Number" in Octal!

oh well at least now I know!



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
0xabc -> 2748
abc -> Not a number as you didn't specify it's hexadecimal, so it takes it's decimal
8 -> 8
08 -> 0, as 8 doesn't exist for octal.

Cheers,
Dian
 
so would parseInt("99",8) = NaN ?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top