2 Replies Latest reply: May 5, 2010 3:38 PM by Me2LoveIt2 RSS

    output 0# instead of just # in Flash CS4 AS3

    Me2LoveIt2 Community Member

      Hi,

      I was wondering if there is any way to get an number output always in a two digit number, for example time:

       

      What my code does right now:

      0:1

      0:2

      0:3

      0:4

      ...

      0:8

      0:9

      0:10

      0:11

       

      What I want my code to do:

      0:01

      0:02

      0:03

      0:04

      ...

      0:08

      0:09

      0:10

      0:11

       

      Thank you for any help and tips!

        • 1. Re: output 0# instead of just # in Flash CS4 AS3
          riquigley Community Member

          First you have to determine if the number is less than 10, and if so, add a zero before it when you cast it as a String.

           

          Take this number for example:

           

          var myNumber:Number = 5;

           

          First, you need to determine if the number is below 10

           

          if (myNumber < 10)

          {

           

          And if so, you will have to cast your number from a number to a String with a zero in front.

           

          var myString:String = "0" + String(myNumber);

           

          }

           

          I did this just last week to make an MP3 Player.

           

           

          Here are two functions I used to convert the number (in this case, the milliseconds being played) into a time format:

           

          function showTime(e:Number):String
              {
              //e is in milliseconds
              var time:int = Math.round(e/1000); // time is now in seconds
              var hours:int = Math.floor(time/3600);
              var minutes:int = Math.floor((time - (hours * 3600))/60);
              var seconds:int = Math.floor(time - (hours*3600) - (minutes*60));
             
              var hoursText:String = convertToString(hours); // function - see below
              var minutesText:String = convertToString(minutes);
              var secondsText:String = convertToString(seconds);
             
              var showTime:String;

              if (hoursText == "00")
                  {
                  showTime = minutesText + ":" + secondsText;
                  }
              else
                  {
                  showTime = hoursText + ":" + minutesText + ":" + secondsText;
                  }
              return showTime;
              }showTime


          function convertToString(e:int):String
              {
              var returnString:String;
             
              if (e == 0)
                  {
                  returnString = "00";
                  }
              else if (e < 10)
                  {
                  returnString = "0" + String(e);
                  }
              else
                  {
                  returnString = String(e);
                  }
              return returnString;
          }//convertToString
          • 2. Re: output 0# instead of just # in Flash CS4 AS3
            Me2LoveIt2 Community Member

            Thank you this is just what I needed!

            I am also trying to build an mp3 player.

            I have a question:

                  Do you use URL's to stream/load your mp3's? f so how did you deal with memory?

             

            By the way this is my function:

             

            music = sound object

            sc = SoundChannel

             

            var totalseconds:Number; //the length of the song (I'm still looking for a better way to get this information, because just like this it basically displays the amount of time downloaded
            var countingSec:Number; //how many seconds where played
            this.addEventListener(Event.ENTER_FRAME, elapsedTime);
            
             
            
            function elapsedTime(event:Event):void{ 
                 if(sc != null){
                      countingSec = Math.floor(sc.position/1000);
                      if(countingSec%60 < 10)
                           count_txt.text=Math.floor(countingSec/60) + ":0" + countingSec%60;
                      else
                           count_txt.text=Math.floor(countingSec/60) + ":" + countingSec%60;
                      if(totalseconds != Math.floor(music.length/1000)){
                           totalseconds = Math.floor(music.length/1000);
                           if(totalseconds%60 < 10)
                                time_txt.text=Math.floor(totalseconds/60) + ":0" + totalseconds%60;
                           else
                                time_txt.text=Math.floor(totalseconds/60) + ":" + totalseconds%60;
                      }
                      if(totalseconds == countingSec && totalseconds != 0){
                           sc.stop();
                           isPlaying = false;
                           playPause_mc.gotoAndStop(1);
                      }
                 }
            }
            

             

             

             

            Thank you~