1

I have seen at least three different ways to test two BigNumbers.

  • Using waffle BigNumber
  • Convert toString()
  • Cast to Number

As an example hardhat test with chai and waffle.

const sendValueBN = ethers.utils.parseEther("0.1337")
const storedValueBN = await ethers.provider.getBalance(contract.address)
...
// using waffle
expect(sendValueBN).to.equal(storedValueBN)

// toString()
assert.equal(sendValueBN.toString(), storedValueBN.toString())

// Cast to Number
assert.equal(Number(sendValueBN), Number(storedValueBN))

Can someone help me understand which one is preferred and why?

I ordered them in what I believe is the preferred order (top to bottom).
Casting to Number is worst because i believe it has the risk of precision loss due to BigNumber 1018 and a JavaScript Number is a double. But I am not sure even after reading this.

1 Answer 1

0

the recommended way to compare BigNumber objects is to use the toString() method, because we compare the actual values of the BigNumber objects as strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.