What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » help with convert strings

help with convert strings
Author: Message:
ChaosMaker
New Member
*


Posts: 4
78 / Male / Flag
Joined: Jun 2010
O.P. help with convert strings
hi all, i want convert strings to numbers
i´m using this
var number1='5';
var number2='4';
var result=number1+number2;
Messenger.MyPersonalMessage=result
but the result is 54, and not 9. why ?
how can i convert '5' to 5 ?
08-08-2010 04:23 AM
Profile E-Mail PM Find Quote Report
Adeptus
Senior Member
****


Posts: 732
Reputation: 40
Joined: Oct 2005
RE: help with convert strings
quote:
var number1='5';
var number2='4';
var result=number1+number2;
...
but the result is 54, and not 9. why ?
To answer your question for your specific example: because you have indicated the values are strings by using single quotes.  Remove the quotes and try the following:
code:
var number1 = 5;
var number2 = 4;
var result = number1 + number2;
Now you should be getting 9 as the result. 

Of course, you won't always be able to do this when your values are coming from somewhere else and happen to be strings.  When that happens you will want to check out parseInt() and parseFloat():
code:
var number1 = '5';
var number2 = '4';
var result = parseInt(number1) + parseInt(number2);
This should yield 9 as well.
08-08-2010 06:00 AM
Profile E-Mail PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
35 / Male / Flag
Joined: Aug 2006
RE: help with convert strings
In addition to that, a trick I tend to use is:

Javascript code:
var number1 = '5';
var number2 = '4';
var result = (number1 * 1) + (number2 * 1);


The brackets shouldn't be needed, but I use them anyway

This post was edited on 08-08-2010 at 07:29 PM by Spunky.
<Eljay> "Problems encountered: shit blew up" :zippy:
08-08-2010 01:34 PM
Profile PM Find Quote Report
NanaFreak
Scripting Contest Winner
*****


Posts: 1476
Reputation: 53
32 / Male / Flag
Joined: Jul 2006
RE: help with convert strings
quote:
Originally posted by Spunky
Javascript code:
var result = (number1 * 1) + (number2 * 2);



shouldnt the second one be * 1 still? :P
08-08-2010 02:19 PM
Profile PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
35 / Male / Flag
Joined: Aug 2006
RE: help with convert strings
Yeah, I was in a rush going out...
<Eljay> "Problems encountered: shit blew up" :zippy:
08-08-2010 07:29 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On