'adding a percentage to a big integer [closed]
I'm making a cookie clicker game where i recently converted the counter from a number primitive to Big-int and i have faced a problem where i need to add a percentage onto a Big-int. Normally if i would like to add 50% of 1000 to 1000 it would be as easy as multiplying 1000 by 1.5 but you cannot do that with Int's. I am open to ideas on how to solve this issue, any bit helps. What I was originally thinking is that I divide it by 100 and then multiply it by 150 which would give the same results, but the problem is that if the original was below 100 then the fact that it is an int would mess this process up.
Solution 1:[1]
You can do that with bigInts too:
const bigIntNum = BigInt(123456);
const multipledByTwo = bigIntNum * 2n;
console.log(multipledByTwo);
But do make sure the number that you are calculating by multiplying should be in the range of bigInt.
To add some percent to the bigInt number, you should write the following code:
const bigIntNum = BigInt(123456);
//adding 50%
const addedFiftyPercent = bigIntNum + (bigIntNum * 50n) / 100n;
//adding x%
const addedXPercent = bigIntNum + (bigIntNum * xn) / 100n;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
