ericelliott/moneysafe

Bug when calculating with currency with 0 decimals

Opened this issue · 0 comments

I am using moneysafe in my code to calculate prices of orders.
Currently, I am testing edge cases and therefore tested my code with a currency that has 0 decimals.
However, the percentage calculations and the multiply function don't behave as expected.

Here is my code:

 // Create currency
 const money = createCurrency({
    decimals: 0
  });
  
// Add Percent
console.log($$(money(100), addPercent(13)).toNumber()); // expect: 113 // result: 100

// Subtract Percent
console.log($$(money(100), subtractPercent(13)).toNumber()); // expect: 87 // result: 100

// Multiply by decimal
console.log(money(100).multipliedBy(2.5).toNumber()); // expect: 250 // result: 300

console.log(money(100).multipliedBy(0.13).toNumber()); // expect: 13 // result: 0

Here is a link to a sandbox:
https://codesandbox.io/p/sandbox/determined-dust-2dyhdm

You can see, that the calculations work as expected once you set the currency decimals to 2.