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

Add choice of rounding method for precision #173

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
55 changes: 36 additions & 19 deletions accounting.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
decimal : ".", // decimal point separator
thousand : ",", // thousands separator
precision : 2, // decimal places
grouping : 3 // digit grouping (not implemented yet)
grouping : 3, // digit grouping (not implemented yet)
round : 0
},
number: {
precision : 0, // default precision on numbers is 0
grouping : 3, // digit grouping (not implemented yet)
thousand : ",",
decimal : "."
decimal : ".",
round : 0
}
};

Expand Down Expand Up @@ -169,7 +171,7 @@
* Alias: `accounting.parse(string)`
*
* Decimal must be included in the regular expression to match floats (defaults to
* accounting.settings.number.decimal), so if the number uses a non-standard decimal
* accounting.settings.number.decimal), so if the number uses a non-standard decimal
* separator, provide it as the second argument.
*
* Also matches bracketed negatives (eg. "$ (1.99)" => -1.99)
Expand Down Expand Up @@ -208,16 +210,28 @@


/**
* Implementation of toFixed() that treats floats more like decimals
* Implementation of xed() that treats floats more like decimals
*
* Fixes binary rounding issues (eg. (0.615).toFixed(2) === "0.61") that present
* Fixes binary rounding issues (eg. (0.615).xed(2) === "0.61") that present
* problems for accounting- and finance-related software.
*/
var toFixed = lib.toFixed = function(value, precision) {
var toFixed = lib.toFixed = function(value, precision, round) {
precision = checkPrecision(precision, lib.settings.number.precision);

var exponentialForm = Number(lib.unformat(value) + 'e' + precision);
var rounded = Math.round(exponentialForm);
var roundMethod

if (round > 0) {
roundMethod = Math.ceil
}
else if (round < 0) {
roundMethod = Math.floor
}
else {
roundMethod = Math.round
}

var rounded = roundMethod(exponentialForm);
var finalResult = Number(rounded + 'e-' + precision).toFixed(precision);
return finalResult;
};
Expand All @@ -230,11 +244,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, round) {
// Resursively format arrays:
if (isArray(number)) {
return map(number, function(val) {
return formatNumber(val, precision, thousand, decimal);
return formatNumber(val, precision, thousand, decimal,round);
});
}

Expand All @@ -246,7 +260,8 @@
(isObject(precision) ? precision : {
precision : precision,
thousand : thousand,
decimal : decimal
decimal : decimal,
round : round
}),
lib.settings.number
),
Expand All @@ -256,11 +271,11 @@

// Do some calc:
negative = number < 0 ? "-" : "",
base = parseInt(toFixed(Math.abs(number || 0), usePrecision), 10) + "",
base = parseInt(toFixed(Math.abs(number || 0), usePrecision, opts.round), 10) + "",
mod = base.length > 3 ? base.length % 3 : 0;

// 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 + (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, opts.round).split('.')[1] : "");
};


Expand All @@ -275,11 +290,11 @@
*
* 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, round) {
// 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, round);
});
}

Expand All @@ -293,7 +308,8 @@
precision : precision,
thousand : thousand,
decimal : decimal,
format : format
format : format,
round : round
}),
lib.settings.currency
),
Expand All @@ -305,7 +321,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.round));
};


Expand All @@ -321,7 +337,7 @@
* NB: `white-space:pre` CSS rule is required on the list container to prevent
* browsers from collapsing the whitespace in the output strings.
*/
lib.formatColumn = function(list, symbol, precision, thousand, decimal, format) {
lib.formatColumn = function(list, symbol, precision, thousand, decimal, format, round) {
if (!list || !isArray(list)) return [];

// Build options object from second param (if object) or all params, extending defaults:
Expand All @@ -331,7 +347,8 @@
precision : precision,
thousand : thousand,
decimal : decimal,
format : format
format : format,
round: round
}),
lib.settings.currency
),
Expand All @@ -358,7 +375,7 @@
var useFormat = val > 0 ? formats.pos : val < 0 ? formats.neg : formats.zero,

// Format this value, push into formatted list and save the length:
fVal = useFormat.replace('%s', opts.symbol).replace('%v', formatNumber(Math.abs(val), checkPrecision(opts.precision), opts.thousand, opts.decimal));
fVal = useFormat.replace('%s', opts.symbol).replace('%v', formatNumber(Math.abs(val), checkPrecision(opts.precision), opts.thousand, opts.decimal, opts.round));

if (fVal.length > maxLength) maxLength = fVal.length;
return fVal;
Expand Down
2 changes: 1 addition & 1 deletion accounting.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.