0

I have used Bignumber.js API for performing some JS calculations involving large numbers. I am performing a subtraction where the result should be 0 but the output is like -0.00000000000000000099. How do I counter such issues with Bignumber API.

(new BigNumber(refinedGoal)).minus(monthlyReminder).toFixed()

gives -0.00000000000000000099

while using arithmetic subtraction operator

((new BigNumber(refinedGoal)) - monthlyReminder).toFixed()

gives 0. My overall goal is to save this result 0 in some JSON variable.

Edit 1: The monthlyReminder.toFixed() value is 1030.19999999999999999886 and refinedGoal value is 1030.2

nraina
  • 324
  • 2
  • 20
  • What are the values of `refinedGoal` and `monthlyReminder` when you get that result? Because `(new BigNumber(0.5)).minus(0.2).toFixed()` is returning `0.3` as expected. – sgarza62 Oct 07 '20 at 16:22
  • Have you defined `refinedGoal` and `monthlyReminder` by specifying type `number` to `BigNumber` constructor? If yes specify them in `string` type instead of `number` to avoid loss of precision. – adzo261 Oct 07 '20 at 16:24
  • @sgarza62 I have added the respective values in the question. – nraina Oct 07 '20 at 16:28
  • @adzo261 yes `monthlyReminder` is a Bignumber object while `refinedGoal` is a JS native number – nraina Oct 07 '20 at 16:29
  • 1
    Try defining your variables in string for e.g: `new BigNumber('1119.234')` or if you want from a variable which is a javascript number then - `new BigNumber(variable.toString())`. – adzo261 Oct 07 '20 at 16:31

0 Answers0