Click to See Complete Forum and Search --> : string to integer, help me i'm a noob
megalolinux
03-16-2003, 02:33 PM
hi, I'm just wondering what the built-in JS function to get an integer value from a string is, I'm trying to get integers from a select option label form.
I know it is Int(str_expression) for some languages like VB. But I'm new to JS and I don't know what it is.
I tried (str_expression:Integer) and that didn't work either.
Can someone help me and un-noobify me on the subject.
thanks
dan@digitalreviewers.com
khaki
03-16-2003, 03:00 PM
Number(str_expression)
(I think... I hope)
Try it and let me know if that's right.
k
AdamGundry
03-16-2003, 03:19 PM
You can also use parseInt(str_expression).
Adam
AdamBrill
03-16-2003, 03:40 PM
Just so you know what the difference is, Number(str_expression) will take the string and turn it into a number. If there are any non-number characters in the string, it will return NaN. parseInt(str_expression) does the same thing except that if it finds any non-number characters it will take all the numbers up to the first non-number character and return that. I just thought I would explain that... ;)
khaki
03-16-2003, 03:42 PM
You read my mind Adam (Brill. hey! your both Adam. lol).
I was going to ask if there were any arguments for that function.
So it just parses at the first non-integer... no other arguments?
(formatting, anything like that?)
AdamBrill
03-16-2003, 03:54 PM
Nope, no other arguments. ;) If the string is "123e4", Number will return NaN, but parseInt would return 123. I hope that helps you understand... :)
Originally posted by Khaki
You read my mind Adam (Brill. hey! your both Adam. lol).LOL :D
AdamBrill
03-16-2003, 04:01 PM
Ok, one more thing... ;) If the string is "11.24e", then parseInt would return 11. If you want to get 11.24, use parseFloat. It is used like this:
parseFloat(str_expression);
I figured I might as well point that out, too. :D
khaki
03-16-2003, 04:08 PM
OK.
Yeah... that's sorta the "formatting" part that I was looking for (kinda, but not really. whatever).
Good to know though. Thanks for throwing that in too.
On a related note:
Is there a Javascript equivalent for Len.
(so that the parsed result and the original string could be compared to determine the original length of the string?)
someone opened a can of worms... and now I'm fishing...
k
khaki
03-16-2003, 04:19 PM
Never mind...
It's the rather obvious length().
gee... I hope that megalolinux doesn't mind that I hijacked his thread (lol)
Thanks for the help!
k
AdamBrill
03-16-2003, 04:35 PM
Just to point out one more thing, to find the length of a number, do it like this:
String(number).length
I hope that helps... ;)
megalolinux
03-16-2003, 10:47 PM
Awesome guys, thanks for helping me out, that was wicked fast. I'll probably have more questions in the near future.
Thanks
Dan
khaki
03-16-2003, 10:49 PM
Oh Adam.... now why did you go and do that?
Let me try to understand...
To find the length of an integer:
String(number).length
...but to find the length of a string:
length(string)
???
Why not String(myStr).length?
(and if so... when would length(x) be used at all?
Another question (don't blame me... it's the worms. lol)...
If I use:
String(number).length
or...
String(myStr).length...
does that return a number recognized as a string value (non-numeric number?) while the following methods return true numeric (integer) values:
length(string) and
length(integer)
I believe that the VB/VBA/VBScript len(string) and len(integer) returns a numeric value (integer), even though the returned value of len is most often used for boolean comparison (unless previously defined otherwise).
So I'm just wondering what kind of datatype Javascript assigns to the same type of returned value.
Just a word on all of that...
I just got the biggest headache while typing that... so please try to read/understand/and answer it for me as best as you can (p l e a s e !).
I'm really trying to understand Javascript, and - correct me if I'm wrong - but I'm beginning to beleive that all returned values (with the exception of those returned via mathematical formulas) are string data types by default (which would then need to be converted to integers if necessary elsewhere in the code).
Whew! My brain is going to explode. LOL (but I'm also thinking that this little bit of information may be part of the reason why I have had so much difficulty trying to understanding Javascript's logic up to this point).
Have I sent you running for the hills, or are you still with me on this?
fun when silly... challenging when not...
k
AdamBrill
03-16-2003, 11:19 PM
Originally posted by khaki
Have I sent you running for the hills, or are you still with me on this?
fun when silly... challenging when not...
k LOL Your funny, k. ;) Just so you know, I won't abandon you. :D
Now to answer your questions... ;) I'm just going to expain how it works as well as I can and you can ask if you still have questions after that. :D
First of all, you can't do number.length, you can only use the .length on a string. That is why I had you use String(number).length. After you do that, number is still a integer, since you didn't tell number to convert, you just checked the length. If you would have wanted it to convert the number to a string, it would be like this:
number = String(number);
Another thing is that there is no length(string) command. You posted that so I figured I would point that out. ;)
Now, when you do a String.length, it returns a integer not a string. So, when you do String(number).length, it leaves number as a integer and returns the length of number...
So, to sum it up, if it is a string, use this:
String.length;
if it is a integer, use this:
String(integer).length;
if you don't know which one it is going to be(or just want the way that always works :D), use this:
String(string\integer).length;
So, if you use the last option, it will work weather it is a string or an integer.
Originally posted by khaki
I'm really trying to understand Javascript, and - correct me if I'm wrong - but I'm beginning to beleive that all returned values (with the exception of those returned via mathematical formulas) are string data types by default (which would then need to be converted to integers if necessary elsewhere in the code).I would have to agree that most of time that is true, but not all the time. It is a good idea to find out what it is returning before you try to do anything with it... That saves a lot of problems. ;)
Anyway, I hope I didn't confuse you too much and good luck. ;)
Since people like to point out how long your posts are(not me, other people ;)), you can now point out that someone writes longer posts than you do. LOL :D
khalidali63
03-16-2003, 11:39 PM
Originally posted by khaki
...but to find the length of a string:
length(string)
........
Why not String(myStr).length?
(and if so... when would length(x) be used at all?
length(string) os not a function JavaScript.
JavaScript functions work like Java or C/C++ function
object.method();
hence string.length is a valid function call to get length
and yes it returns a number value.
Cheers
Khalid
khaki
03-16-2003, 11:41 PM
Oh crap!
I thought that length was a function...
it's a property! :rolleyes:
Javascript! I try, I try, I try... it just won't click! Ugh!
Anyway... Adam, you are the best. I'll read through your response again in the morning when my head is less cluttered.
Since people like to point out how long your posts are (not me, other people ), you can now point out that someone writes longer posts than you do. LOL I know that they point it out for good reason Adam (even if you don't - although you kinda just did. wink). I try to keep them short... I really do. Maybe I should just be banned from the Javascript forum completely. I don't do very well here. :(
long and wrong (ugh)...
k
EDIT: Oh... thanks khalid. Further confirmation of my dizziness.
I need to be followed around with a pooper-scooper when I'm in this forum (lol).
AdamBrill
03-17-2003, 08:04 AM
Originally posted by khaki
I know that they point it out for good reason Adam (even if you don't - although you kinda just did. wink). I try to keep them short... I really do. Maybe I should just be banned from the Javascript forum completely. I don't do very well here. LOL hmm.. Maybe I did, but that isn't what I was trying to do. :D I can't see what is wrong with it anyway... ;)
Dave - Ok, how about a better explaination?? ;) First of all, on MSDN it said the other argument can be anywhere from 2 to 36... What do they all do? Is there a link that tells what each number does? I couldn't find anywhere that explained it very well... They all just said it existed and left it at that. :rolleyes:
khaki
03-17-2003, 09:59 AM
At the risk of leaving another stain on the beautiful Javascript forum carpet... (especially since Dave thinks that the "issue of numbers and strings to numbers has been pretty well hashed out here".) I'd just like to confirm 1 more thing (and then I'll go away.... for a little while anyway)...
Are these 2 equivalent (i mean, i know that it works, but is it the equivalent way of doing the same thing)?
vbscript:
int(document.form1.text1.value)
javascript:
parseInt(document.form1.text1.value)
...am I over-thinking this?
(please try to understand that I am trying to apply what I know in one language to the comparable way of doing it in another language... so that I can think in both languages).
It seems to me like the javascript parseInt() does more (even if not required) than the vbscript int().
But whatever... (theres nothing wrong with that) if I should be using parseInt() in the same way that I have always used int(), then so be it.
So...?
guilty of telekinetically beating a subject to death using only my mind and a keyboard...
k
PS - Dave... thanks for the additional argument for the parseInt() method. I personally would only need to use the decimal argument (10), but I still find it kinda peculiar that one would have the need to know the decimal value of 377aq42mmo2 (but then again... you Javascript geniuses probably do that sort of thing all the time. lol)
AdamBrill
03-17-2003, 04:52 PM
Well, I'm not a VBScript expert, but this is how it worked for me. ;) With this:
VBScript :
int(document.form1.text1.value)
If the form value had only numerical digits it would work fine. If it had any other characters it would come up with an error. With this:
JavaScript:
parseInt(document.form1.text1.value)
If the form value had only numerical digits it would work fine and if there were any other characters, it would return the numbers up to the first non-integer character.
So, to answer your question, they both do basically the same thing(convert a string to a integer). But they have a few differences, as I said above. I hope this helps you to understand... :D
khaki
03-17-2003, 05:06 PM
Yes Adam... thank you.
That's all I needed to know.
(and i'm aware of the potential for error based on the element's contents. The example was basic... but thanks for pointing that out. It's always best to assume nothing with me, I guess).
I appreciate your help.
k
AdamBrill
03-17-2003, 06:22 PM
Ok, thanks Dave. ;)
khaki
03-17-2003, 06:35 PM
Thanks Dave.
That really closed some gaps for me.
It's all just Javascript baby steps for me, I guess.
Does anyone know of any resource which actually takes a VBScript/Javascript equivalency approach?
If not... would someone like to collaborate on one. I smell book in that!
pithy and appreciative...
k