Skip to content

Commit

Permalink
ensure that rounding rounds half up (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
scurker committed Apr 9, 2018
1 parent 212624c commit 347bd3c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
23 changes: 10 additions & 13 deletions src/currency.js
Expand Up @@ -53,22 +53,19 @@ function currency(value, opts) {
function parse(value, opts, useRounding = true) {
let v = 0
, { decimal, errorOnInvalid, precision: decimals } = opts
, precision = pow(decimals);
, precision = pow(decimals)
, isNumber = typeof value === 'number';

if (typeof value === 'number') {
v = value * precision;
} else if (value instanceof currency) {
v = value.value * precision;
} else if (typeof(value) === 'string') {
if (isNumber || value instanceof currency) {
v = ((isNumber ? value : value.value) * precision).toFixed(1);
} else if (typeof value === 'string') {
let regex = new RegExp('[^-\\d' + decimal + ']', 'g')
, decimalString = new RegExp('\\' + decimal, 'g');
v = parseFloat(
value
.replace(/\((.*)\)/, '-$1') // allow negative e.g. (1.99)
.replace(regex, '') // replace any non numeric values
.replace(decimalString, '.') // convert any decimal values
* precision // scale number to integer value
);
v = value
.replace(/\((.*)\)/, '-$1') // allow negative e.g. (1.99)
.replace(regex, '') // replace any non numeric values
.replace(decimalString, '.') // convert any decimal values
* precision; // scale number to integer value
v = v || 0;
} else {
if(errorOnInvalid) {
Expand Down
22 changes: 21 additions & 1 deletion test/test.js
Expand Up @@ -79,6 +79,26 @@ test('should subtract floating point', t => {
t.not(parseFloat(value), 2.52-.01, 'currency(2.52).subtract(.01) does not equal 2.5100000000000002');
});

test('should round half up', t => {
var value1 = currency(17.955)
, value2 = currency(17.855)
, value3 = currency(17.455);

t.is(value1.value, 17.96, 'currency(17.955) rounds half up to 17.96');
t.is(value2.value, 17.86, 'currency(17.855) rounds half up to 17.86');
t.is(value3.value, 17.46, 'currency(17.955) rounds half up to 17.46');
});

test('should round negative values half up', t => {
var value1 = currency(-17.955)
, value2 = currency(-17.855)
, value3 = currency(-17.455);

t.is(value1.value, -17.95, 'currency(-17.955) rounds half up to -17.95');
t.is(value2.value, -17.85, 'currency(-17.855) rounds half up to -17.85');
t.is(value3.value, -17.45, 'currency(-17.955) rounds half up to -17.45');
});

test('currency multiplication', t => {
var value = currency(1.23).multiply(2);
var floatingValue = currency(.1).multiply(.2);
Expand All @@ -95,7 +115,7 @@ test('currency multiplication with precision', t => {

test('currency division', t => {
var value = currency(9.87).divide(2);
t.is(parseFloat(value), 4.93, 'currency(9.87).divide(2) is 4.93');
t.is(parseFloat(value), 4.94, 'currency(9.87).divide(2) is 4.94');
});

test('currency division with precision', t => {
Expand Down

0 comments on commit 347bd3c

Please sign in to comment.