Skip navigation
Currently Being Moderated

comma(,) after 3 digits

May 25, 2012 6:17 AM

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;
          }
}
 
Replies
  • kglad
    72,260 posts
    Jul 21, 2002
    Currently Being Moderated
    May 25, 2012 8:27 AM   in reply to Venkom

    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-st ring

     
    |
    Mark as:
  • Currently Being Moderated
    May 25, 2012 8:33 AM   in reply to Venkom
    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;

        }

    }

     
    |
    Mark as:
  • kglad
    72,260 posts
    Jul 21, 2002
    Currently Being Moderated
    May 26, 2012 7:35 AM   in reply to Venkom

    you're welcome.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points