Skip to main content
Inspiring
June 15, 2008
Answered

Label1.text + Label2.text = Mess

  • June 15, 2008
  • 6 replies
  • 1012 views
Hello!

Im sorry im troubling you with this (i guess) simple problem. My problem i simple. I want to add two numbers stored in two different label components.

Ex:
{
Label1.text = 1;
Label2.text = 1;

Label3.text = Label1.text + Label2.text;
}

The result is that Label3.text becomes 11, but i want it to become 2! How do i make it add the to numbers instead of just adding two strings to each other.
This topic has been closed for replies.
Correct answer kglad
convert those strings to numbers using the Number() function.

6 replies

Inspiring
June 16, 2008
I think it has been more often when I was reading css, xml, or other external files, but it could also have been some kind of white space or special key at the end. In any event I just find it easier to always use parseInt/Float and not have to worry about it working.

I've never tested it for any kind of speed hit for anything that required a lot of operations, so there might be a savings there, but these types of things are usually fairly low performance in any event.

So for typing four more characters I feel I get a benefit. Plus you can use the second argument and convert to other bases if you need to!
kglad
Community Expert
Community Expert
June 16, 2008
i've never had it fail. was it a white space issue that caused Number() to fail?
Inspiring
June 16, 2008
Or better yet, really convert them to numbers using parseFloat() or you can use parseInt() if you know that you will only have integers.

Label3.text = parseFloat(Label1.text) + parseFloat(Label2.text);

There are times when I have seen casting as Number() fail to properly change the value to a number, even when it seems like it should work.
kglad
Community Expert
Community Expert
June 16, 2008
you're welcome.
PinnennetAuthor
Inspiring
June 15, 2008
Thank You!
kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
June 15, 2008
convert those strings to numbers using the Number() function.