Open GoogleCodeExporter opened 9 years ago
Anyone interested in contributing this?
Original comment by mikechambers
on 3 Jul 2008 at 4:30
I sent you an email Mike.
Original comment by trimedia...@gmail.com
on 5 Jul 2008 at 3:03
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
Original comment by trimedia...@gmail.com
on 7 Jul 2008 at 1:52
Attachments:
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
Original comment by trimedia...@gmail.com
on 8 Jul 2008 at 5:37
Attachments:
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");
}
Original comment by mikechambers
on 8 Jul 2008 at 3:57
Hi Mike...
Thanks for running the tests.
Here's a new version with the fixes.
...Neil
Original comment by trimedia...@gmail.com
on 9 Jul 2008 at 3:28
Attachments:
Original issue reported on code.google.com by
madsbsst...@gmail.com
on 3 Jul 2008 at 9:17