-
Hey All, I am trying to figure out the best way to do math on numbers that have different precision. There's a contract I interact with that returns
In python or an app that has big number support I would do something like balance * price / 10 ** decimals With BigNumber doing this I lose significant figures if I do something similar. Also in JS I am doing floating point math which is bad... The example I have is a How would you get the most precise value of the pool using ethers BigNumber. I have tried using parseUnits(balance.toString(), decimals).mul(price) But that number is wrong... I feel like I am just throwing things against the wall, including using Any ideas would be helpful |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
If you only care about the truncate whole value, the math should work fine, You can also use the ethers.FixedNumber class to work things through and see where the fractional bits are. |
Beta Was this translation helpful? Give feedback.
-
I want to add my partial solution but I was hoping there is a more elegant way with ethers Which is simpler in formatUnits(balance.mul(price), (18+decimal)) EDIT: If you use darkmode you cannot see the formula... Bummer |
Beta Was this translation helpful? Give feedback.
If you only care about the truncate whole value, the math should work fine,
balance.mul(price).div(ethers.constants.WeiPerEther)
. The order is important. Are you sure you want to divide though? If you do unit analysis, are you targeting your price in ether or in wei?You can also use the ethers.FixedNumber class to work things through and see where the fractional bits are.