Konubinix' opinionated web of thoughts

Don't Store Ethers or Wei as Simple Integer or Floats

Fleeting

ethereum, floating-point, javascript integers and precision

Why can’t I just use numbers?

The first problem many encounter when dealing with Ethereum is the concept of numbers. Most common currencies are broken down with very little granularity. For example, there are only 100 cents in a single dollar. However, there are 1018 wei in a single ether.

JavaScript uses IEEE 754 double-precision binary floating point numbers to represent numeric values. As a result, there are holes in the integer set after 9,007,199,254,740,991; which is problematic for Ethereum because that is only around 0.009 ether (in wei), which means any value over that will begin to experience rounding errors.

To demonstrate how this may be an issue in your code, consider:

https://docs.ethers.org/v5/api/utils/bignumber/