5 Replies Latest reply on Aug 5, 2010 4:25 PM by kglad

    show 1 decimal points on integers in AS 2...

    BG Designer

      How do I most simply format the following code's output to 1 decimal:

       

      var curcount:Number= 3.0;
      var increment:Number = 0.1;
      
      var si:Number = setInterval(function ()
      {    
      counterText.text = String(curcount += increment);
      
      }, 200);
      

       

      The output already shows the one decimal except when it lands on an integer.

       

      Any help is appreciated!

       

      Bryan

        • 1. Re: show 1 decimal points on integers in AS 2...
          kglad Adobe Community Professional & MVP

          use:

           


           

          var curcount:Number= 3.0;
          var increment:Number = 0.1;

          var si:Number = setInterval(function ()
          {   
          counterText.text = formatF(curcount += increment);

          }, 200);

          function formatF(n:Number):String{
          var a:Array = n.toString().split(".");
          while(a[1].length<1){
          a[1] = a[1]+"0";
          }
          return a[0]+"."+a[1];
          }

          • 2. Re: show 1 decimal points on integers in AS 2...
            BG Designer Level 1

            Thanks  for your quick reply.

             

            It's showing a "u" instead of a 0, when I test locally.

             

            will that change when tested on a server?

             

            Bryan

            • 3. Re: show 1 decimal points on integers in AS 2...
              kglad Adobe Community Professional & MVP

              no, that will still be a problem.  use:

               

              function formatF(n:Number):String {
                  var a:Array = (Math.round(n*10)/10).toString().split(".");
                  if(a[1]==undefined){
                      a[1] = 0;
                  }
                  return a[0] + "." + a[1];
              }

              • 4. Re: show 1 decimal points on integers in AS 2...
                BG Designer Level 1

                Hi again,

                 

                That worked great! Thanks so much for your help! Kglad, you've made me glad!

                 

                Seriously, though, you are all over these forum threads. Does it serve as good marketing for your business? How much do you charge for Flash consulting, if more in depth than these forum discussions?

                • 5. Re: show 1 decimal points on integers in AS 2...
                  kglad Adobe Community Professional & MVP

                  i usually charge by the job.  i gather as much information as i need to understand what should be done and i quote a price that can be accepted or not.

                   

                  most jobs are under $100 but i've charged as much as $10,000 for large complex projects and as little as $0 for people that offer to pay but have a problem i would fix free of charge via these forums.

                   

                  because i do no advertising and no soliciting, outside the 3 flash adobe forums, these are the only marketing i've done.  it's enough to generate a fair amount of work but it would not come close to paying my living expenses.

                   

                  fortunately (for me), flash is more a hobby for me than a business and is something i enjoy.  i have a medical career that pays my bills.