4 Replies Latest reply: May 26, 2012 7:35 AM by kglad RSS

    comma(,) after 3 digits

    Venkom Community Member

      i trying ot add comma(,) after 3 digits to my result value.

      but result first time is good, and 2nd time is result changed (not correct result value)

       

       

      my code:

      var reg:RegExp = /\d\d\d$/g;
      var a:Array = new Array();
      var res:String ='';
      var dnSpeeds:String;
      
        dnSpeeds = e.params.bandWidth;
        downSpeeds.text = dnSpeeds + " Kbps";
        downTest();
      
       while (dnSpeeds.length > 3){
                a.push(','+dnSpeeds.match(reg));
                dnSpeeds = dnSpeeds.replace(reg,'');
                }
      
                for (var i:int = a.length; i > 0; i--){
                res = res+a[i-1];
                }
      
                res = dnSpeeds+res;
                trace(res);
      
      
      function downTest()
      {
      if (Number(dnSpeeds) >= 512)
                {
        Pass.visible = true;
        Fail.visible = false;
                }
      else{
      
        Pass.visible = false;
        Fail.visible = true;
                }
      }
      
        • 1. Re: comma(,) after 3 digits
          kglad MVP

          search for "number formatting flash as3" to find formatting that does what you want.  for example,

           

          http://stackoverflow.com/questions/721304/insert-commas-into-number-string

          • 2. Re: comma(,) after 3 digits
            esdebon Community Member
            I used this function, I found somewhere that I can not remember

             

             

             

            function addCommasToNumber(number:Number):String

            {

                var negNum:String = "";

                if (number<0)

                {

                    negNum = "-";

                    number = Math.abs(number);

             

                }

                var num:String = String(number);

                var results:Array = num.split(/\./);

                num = results[0];

                if (num.length > 3)

                {

             

                    var mod:Number = num.length % 3;

                    var output:String = num.substr(0,mod);

                    for (var i:Number = mod; i<num.length; i += 3)

                    {

                        output += ((mod == 0 && i == 0) ? "" : ",")+num.substr(i, 3);

                    }

                    if (results.length > 1)

                    {

                        if (results[1].length == 1)

                        {

                            return negNum+output+"."+results[1]+"0";

                        }

                        else

                        {

                            return negNum+output+"."+results[1];

             

                        }

             

                    }

                    else

                    {

             

                        return negNum+output;

             

                    }

             

                }

             

                if (results.length > 1)

                {

                    if (results[1].length == 1)

                    {

                        return negNum+num+"."+results[1]+"0";

                    }

                    else

                    {

                        return negNum+num+"."+results[1];

                    }

                }

                else

                {

                    return negNum+num;

                }

            }

            • 3. Re: comma(,) after 3 digits
              Venkom Community Member

              yaa i tried this example. working

              thank you guys,

              • 4. Re: comma(,) after 3 digits
                kglad MVP

                you're welcome.