From fb63c504b58895d2ae5afeb15ec49cd4b87a0c56 Mon Sep 17 00:00:00 2001 From: Mike Sweeney Date: Tue, 4 May 2021 09:14:36 -0700 Subject: [PATCH] #132 division by another currency object returns number --- src/currency.js | 11 ++++++++--- test/test.js | 4 ++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/currency.js b/src/currency.js index 1edc89fe..7668252a 100644 --- a/src/currency.js +++ b/src/currency.js @@ -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); }, /** diff --git a/test/test.js b/test/test.js index ce1d38ca..a8d94d75 100644 --- a/test/test.js +++ b/test/test.js @@ -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); }); \ No newline at end of file