Skip to content

Commit

Permalink
allow fractional cent values when using fromCents option
Browse files Browse the repository at this point in the history
  • Loading branch information
scurker committed Aug 5, 2020
1 parent 205ac48 commit bde1acf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 35 deletions.
7 changes: 2 additions & 5 deletions src/currency.js
Expand Up @@ -80,15 +80,12 @@ function parse(value, opts, useRounding = true) {
v = 0;
}

if (fromCents) {
v = Math.trunc(v); // Remove decimals. Invalid for cents.
} else {
if (!fromCents) {
v *= precision; // scale number to integer value
v = v.toFixed(4); // Handle additional decimal for proper rounding.
v = useRounding ? round(v) : v;
}

return v;
return useRounding ? round(v) : v;
}

/**
Expand Down
37 changes: 7 additions & 30 deletions test/test.js
Expand Up @@ -487,13 +487,10 @@ test('should allow creation from cents', t => {
let c3 = value => currency(value, { fromCents: true, precision: 3 });

t.is(c1(500).toString(), '5.00', 'value is not parsed from cents to 5.00');
t.is(c1(500.678).toString(), '5.00', 'value does not truncate decimals when parsed from cents');
t.is(c1('455').toString(), '4.55', 'value is not parsed from a string to cents');
t.is(c2(500).toString(), '500', 'value is not parsed from cents to 5.00');
t.is(c2(500.678).toString(), '500', 'value does not truncate decimals when parsed from cents');
t.is(c2('455').toString(), '455', 'value is not parsed from a string to cents');
t.is(c3(500).toString(), '0.500', 'value is not parsed from cents to 5.00');
t.is(c3(500.678).toString(), '0.500', 'value does not truncate decimals when parsed from cents');
t.is(c3('455').toString(), '0.455', 'value is not parsed from a string to cents');
});

Expand All @@ -510,19 +507,6 @@ test('should parse cents from a number when using fromCents option', t => {
t.is(c3.intValue, 123);
});

test('should truncate decimals from a number when using fromCents option', t => {
let c1 = currency(123.34, { fromCents: true });
let c2 = currency(123.12, { fromCents: true, precision: 0 });
let c3 = currency(123.987, { fromCents: true, precision: 3 });

t.is(c1.value, 1.23);
t.is(c1.intValue, 123);
t.is(c2.value, 123);
t.is(c2.intValue, 123);
t.is(c3.value, 0.123);
t.is(c3.intValue, 123);
});

test('should parse cents from a string when using fromCents option', t => {
let c1 = currency('123', { fromCents: true });
let c2 = currency('123', { fromCents: true, precision: 0 });
Expand All @@ -536,19 +520,6 @@ test('should parse cents from a string when using fromCents option', t => {
t.is(c3.intValue, 123);
});

test('should truncate decimals from a string when using fromCents option', t => {
let c1 = currency('123.34', { fromCents: true });
let c2 = currency('123.12', { fromCents: true, precision: 0 });
let c3 = currency('123.987', { fromCents: true, precision: 3 });

t.is(c1.value, 1.23);
t.is(c1.intValue, 123);
t.is(c2.value, 123);
t.is(c2.intValue, 123);
t.is(c3.value, 0.123);
t.is(c3.intValue, 123);
});

test('should handle add with fromCents option', t => {
let c1 = currency(12345, { fromCents: true });
let c2 = currency(123, { fromCents: true });
Expand All @@ -572,10 +543,16 @@ test('should handle multiply with fromCents option', t => {

test('should handle divide with fromCents option', t => {
let c1 = currency(12345, { fromCents: true });
t.is(c1.divide(2).value, 61.72);
t.is(c1.divide(2).value, 61.73);
});

test('should handle distribute with fromCents option', t => {
var values = currency(100, { fromCents: true }).distribute(4);
t.deepEqual(values.map(v => v.value), [.25, .25, .25, .25]);
});

test('should handle fractional cents', t => {
var values = currency(1234.56, { fromCents: true });
t.is(values.intValue, 1235);
t.is(values.value, 12.35);
});

0 comments on commit bde1acf

Please sign in to comment.