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

#132 division by another currency object returns number #309

Open
wants to merge 1 commit into
base: main
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,17 @@ currency.prototype = {

/**
* Divides value.
* @param {number} number
* @param {number|currency} value
* @returns {currency}
*/
divide(number) {
divide(value) {
let { intValue, _settings } = this;
return currency(intValue /= parse(number, _settings, false), _settings);

if (value instanceof currency) {
return intValue / parse(value, _settings, false);
}

return currency(intValue /= parse(value, _settings, false), _settings);
},

/**
Expand Down
4 changes: 4 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,4 +555,8 @@ test('should handle fractional cents', t => {
var values = currency(1234.56, { fromCents: true });
t.is(values.intValue, 1235);
t.is(values.value, 12.35);
});

test('division by currency should result in a number, not currency', t => {
t.is(currency(110).divide(currency(20)), 5.5);
});