1

So I have this for calculating percentage change between two BigNumbers:

const percentageChange = (a: BigNumber, b: BigNumber) => b.sub(a).div(a).mul(BigNumber.from('100'))

Now the issue is when I call toString() or toNumber() on it I always get 0.

The a and b are numbers in wei and are very close together, so percentage should be something like 0.000021 or some such.

How do I calculate the percentage and get JS-native type without rounding it to zero?

1 Answer 1

0

BigNumbers don't accept decimal points, it means that when doing

b.sub(a).div(a) 
if b.sub(a) < a will always return 0

I would suggest you multiply for a factor for instance 1e6 a and b before doing the operations and once you have your percentage*1e6 as number, you can divided by 1e6 and multiply by 100

Hope it helps

Your Answer

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

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