5 Replies Latest reply on May 6, 2008 6:38 AM by kglad

    String to Number

    ooba Level 1
      trying to compare values but value is seen as a string so it wont add.

      i.e.

      var x = value from XML file. such as 1111
      var y = x + 5

      output is 11115
      should be 1116
        • 1. Re: String to Number
          kglad Adobe Community Professional & MVP
          use the Number() function.
          • 2. Re: String to Number
            ooba Level 1
            Simple answer thanks Kglad.

            but can you tell me why it was not a Number to begin with?
            here is the line of code

            var nextT:Number = nextObjTimeMarker.timeMarker + 5;
            results in 1115

            It is now written as the following even though i consider it redundant.
            var nextT:Number = Number( nextObjTimeMarker.timeMarker ) + 5;
            results in 116
            • 3. Re: String to Number
              kglad Adobe Community Professional & MVP
              all "numbers" loaded by flash from text or xml files are really strings. so any addition you do with them will be string addition.

              you should cast that string as a number once, and then you don't have to do that again:

              var x:Number = Number( nextObjTimeMarker.timeMarker );
              .
              .
              .
              var nextT:Number = x+5;
              • 4. Re: String to Number
                ooba Level 1
                kglad, thank you so much for the response. I would never had thought that the value would be string inferred since the var was stong typed. I updated my code and things are working smoothly. Thanks again.
                • 5. Re: String to Number
                  kglad Adobe Community Professional & MVP
                  you're welcome.

                  p.s. the as3 compiler would generate an error about not being able to coerce a string to a number. as2 just lets you have problems until you figure it out.