Replies: 1 comment
-
The BigNumber class implements integer maths, which implies truncation. There are many ways to round, depending on the goal. The FixedNumber class offers the round-to-infinity operation (via For other rounding strategies, such as round-to-parity, you will need to implement that yourself. For blockchain purposes, you almost always want truncation (which is what BigNumber implements), as that is what the EVM performs. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Ethers Version
5.5.2
Search Terms
rounding, round, remainder, cut off
Describe the Problem
When using
BigNumber.div()
, decimals are always cut off instead of rounded. This leads to rounding errors in many cases and when using libraries like https://github.com/paulrberg/prb-math that use proper rounding it causes conflicting results.Ethers.js relies on BN.js for BigNumber math and there have been issues created regarding this:
indutny/bn.js#290
However, there has been very little activity around it, and the only other similar issue has been stale for 4+ years. Alternatively this could be solved within the BigNumber implementation by checking for the number in the first decimal place and rounding accordingly, similar to the code example I have provided.
Code Snippet
Beta Was this translation helpful? Give feedback.
All reactions