I'm confused about how to manage/calculate with WEI in a web3 application.

So far I found:

  • BigInt: This is JS native and it works with large quantities (e.g. 10000000000000000000000). But it doesn't have out of the box utilities like e.g. converting to hex.

  • BigNumber: This is provided by ethers:

Many operations in Ethereum operation on numbers which are outside the range of safe values to use in JavaScript. A BigNumber is an object which safely allows mathematic operations on numbers of any magnitude.

Given this description, and that it's dedicated to Ethereum, I thought this must be it, but it actually doesn't work with large numbers:

// Numbers outside the safe range fail:
// Error: overflow (fault="overflow", operation="BigNumber.from", value=9007199254740991, code=NUMERIC_FAULT, version=bignumber/5.0.8)

Aside of that, for WEI I probably need integers, not floating point.

So what's the recommended way to deal with WEI?

In the ideal case I'd like something that not only represent big numbers but actual WEIs and has built in functionality to convert safely to and calculate with GWEI or ETH out of the box, but this is not a must.

Using Typescript, but it probably isn't relevant.

Edit: I found another library: BigNumber.js. This seems to do what I expect. It can be converted to string to interact with web3.js.

1 Answer 1


BigNumber is the recommended way to go. Probably it has much better support than various BigInt libraries.

There are lots of BigNumber libraries out there and I guess most of them provide very similar functionality. So probably almost any of them will work just fine for you.

At least ethers.js has a built-in BigNumber support so no need to look any further if you happen to use that library.

When you have your BigNumber library you can get regular token amounts with something like this (taken from one of my ERC20 token deployment scripts):

var exp = ethers.BigNumber.from("10").pow(18);
const supply = ethers.BigNumber.from("50").mul(exp);
  • 2
    Hmm did you read my complete post? ether's BigNumber (weirdly) seems not to work with big numbers: BigNumber.from(Number.MAX_SAFE_INTEGER) leads to overflow. What am I missing?
    – User
    Nov 22, 2020 at 21:57
  • I've had some issues with using "big numbers" directly with BigNumber as well, so the safest is to just create the big numbers yourself like I showed Nov 23, 2020 at 7:05
  • 1
    Thanks! Using web3.utils.toBN() instead of parseInt AND using the bn.sum, .sub, .mul methods did the trick. Jul 1, 2021 at 9:01

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.