Skip to content

Commit

Permalink
Merge pull request #34 from tastypackets/develop
Browse files Browse the repository at this point in the history
Add default rounder to fromDecimal
  • Loading branch information
davidkalosi authored Oct 25, 2017
2 parents d2134fd + 0151c0b commit 10a4870
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/money.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ Money.fromDecimal = function (amount, currency, rounder) {
if (decimals > currency.decimal_digits)
throw new Error("The currency " + currency.code + " supports only "
+ currency.decimal_digits + " decimal digits");

rounder = Math.round;
} else {
if (['round', 'floor', 'ceil'].indexOf(rounder) === -1 && typeof rounder !== 'function')
throw new TypeError('Invalid parameter rounder');
Expand All @@ -116,8 +118,7 @@ Money.fromDecimal = function (amount, currency, rounder) {
var precisionMultiplier = Math.pow(10, currency.decimal_digits);
var resultAmount = amount * precisionMultiplier;

if (rounder)
resultAmount = rounder(resultAmount);
resultAmount = rounder(resultAmount);

return new Money(resultAmount, currency);
};
Expand Down
4 changes: 4 additions & 0 deletions test/money.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ describe('Money', function () {
it('should create a new instance from decimal string using `.fromDecimal()`', function () {
var money = Money.fromDecimal('10.01', Money.EUR);
var money1 = Money.fromDecimal('10', Money.EUR);
var money3 = Money.fromDecimal(16.90, Money.EUR);
var money4 = Money.fromDecimal(16.95, Money.EUR);

expect(money.amount).to.equal(1001);
expect(money1.amount).to.equal(1000);
expect(money3.amount).to.equal(1690);
expect(money4.amount).to.equal(1695);
});

it('should not create a new instance from decimal using `.fromDecimal()` if too many decimal places', function () {
Expand Down

0 comments on commit 10a4870

Please sign in to comment.