Skip to main content
Inspiring
September 10, 2006
Question

Multiplying Strings?

  • September 10, 2006
  • 1 reply
  • 387 views
Shouldn’t the following be illegal? In this example, shouldn’t the parseInt() be used to convert the 2 strings into an int and then multiplied?

var tenStr:String = new String("10");
var fiveStr:String = new String("5");

this.theAnswer = this.tenStr*this.fiveStr;
trace(this.theAnswer);//Output: 50
This topic has been closed for replies.

1 reply

Inspiring
September 10, 2006
myIP,

> Shouldn?t the following be illegal? In this example,
> shouldn?t the parseInt() be used to convert the 2
> strings into an int and then multiplied?

I didn't see parseInt() in your code, but you certainly raise an
interesting point.

> var tenStr:String = new String("10");
> var fiveStr:String = new String("5");

So far, so good. You've declared two String variables and have clearly
supplied them with String values.

> this.theAnswer = this.tenStr*this.fiveStr;

In this line, the * operator "sees" two numbers, so it multiplies them.
I know you typed them as Strings, but keep in mind: the (so-called) strong
typing in AS2 is not *true* strong typing. In AS2, the post colon suffix is
only checked during the compile process. At compile time, vars tenStr and
fiveStr are, in fact, strings.

If either variable had had any characters besides numerals, the result
of your multiplication would be NaN, as expected.


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


injpixAuthor
Inspiring
September 10, 2006
Thanks for your explanation. It seems strange that you can multiple, divide and subtract by just changing the sign (operator) in code I posted earlier. However if you want to add the two variables, it seems that you would need to convert the strings to numbers to prevent them from concatenating. And I understand why this is, but it’s inconsistent with other arithmetic equations. It also looks like that this “strong typing” is also carrying over to AS3.0. Do you know if similar code can be performed in other ECMA compliant languages?