diff --git a/accounting.js b/accounting.js index 755a3d5..a11c127 100644 --- a/accounting.js +++ b/accounting.js @@ -31,11 +31,13 @@ decimal : ".", // decimal point separator thousand : ",", // thousands separator precision : 2, // decimal places - grouping : 3 // digit grouping (not implemented yet) + grouping : "Arabic" // 1 | 2 | 3 ... | 'Indian', 'Arabic' ; Defaults to 'Arabic' + // Indian => 1,24,33,242 Arabic => 12,433,242 }, number: { precision : 0, // default precision on numbers is 0 - grouping : 3, // digit grouping (not implemented yet) + grouping : "Arabic", // 1 | 2 | 3 ... | 'Indian' | 'Arabic' ; Defaults to 'Arabic' + // Indian => 1,24,33,242 Arabic => 12,433,242 thousand : ",", decimal : "." } @@ -229,11 +231,11 @@ * Localise by overriding the precision and thousand / decimal separators * 2nd parameter `precision` can be an object matching `settings.number` */ - var formatNumber = lib.formatNumber = lib.format = function(number, precision, thousand, decimal) { + var formatNumber = lib.formatNumber = lib.format = function(number, precision, thousand, decimal, grouping) { // Resursively format arrays: if (isArray(number)) { return map(number, function(val) { - return formatNumber(val, precision, thousand, decimal); + return formatNumber(val, precision, thousand, decimal, grouping); }); } @@ -245,7 +247,8 @@ (isObject(precision) ? precision : { precision : precision, thousand : thousand, - decimal : decimal + decimal : decimal, + grouping: grouping }), lib.settings.number ), @@ -255,13 +258,30 @@ // Do some calc: negative = number < 0 ? "-" : "", + groupSize = (opts.grouping == "Indian") ? "Indian" : (opts.grouping == "Arabic") ? 3 : parseInt(opts.grouping), base = parseInt(toFixed(Math.abs(number || 0), usePrecision), 10) + "", - mod = base.length > 3 ? base.length % 3 : 0; - + reverseBase = base.split('').reverse(), + retVal = (reverseBase.length <= 3) ? reverseBase : groupNumbers(reverseBase, opts.thousand, groupSize); // Format the number: - return negative + (mod ? base.substr(0, mod) + opts.thousand : "") + base.substr(mod).replace(/(\d{3})(?=\d)/g, "$1" + opts.thousand) + (usePrecision ? opts.decimal + toFixed(Math.abs(number), usePrecision).split('.')[1] : ""); + return negative + retVal.reverse().join('') + (usePrecision ? opts.decimal + toFixed(Math.abs(number), usePrecision).split('.')[1] : ""); }; + function groupNumbers(reverseBase,separator, groupSize){ + separator = separator || lib.settings.number.thousand; + startIndex = 0; + if (groupSize == "Indian") { + reverseBase.splice(3, 0, separator) + groupSize = 2; + startIndex = 4; + } + for (i = startIndex; i <= reverseBase.length; i++ ){ + i += groupSize; + if (reverseBase[i] != undefined){ + reverseBase.splice(i, 0, separator) + } + } + return reverseBase + } /** * Format a number into currency @@ -274,11 +294,12 @@ * * To do: tidy up the parameters */ - var formatMoney = lib.formatMoney = function(number, symbol, precision, thousand, decimal, format) { + var formatMoney = lib.formatMoney = function(number, symbol, precision, thousand, decimal, format, grouping) { + grouping = grouping || 'Arabic' // Resursively format arrays: if (isArray(number)) { return map(number, function(val){ - return formatMoney(val, symbol, precision, thousand, decimal, format); + return formatMoney(val, symbol, precision, thousand, decimal, format, grouping); }); } @@ -292,7 +313,8 @@ precision : precision, thousand : thousand, decimal : decimal, - format : format + format : format, + grouping: grouping }), lib.settings.currency ), @@ -304,7 +326,7 @@ useFormat = number > 0 ? formats.pos : number < 0 ? formats.neg : formats.zero; // Return with currency symbol added: - return useFormat.replace('%s', opts.symbol).replace('%v', formatNumber(Math.abs(number), checkPrecision(opts.precision), opts.thousand, opts.decimal)); + return useFormat.replace('%s', opts.symbol).replace('%v', formatNumber(Math.abs(number), checkPrecision(opts.precision), opts.thousand, opts.decimal, opts.grouping)); }; @@ -410,4 +432,4 @@ } // Root will be `window` in browser or `global` on the server: -}(this)); +}(this)); \ No newline at end of file diff --git a/index.html b/index.html index 2f3b3f4..d6dbcec 100644 --- a/index.html +++ b/index.html @@ -44,7 +44,7 @@

Library Methods

formatMoney() - format any number into currency

-

The most basic library function for formatting numbers as money values, with customisable currency symbol, precision (decimal places), and thousand/decimal separators:

+

The most basic library function for formatting numbers as money values, with customisable currency symbol, precision (decimal places), thousand/decimal separators and grouping paramaeter (same as in formatNumber)

// Default usage:
 accounting.formatMoney(12345678); // $12,345,678.00
 
@@ -55,7 +55,9 @@ 

formatMoney() - format any number into currency

accounting.formatMoney(-500000, "£ ", 0); // £ -500,000 // Simple `format` string allows control of symbol position (%v = value, %s = symbol): -accounting.formatMoney(5318008, { symbol: "GBP", format: "%v %s" }); // 5,318,008.00 GBP
+accounting.formatMoney(5318008, { symbol: "GBP", format: "%v %s" }); // 5,318,008.00 GBP +accounting.formatMoney(5318008, { symbol: "GBP", format: "%v %s", grouping: "Arabic"}); // 5,318,008.00 GBP +accounting.formatMoney(5318008, { symbol: "Rs", format: "%s %v", grouping: "Indian"}); // Rs 53,18,008.00

formatColumn() - format a list of values for column-display

@@ -84,6 +86,13 @@

formatNumber() - format a number with custom precision and

The base function of the library, which takes any number or array of numbers, runs accounting.unformat() to remove any formatting, and returns the number(s) formatted with separated thousands and custom precision:

accounting.formatNumber(5318008); // 5,318,008
 accounting.formatNumber(9876543.21, 3, " "); // 9 876 543.210
+

formatNumber() also support grouping of output in 'Indian' and 'Arabic' (default) number sytems. Output can be grouped in groups of custom size.

+
accounting.formatNumber(5318008, {grouping : "Indian"}); // 53,18,008
+accounting.formatNumber(5318008, {grouping : "Arabic"}); // 5,318,008
+//Custom
+accounting.formatNumber(5318008,{grouping: 2}); // 5,31,80,08
+accounting.formatNumber(5318008,{grouping: 4}); // 531,8008
+		

toFixed() - better rounding for floating point numbers

@@ -470,4 +479,4 @@

Links

})(); - + \ No newline at end of file diff --git a/tests/qunit/methods.js b/tests/qunit/methods.js index 19e354b..516f365 100644 --- a/tests/qunit/methods.js +++ b/tests/qunit/methods.js @@ -10,7 +10,7 @@ $(document).ready(function() { accounting.settings.number.decimal = ','; equals(accounting.unformat("100,00"), 100, 'Uses decimal separator from settings'); - equals(accounting.unformat("¤1.000,00"), 1000, 'Uses decimal separator from settings'); + equals(accounting.unformat("¤1.000,00"), 1000, 'Uses decimal separator from settings'); accounting.settings.number.decimal = '.'; }); @@ -20,6 +20,16 @@ $(document).ready(function() { }); test("accounting.formatNumber()", function() { + //format ussing Indian Number Format + equals(accounting.formatNumber(34243424.435, 2, ",", ".", "Indian"), "3,42,43,424.44", 'format numbers in Indian Number system') + //format ussing Arabic Number Format + equals(accounting.formatNumber(34243424.435, 2, ",", ".", "Arabic"), "34,243,424.44", 'format numbers in Arabic Number system') + //allow for custom grouping + equals(accounting.formatNumber(34243424.435, 2, ",", ".", 4), "3424,3424.44", 'allows for custom grouping') + //allow for custom grouping + equals(accounting.formatNumber(34243424.435, 2, ",", ".", "4"), "3424,3424.44", 'allows for custom grouping') + //picks up grouping value from options object + equals(accounting.formatNumber(34243424.435, {grouping: "Indian"}), "3,42,43,424", 'picks grouping value from options object') // Check custom precision and separators: equals(accounting.formatNumber(4999.99, 2, ".", ","), "4.999,99", 'Custom precision and decimal/thousand separators are a-ok') @@ -45,7 +55,7 @@ $(document).ready(function() { test("accounting.formatMoney()", function() { equals(accounting.formatMoney(12345678), "$12,345,678.00", "Default usage with default parameters is ok"); equals(accounting.formatMoney(4999.99, "$ ", 2, ".", ","), "$ 4.999,99", 'custom formatting via straight params works ok'); - equals(accounting.formatMoney(-500000, "£ ", 0), "£ -500,000", 'negative values, custom params, works ok'); + equals(accounting.formatMoney(-500000, "£ ", 0), "£ -500,000", 'negative values, custom params, works ok'); equals(accounting.formatMoney(5318008, { symbol: "GBP", format: "%v %s" }), "5,318,008.00 GBP", "`format` parameter is observed in string output"); equals(accounting.formatMoney(1000, { format: "test %v 123 %s test" }), "test 1,000.00 123 $ test", "`format` parameter is observed in string output, despite being rather strange"); @@ -94,4 +104,4 @@ $(document).ready(function() { }); -}); +}); \ No newline at end of file