4 Replies Latest reply on Sep 11, 2006 5:56 AM by injpix

    Multiplying Strings?

    injpix Level 3
      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
        • 1. Re: Multiplying Strings?
          Level 7
          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."


          • 2. Re: Multiplying Strings?
            injpix Level 3
            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?
            • 3. Re: Multiplying Strings?
              Level 7
              myIP,

              > It also looks like that this ?strong typing? is also carrying
              > over to AS3.0.

              Have you tried it yet in AS3? I haven't given it a shot, but I will --
              then I'll post back. My hunch is that results would be different in AS3,
              because in the new language, strong typing is the Real McCoy.

              As soon as I've tested, I'll write back. :)


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


              • 4. Re: Multiplying Strings?
                injpix Level 3
                I am almost certain that this also happens in AS3. I was aware of this before AS3 came out but I would also assume that it would have been resolved in AS3. However after looking up operators in the ECMA PDF (type in page 60 at the bottom of Reader);

                http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

                …it doesn’t seem that ECMA cares about this also. So now it makes sense why Adobe left this “as is” in AS3. In my humble opinion, I believe that it should be changed. Perhaps it will in the next ECMA version!?