4 Replies Latest reply on May 28, 2010 1:02 AM by Annqan

    Can't convert my String to numbers




      I'm trying to convert my received data to numbers in actionscript in Flex.

      I want to use the numbers that I get in a calculation.


      The function where the data ("Svar") in received and saved in a string looks like this:


      Svar.htmlText = event.result.toString();


      And this is the calculation that I want to do:



      var r:Number = Number(Svar)/2.1;

      var newheight:Number = 420 * r;

      var newwidth:Number = 162 * r;

      resizeLarge.widthTo = newwidth;

      resizeLarge.heightTo = newheight;




      I've checked the calculation and it works so it must be something wrong with the

      "Svar" data.



      I'd be really grateful if someone could help me!

        • 1. Re: Can't convert my String to numbers
          injpix Level 3

          Use parseInt().



          var r:Number = parseInt(Svar)/2.1;

          • 2. Re: Can't convert my String to numbers
            BhaskerChari Level 4



            You are doing something wrong with the calculation in the below line of code.


            var r:Number = Number(Svar)/2.1;


            Here what does Svar corresponds to....Is it a variable or a Labeel control....?


            I think Svar is some kind of a Label or Text control since in your code you have used "htmlText: attribute with the "Svar" as shown below.


            Svar.htmlText = event.result.toString();


            So in your calculation also you need to use the same ...I mean ...text property as shown below..


            var r:Number = Number(Svar.text)/2.1;



            Dont use Svar.htmlText  in calculation as it may give include unnecessary htmltags and your conversion from string to Number fails.



            Hope now the things are clear...If you still have any dobt let me know.


            If this post answers your question or helps, please kindly mark it as such.


            Bhasker Chari

            1 person found this helpful
            • 3. Re: Can't convert my String to numbers
              Annqan Level 1

              Hi Bhasker Chari!


              Thanks for your help!


              I did what you said and it almost works now..


              The application that I'm working on will resize an image to a new size. The new size is

              calculated from "Svar" as you saw. Before I did what you said, the image didn't resize

              and after it resized to... well nothing. It disappeared.


              In the test I did, the Svar-data was 1.85 so the new height of the image should be

              370 px (the original height was 420 px).


              How do I see that the new value of "r" is a number?


              Thanks again!



              • 4. Re: Can't convert my String to numbers
                BhaskerChari Level 4

                Hi Annqan,


                I am sorry I din't get your question...Are you asking about how you can see that the value of "r" is a number?


                If it is so then you can debug by putting a break point at the line of code and watch the variable "r" by right clicking on the letter "r" and click watch r.


                You can see the value of "r" that has been calculated.




                Bhasker Chari