I have 2 numbers:
const a = 61921447244562694144000n;
const b = 93068664972055293198336n;
and I'm trying to find a percentage change between them.
So far I tried 3 methods - using just numbers, using BigInts and JSBI.
const percentageChange = ((a - b ) / b) * 100n);
This method is not working because this calculation with BigInts will always give just once digit due the limitations with decimals - BigInt always rounds up the number.
With just numbers I got the answer, but it was incorrect (guess because of the large numbers)
JSBI giving many different errors, but I still can't wrap my head arround it:
let a = JSBI.subtract(oldValue, newValue);
let b = JSBI.divide(a, oldValue);
jsbi errors such as:
TypeError: _.__digit is not a function
or
Error: Convert JSBI instances to native numbers using `toNumber`.
You can change the order of operations:
const a = 61921447244562694144000n;
const b = 93068664972055293198336n;
const percentageChange = (a - b ) * 100n / b;
console.log(percentageChange.toString());
console.log(Number(percentageChange));
You can add even more digits:
const a = 61921447244562694144000n;
const b = 93068664972055293198336n;
const percentageChange = (a - b ) * 1000n / b;
console.log(Number(percentageChange) / 10);