I am trying to make a cookie clicker type game for fun and I ran into a problem. As you get into bigger numbers in a lot of games, cookie clicker for example, the counter doesn't just have a 20 digit number up there, instead it takes the multiple of a thousand i.e.(million, billion, trillion) and so on. I was wondering how I would make this possible so that it can display a drastically high number while still being user friendly and still have the possible math operations applied to it. An important note is that when it passes 1000 of the certain suffix then it rolls over to the next, on the other hand if it goes underneath 1 then it rolls back a multiple of 1000. If anyone has an idea it would be really helpful.
-
5BigIntBravo– Bravo2022-03-29 23:06:33 +00:00Commented Mar 29, 2022 at 23:06
-
You could probably do it with regular old javascript Numbers too, if you keep track of the multiplier separately.James– James2022-03-29 23:12:33 +00:00Commented Mar 29, 2022 at 23:12
-
do i need to import (BigInt) or is it just normally in java scriptYolo2116– Yolo21162022-03-29 23:25:46 +00:00Commented Mar 29, 2022 at 23:25
Add a comment
|
1 Answer
Here's an example using regular javascript numbers. This outputs a number less than a thousand plus a symbol
const map = ["", "K", "M", "B", "T", "Quad", "Quint", "Sext", "Sept", "Oct", "Nov", "Dec"];
const outputAsCookies = (number) => {
if (number < 1000) return number.toString();
let log = Math.log10(number);
let div = log - log % 3;
let index = div / 3;
while (index >= map.length) {
// ran out of map elements
index -= 1;
div -= 3;
}
return (number / Math.pow(10, div)).toPrecision(6) + " " + map[index];
};
console.log(outputAsCookies(100));
console.log(outputAsCookies(1000));
console.log(outputAsCookies(98765432109876));
console.log(outputAsCookies(3.1415926535e32));
3 Comments
Yogi
I think 10E24 is actually called a yotta, but I'll upvote anyway - lol.
Yolo2116
Do you know how i would do this with big-int, i don't think that the math methods work with bigint.
James
Sorry no, I haven't used BigInt, I haven't needed that much precision before.