8 Replies Latest reply on Aug 20, 2008 7:17 AM by factorypolaris

    Doing Math In Action Script

    factorypolaris
      Hello,

      I am very new to Flex so this is going to be very simple for somebody.

      I am trying to do some math calculations in a Flex App. Here is the function. Called with a change handler.

      private function addAllUp(evt:Event):void {
      unitInput1.text = (quantityInput1.text * priceInput1.text);
      }

      unitInput1, quantityInput1 and priceInput1 are all text input fields. I have it spelled out what I am trying to accomplish in plain english now but I have attempted many variations including Number() with no success. The input fields will always be a number. Im not sure if I am supposed to import somthing to make math work?? I have seached for a long time with no success, I would really appreciate any help.

      Thanks,
      Chris
        • 1. Re: Doing Math In Action Script
          Level 7

          "ItsJustMeHereManb" <webforumsuser@macromedia.com> wrote in message
          news:g8f8el$9i6$1@forums.macromedia.com...
          > Hello,
          >
          > I am very new to Flex so this is going to be very simple for somebody.
          >
          > I am trying to do some math calculations in a Flex App. Here is the
          > function.
          > Called with a change handler.
          >
          > private function addAllUp(evt:Event):void {
          > unitInput1.text = (quantityInput1.text * priceInput1.text);
          > }
          >
          > unitInput1, quantityInput1 and priceInput1 are all text input fields. I
          > have
          > it spelled out what I am trying to accomplish in plain english now but I
          > have
          > attempted many variations including Number() with no success. The input
          > fields
          > will always be a number. Im not sure if I am supposed to import somthing
          > to
          > make math work?? I have seached for a long time with no success, I would
          > really appreciate any help.

          What result are you getting, and how is it different than what you want? Are
          you seeing any errors?


          • 2. Re: Doing Math In Action Script
            factorypolaris Level 1
            I get the following errors.
            1067: Implicit coercion of a value of type String to an unrelated type = Number.
            1067: Implicit coercion of a value of type Number to an unrelated type = Number

            My goal is to Multiply quantityInput1.text * priceInput1.text and have it return the number.

            I have had it return a combonation of the two:
            IE:
            quantityInput1 = 2
            priceInput1 = 2
            It would return "22". This is not what I want. I want it to return 4 (2 times 2).

            Thanks for your help so far. Thanks in advance
            • 3. Doing Math In Action Script
              justria Level 1
              You should use one of these:

              var nOne:Number = parseInt("13.3"); // 13
              var nTwo:Number = parseFloat("13.3"); // 13.3
              var nThree:Number = parseInt("54"); // 54
              var nFour:Number = parseFloat("54"); // 54

              Additionally, here is a list of global functions

              http://livedocs.adobe.com/flex/201/langref/package.html
              • 4. Re: Doing Math In Action Script
                Dr. Fred Mbogo Level 1
                The ActionScript 3 compiler is just more strict than your average browser JavaScript interpreter, that's all. It wants you to be explicit about your type conversions:

                unitInput1.text = (quantityInput1.text as Number * priceInput1.text as Number).toString();

                Haven't tried it, but something like that.
                • 5. Re: Doing Math In Action Script
                  factorypolaris Level 1
                  Boy, you aint kidding about AS3 being strict. I am not able to get:
                  unitInput1.text = (quantityInput1.text as Number * priceInput1.text as Number).toString();

                  To work. I have tried changing it a little but I cant seem to get it. Still getting the following errors
                  Implicit coercion of a value of type Class to an unrelated type Number.
                  1067: Implicit coercion of a value of type String to an unrelated type Number.

                  Got any other solutions for this??


                  Is Action Script the best solution for my math calculating needs? Can I use javascript? Or should I do a call to my server each time to calculate the change. Again, I am very new to FLEX and have been able to get everything so far, but doing simple math calculations is almost starting to seem impossible. Any advise...
                  • 6. Re: Doing Math In Action Script
                    justria Level 1
                    How about this:

                    unitInput1.text = ( parseFloat( quantityInput1.text ) * parseFloat( priceInput1.text ) ).toString();
                    • 7. Re: Doing Math In Action Script
                      justria Level 1
                      Additionally, this should compile too:

                      unitInput1.text = ( ( quantityInput1.text as Number ) * ( priceInput1.text as Number ) ).toString();
                      • 8. Re: Doing Math In Action Script
                        factorypolaris Level 1
                        justria,

                        Both of those compiled for me fine. Thank you very much. I really appreciate everyones help on this one.

                        Have a great day!