Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6801993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:10:08+00:00 2026-05-26T19:10:08+00:00

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

  • 0

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;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:10:09+00:00Added an answer on May 26, 2026 at 7:10 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to format mobile numbers. e.g. +61421 123 123 0421 123 123 0061421123123
I need to find out how to format numbers as strings. My code is
I need to format float value and I only need 2 numbers after point
I need to format numbers so that thousands were not separated by anything and
We have mobile numbers stored in db in format 0789 121 928 We need
I need to use Excel conditional format strings to format numbers in a .Net
I need to format a phone number as one long string of numbers (US
I need to format decimal numbers with patterns in J2ME just like java DecimalFormat
I need to format numbers in a way that they should be preceded with
I need to format a number so that there is a comma seperating the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.