Paolo Broccardo
Paolo Broccardo

Reputation: 1744

Comma format a number

I need to format numbers with commas as thousand seperators, for example:

1234 = 1,234
1234.50 = 1,234.50
12345.60 = 12,345.60
123456.70 = 123,456.70
1234567.80 = 1,234,567.80
etc etc

This needs to work for numbers with decimal values or without i.e. both 1234567.80 and 1234567

This is for Actionscript 2 in a Coldfusion / Flash application, so normal actionscript is being used. I have seen a couple of solutions on the net but none quite do the trick.

So far I have the function below, but it is not formatting correctly when decimals are provided.For example: 21898.5 becomes 2,188,8.5.

Please could you help me find the bug or offer an alternative solution that fulfils the requriements. Thanks

_global.NumberFormat = function(theNumber)
        {
            var myArray:Array;
            var numberPart:String;
            var decPart:String;
            var result:String = '';
            var numString:String = theNumber.toString();

            if(theNumber.indexOf('.') > 0)
            {
                myArray = theNumber.split('.');
                numberPart = myArray[0];
                decPart = myArray[1];
            }
            else
            {
                numberPart = numString;
            }

            while (numString.length > 3)
            {
                var chunk:String = numString.substr(-3);
                numString = numString.substr(0, numString.length - 3);
                result = ',' + chunk + result;
            }   
            if (numString.length > 0)
            {
                    result = numString + result;
            }   

            if(theNumber.indexOf('.') > 0)
            {
                result = result + '.' + decPart;
            }


            //alert('Result: ' + result);

            return result;
        }

Upvotes: 3

Views: 4714

Answers (3)

Reijii
Reijii

Reputation: 49

public static function formatNumberString(value:Number,separator:String):String {
var result:String = "";
var digitsCount:Number = value.toString().length;

separator = separator || ",";

for (var i:Number = 0; i < digitsCount; i++) {
  if ((digitsCount - i) % 3 == 0 && i != 0) {
    result += separator;
  }
    result += value.toString().charAt(i);
  }
  return result;
}

Upvotes: 0

shanethehat
shanethehat

Reputation: 15570

You could try this:

_global.NumberFormat = function(numString)
{
    numString = String(numString);
    var index:Number = numString.indexOf('.');
    var decimal:String;
    if(index > 0) {
        var splitByDecimal:Array = numString.split(".");
        //return NumberFormat(splitByDecimal[0])+"."+splitByDecimal[1];
        numString = splitByDecimal[0];
        decimal = splitByDecimal[1];
    } else if(index === 0) {
        return "0"+numString;
    }
    var result:String = '';
    while (numString.length > 3 ) {
        var chunk:String = numString.substr(-3);
        numString = numString.substr(0, numString.length - 3);
        result = ',' + chunk + result;
    }
    result = numString + result;
    if(decimal) result = result + "." + decimal;
    return result;
}

It splits the number by the decimal if present(compensating for an illegal '.01234' if required), and uses recursion so call itself on the split element.

For your example numbers this traces:

1,234
1,234.50
12,345.60
123,456.70
1,234,567.80

Just for fun

This is why your original code didn't work:

  1. After creating a string representation of the number (var numString:String = theNumber.toString();) you then carried on using the actual number rather than the string version.
  2. After assigning a value to number part you then continued to perform operations on numString rather than numberPart.

A corrected version looks like this:

_global.NumberFormat = function(theNumber)
{
    var myArray:Array;
    var numberPart:String;
    var decPart:String;
    var result:String = '';
    var numString:String = theNumber.toString();

    if(numString.indexOf('.') > 0)
    {
        myArray = numString.split('.');
        numberPart = myArray[0];
        decPart = myArray[1];
    }
    else
    {
        numberPart = numString;
    }

    while (numberPart.length > 3)
    {
        var chunk:String = numberPart.substr(-3);
        numberPart = numberPart.substr(0, numberPart.length - 3);
        result = ',' + chunk + result;
    }   
    if (numberPart.length > 0)
    {
        result = numberPart + result;
    }   

    if(numString.indexOf('.') > 0)
    {
        result = result + '.' + decPart;
    }


    //alert('Result: ' + result);

    return result;
}

Upvotes: 3

Bero Windrider
Bero Windrider

Reputation: 23

Try this out, works fine for me:

var largeNumber:String=new String(1000000.999777);
var fAr:Array=largeNumber.split(".");
var reg:RegExp=/\d{1,3}(?=(\d{3})+(?!\d))/;

while(reg.test(fAr[0]))
fAr[0]=fAr[0].replace(reg,"$&,");
var res:String=fAr.join(".");
trace(res);

Trace: 1,000,000.999777

Upvotes: -1

Related Questions