Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: NumberFormatter - Thousand seperator and decimals #58

Open
darronschall opened this issue Jul 28, 2010 · 6 comments
Open

Comments

@darronschall
Copy link
Contributor

Originally filed by madsbsstage on 2008-07-03T09:17:45

addThousandSeperator(123456789, true) would return 123.456.789
addThousandSeperator(123456789, false) would return 123,456,789

formatDecimals(123.456789, 2, true) would return 123,46
formatDecimals(123.456789, 2, false) would return 123.46

@darronschall
Copy link
Contributor Author

Updated by mikechambers on 2008-07-03T16:30:28

Anyone interested in contributing this?

Removed label Type-Defect
Added label Type-Enhancement
Original ticket set status to Accepted (we converted to open)

@darronschall
Copy link
Contributor Author

Updated by trimediapro on 2008-07-05T15:03:26

I sent you an email Mike.

@darronschall
Copy link
Contributor Author

Updated by trimediapro on 2008-07-07T01:52:11

Here's what I put together.

I have add in 3 static functions to NumberFormatter
addSeparator(p_num:Number, p_separator:String):String
formatDecimal(p_num:Number, p_decimal:String=".", p_decimalPlaces:Number=0):String
format(p_num:Number, p_separator:String, p_decimalPlaces:Number=0,
p_decimal:String="."):String

Here are some examples and a basic explanation of all 3.

The "addSeparator" function is for adding a "thousands separator" into a number.
This function will return the same number (as a String) but with the new thousands
separator that was specified.
// Sample Calls
NumberFormatter.addSeparator(-12198.12345, ","); // output -- -12,198.12345
NumberFormatter.addSeparator(1000000.00, " "); // output -- 1 000 000.00

The "formatDecimal" function is for doing just that.
This function will return the same number (as a String) but with the new decimal
character that was specified.
// Sample Calls
NumberFormatter.formatDecimal(0.1983567, ","); // output -- 0,1983567
NumberFormatter.formatDecimal(198.3567, ","); // output -- 198,3567

The "format" function of the NumberFormatter is much more versatile. You can get the
same results as addSeparator and formatDecimal as well as rounding to n number of
decimal places all in a single call.

NumberFormatter.format(p_num:Number, p_separator:String, p_decimalPlaces:Number=0,
p_decimal:String="."):String
@param p_num is the number to work on
@param p_separator is the character to use as the thousands separator
@param p_decimalPlaces is the number of decimal places to round the number to
@param p_decimal is the character to use as the decimal

// Sample Calls
NumberFormatter.format(0.123456789, " ", 5, ","); // output -- 0,12346
NumberFormatter.format(-123456789, ",", 5, "_"); // output -- -
123,456,789_00000
NumberFormatter.format(123456789, "-", 0, ","); // output -- 123-456-789
NumberFormatter.format(123456789, "-"); // output -- 123-456-789

If just the thousands separator needs to be added
NumberFormatter.format(-123456789, ","); // output -- -123,456,789

If just the decimal needs to change
When p_decimalPlaces is set to -1 (negative one) the result is the same as calling
the formatDecimal function. The same number is returned with no rounding and just
the decimal character has been modified.
NumberFormatter.format(0.123456789, "", -1, ","); // output -- 0,123456789

If the decimal needs to change and the number is rounded to n decimal places
NumberFormatter.format(0.123456789, "", 5, ","); // output -- 0,12346
NumberFormatter.format(-123456789, "", 2, ","); // output -- -123456789,00

If the thousands separator needs to be added and the decimal needs to change
NumberFormatter.format(12345.6789, " ", -1, ","); // output -- 12 345,68

If the thousands separator needs to be added and the decimal needs to change and the
number must be rounded to n decimal places
NumberFormatter.format(123456789, " ", 2, ","); // output -- 123 456 789,00
NumberFormatter.format(12345.6789, " ", 2, ","); // output -- 12 345,68

All of the functions should work with positive and negative numbers.
I think that about covers it.

I'm fairly certain that parts of the format function could be optimized. I was just
too lazy to figure out the RegExp calls. If anyone else would like to do some
optimizing that would be great.

I have attached the updated NumberFormatter.as file for submission into corelib.

...Neil Madsen

@darronschall
Copy link
Contributor Author

Updated by trimediapro on 2008-07-08T05:37:40

I found a bug in the previous file when rounding a number smaller than 0.1
ie. 0.01 or 0.001 or 0.0001 or 0.0052367 etc.
I've made some changes to fix the bug.

Here's the newer version of the NumberFormatter.as file.

...Neil

@darronschall
Copy link
Contributor Author

Updated by mikechambers on 2008-07-08T15:57:04

I put together a test case to check some edge cases for the addSeperator API, and the
class is failing some of them:

    public function testAddSeperator():void
    {
        assertTrue('NumberFormatter.addSeparator(1000, ",") == "1,000"',
                NumberFormatter.addSeparator(1000, ",") == "1,000");

        assertTrue('NumberFormatter.addSeparator(90, ",") == "90"',
                NumberFormatter.addSeparator(90, ",") == "90"); 

        //fail
        assertTrue('NumberFormatter.addSeparator(90.00, ",") == "90.00"',
                NumberFormatter.addSeparator(90.00, ",") == "90.00");   

        //fail
        assertTrue('NumberFormatter.addSeparator(90.0, ",") == "90.0"',
                NumberFormatter.addSeparator(90.0, ",") == "90.0");     

        //fail
        assertTrue('NumberFormatter.addSeparator(1000.00, ",") == "1,000.00"',
                NumberFormatter.addSeparator(1000.00, ",") == "1,000.00");              

        //fail
        assertTrue('NumberFormatter.addSeparator(1000.0, ",") == "1,000.0"',
                NumberFormatter.addSeparator(1000.0, ",") == "1,000.0");        

        //fail
        assertTrue('NumberFormatter.addSeparator(1000, ".") == "1.000"',
                NumberFormatter.addSeparator(1000, ".") == "1.000");    

        assertTrue('NumberFormatter.addSeparator(123456789, ",") == "123,456,789"',
                NumberFormatter.addSeparator(123456789, ",") == "123,456,789");

        assertTrue('NumberFormatter.addSeparator(123456789, "XXX") == "123XXX456XXX789"',
                NumberFormatter.addSeparator(123456789, "XXX") == "123XXX456XXX789");

        //fail : causes infinite loop       
        assertTrue('NumberFormatter.addSeparator(123456789, "1") == "12314561789"',
                NumberFormatter.addSeparator(123456789, "1") == "12314561789");
    }

@darronschall
Copy link
Contributor Author

Updated by trimediapro on 2008-07-09T03:28:52

Hi Mike...
Thanks for running the tests.
Here's a new version with the fixes.

...Neil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant