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 @@
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 @@+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.00formatMoney() - 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
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
+