Why is BigNumber with etherjs returning 0

5.2k views Asked by At

I am using BigNumber from etherjs and I am doing this very simple operation:

const cost = BigNumber.from(3)
    .div(BigNumber.from(1000))
    .mul(BigNumber.from(3));

  console.log(cost.toString());

It outputs '0', but should be 0.09.

Why? What am I doing wrong?

2

There are 2 answers

0
T.J. Crowder On

As far as I can tell from the documentation, Ethers' BigNumber only handles big integers. Nothing in the documentation mentions fractional values or precision or scale, all of which you'd expect to see in a library handling fractional values. Separately, the documentation mentions it currently uses BN.js in its implementation of BigNumber. BN.js doesn't handle fractional values. From its documentation:

Note: decimals are not supported in this library.

That's not terribly clear (most of the examples are in decimal), they probably meant fractional values aren't supported.

A couple of options for you:

  1. You could work in units that are a multiple of the precision you need. For instance, if you need two digits of precision to the right of the decimal point, you'd work with values that are multiplied by 100. Then divide by 100 to get the whole portion for output, and do a remainder operation on 100 to get the fractional portion for output (formatting with a leading 0 if needed).

  2. There are several libraries for big numbers that handle fractional values, such as bignumber and big.js.

0
Safeer Khan On

Ensure that you're passing the correct input to the BigNumber instance. If the input is invalid or not as expected, it might result in 0.