9 Replies Latest reply on Jul 15, 2011 3:42 PM by Francisc

    Best way to cast to String

    Francisc Level 3

      Hello,

       

      I want to convert item.someNumber to string in order to count it's char length. If it has only one char, I prefix a '0', otherwise I don't touch it.

      What's the best way to do this?

       

      item.someNumber.toString()

      String(item.someNumber)

      (String)item.someNumber

      ?

       

      Thank you.

        • 1. Re: Best way to cast to String
          kokorito Level 4

          if(someNumber < 10)

          {

               someString = "0" + someNumber;

          }else{

               someString = String(someNumber);

          }

           

           

          or some variation if you handle negative numbers

          if(someNumber < 10 && someNumber > 0)

          • 2. Re: Best way to cast to String
            Francisc Level 3

            Nice, thank you.

            • 3. Re: Best way to cast to String
              Peter Blazejewicz Level 4

              Hi,

              there are probably many different ways, you could try:

              var paddedNumber:String = ((i>>0) > 0 && i < 10) ? new String("0"+i) : new String(i);

              trace(paddedNumber);

              where *i* is Number - it could be float, right? so it's rounded for check and padded and e.g. displayed as 09.999 (as there is one char before dot),

               

              hth,

              regards,

              Peter

              1 person found this helpful
              • 4. Re: Best way to cast to String
                Francisc Level 3

                Thank you Peter.

                 

                I was worried about uneccesary overhead on Mobile devices.

                • 5. Re: Best way to cast to String
                  drkstr_1 Level 4

                  Overhead will be caused by graphic rendering, not casting on Strings. 

                   

                  I suggest focusing more effort on optomizing skins, preforming efficient layout bound calculations, reducing the complexity of the display list hierarchy at any given time, and bitmap chacing on complex vector graphics (if needed).

                   

                  Those are the things that will cause noticable preformance issue to the end user. The effects various type casting methods are negligable.

                  • 6. Re: Best way to cast to String
                    Francisc Level 3

                    I remember reading that even casting matters in mobile development with Flex, hence me asking.

                    • 7. Re: Best way to cast to String
                      drkstr_1 Level 4

                      I'm not saying to disregard the most efficient way to cast a string, just that it will never end up being your "bottleneck". If you plan on spending time going back through your code to change your type casting method, you are waisting your effort (which could be spent fixing the actual bottlenecks in your application). I was just adding this as a side note in case you were attempting to spin your wheels trying to optomize where it's not needed. Your question is still a valid one.

                       

                       

                      FYI, strong typing is always fastest (at least in the newer version of the player), regardless of the type.

                       

                      IE.

                       

                      SomeType(someVar);
                      1 person found this helpful
                      • 8. Re: Best way to cast to String
                        Francisc Level 3

                        OK. I've reached any bottlenecks yet, thankfully.

                        I'm taking it one step at a time.

                        • 9. Re: Best way to cast to String
                          drkstr_1 Level 4

                          Sure you have!

                           

                          Bottlnecks always exist. No matter what you do, you will always have a "limiting reagent" in your app. The key to optomization is finding and eleminating bottlenecks until they are no longer noticeable to the end user. The bottlenecks will still exist, they will just no longer be noticble. Type casing differently on a string is a perfect example of an optomization that will go completly unoticed by anyone but yourself.

                           

                          Anyways, sorry to go off topic. Most of this is a matter of opinion depending on your shchool of thought. I am off the school of throught that if a tree falls in a forest with no one around to hear it, then in fact, no sound was made.