Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions accounting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 : "."
}
Expand Down Expand Up @@ -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);
});
}

Expand All @@ -245,7 +247,8 @@
(isObject(precision) ? precision : {
precision : precision,
thousand : thousand,
decimal : decimal
decimal : decimal,
grouping: grouping
}),
lib.settings.number
),
Expand All @@ -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
Expand All @@ -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);
});
}

Expand All @@ -292,7 +313,8 @@
precision : precision,
thousand : thousand,
decimal : decimal,
format : format
format : format,
grouping: grouping
}),
lib.settings.currency
),
Expand All @@ -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));
};


Expand Down Expand Up @@ -410,4 +432,4 @@
}

// Root will be `window` in browser or `global` on the server:
}(this));
}(this));
15 changes: 12 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h2>Library Methods</h2>

<h4><strong>formatMoney()</strong> - format any number into currency</h4>

<p>The most basic library function for formatting numbers as money values, with customisable currency symbol, precision (decimal places), and thousand/decimal separators:</p>
<p>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)</p>
<pre class="prettyprint lang-js">// Default usage:
accounting.formatMoney(12345678); // $12,345,678.00

Expand All @@ -55,7 +55,9 @@ <h4><strong>formatMoney()</strong> - format any number into currency</h4>
accounting.formatMoney(-500000, "&pound; ", 0); // &pound; -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</pre>
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</pre>


<h4><strong>formatColumn()</strong> - format a list of values for column-display</h4>
Expand Down Expand Up @@ -84,6 +86,13 @@ <h4><strong>formatNumber()</strong> - format a number with custom precision and
<p>The base function of the library, which takes any number or array of numbers, runs <code>accounting.unformat()</code> to remove any formatting, and returns the number(s) formatted with separated thousands and custom precision:</p>
<pre class="prettyprint lang-js">accounting.formatNumber(5318008); // 5,318,008
accounting.formatNumber(9876543.21, 3, " "); // 9 876 543.210</pre>
<p>formatNumber() also support grouping of output in 'Indian' and 'Arabic' (default) number sytems. Output can be grouped in groups of custom size.</p>
<pre class="prettyprint lang-js">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
</pre>


<h4><strong>toFixed()</strong> - better rounding for floating point numbers</h4>
Expand Down Expand Up @@ -470,4 +479,4 @@ <h2>Links</h2>
})();
</script>
</body>
</html>
</html>
16 changes: 13 additions & 3 deletions tests/qunit/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '.';
});

Expand All @@ -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')

Expand All @@ -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");

Expand Down Expand Up @@ -94,4 +104,4 @@ $(document).ready(function() {

});

});
});