Using Very Large Numbers and Prefixes // For an Idle Game e100,e1000, and more.

Hi. So I made a sample code to make a never-ending number prefix system. The problem is that the code can get super long if I copy and paste a thousand times and I have no idea on how to automate this script with only a few lines of code. Here is the code.

if (num >= 1000)
        {
            e1num = num / 1000;
            numText.text = "" + e1num.ToString("F2") + "e3" ;
        }
        if (e1num >= 10)
        {
            e2num = e1num / 10;
            numText.text = "" + e2num.ToString("F2") + "e4";
        }
        if (e2num >= 10)
        {
            e3num = e2num / 10;
            numText.text = "" + e3num.ToString("F2") + "e5";
        }
        if (e3num >= 10)
        {
            e4num = e3num / 10;
            numText.text = "" + e4num.ToString("F2") + "e6";
        }
        if (e4num >= 10)
        {
            e5num = e4num / 10;
            numText.text = "" + e5num.ToString("F2") + "e7";
        }
        if (e5num >= 10)
        {
            e6num = e5num / 10;
            numText.text = "" + e6num.ToString("F2") + "e8";
        }
        if (e6num >= 10)
        {
            e7num = e6num / 10;
            numText.text = "" + e7num.ToString("F2") + "e9";
        }
        if (e7num >= 10)
        {
            e8num = e7num / 10;
            numText.text = "" + e8num.ToString("F2") + "e10";
        }
        if (e8num >= 10)
        {
            e9num = e8num / 10;
            numText.text = "" + e9num.ToString("F2") + "e11";
        }
        if (e9num >= 10)
        {
            e10num = e9num / 10;
            numText.text = "" + e10num.ToString("F2") + "e12";
        }

num is the first number variable for 1-10000, (only display when the value is 1-1000). e1num is e3 and displays 1000-10000. e2num is e4 and displays 10000-100000. and this goes on 8 more times. I don’t want to do this 992 more times in order to hit e1000. Do you guys think you can help me find a way to shrink this script so it can be never-ending and very easy to read and change (variable names and text such as “e3”, I want to prevent having to copy and paste variables 20 times like my previous script. Thanks, NightGrounds/ZachAttack9280.

You missed an important fact about your number. Which type is “num”? You know that an int (which is an Int32) only goes up to 2,147,483,647 the unsigned uint up to 4,294,967,295. If you use a long (which is an Int64) the number goes up to 9,223,372,036,854,775,807 and the unsigned version ulong up to 18,446,744,073,709,551,615. So even with an ulong you are bound to max “e19”.

So you can’t store an infinite large number in any data type that has a fix length / size. Even when using double as datatype the range is limited and more important the precision decreases the larger the number gets. That means changes that are too small may not be recognised and rounded to 0. I’ve posted a table for the float data type over here.

If you actually work with integer numbers you could use an infinite integer struct which stores it’s value inside an array which can grow as necessary. For example BigInteger

Apart from how you want to store you number you can directly determine the base 10 exponent of a number by calculating the base 10 log of your number and round it down to the nearest integer. The BigInteger has a dedicated Log10 method.

Note that solutions like the BigInteger have quite a bit of overhead the larger the number gets. It shouldn’t be too bad if you have just a couple of them.

What solution might be the best for you depends on your relative sizes of the numbers you work with. Sometimes it’s enough to use a hybrid of an ulong and a double value. You should think about your max increase and what’s the max required range you may need. For example if you use an ulong value that you increase by 10000 every frame at 60 fps it would take 974904 years to run out of digits. However if your relative increase should itself increase over time simply using a double might be enough. We don’t know enough about your conditions to give a clear answer.

Note that while this will incur rounding error and you will lose precision, the easiest way to do this in a one-liner is to use the built in custom string formatting to format the string as an exponent with the decimal places that you want.

numText.text = string.Format((num < 1000) ? "{0:F3}" : "{0:0.00e0}", num);

Alright, I tried that out and I’m running into the same problem as I have been trying to solve, the dividing part. Basically, the highest number I can go to is 3.40e38 but I would like to go higher. I’m still trying to find a way I can simply divide e30 or e10 of it so I can go higher.

If you have ever played or heard of Best Fiends Forever, it’s an idle game that has, multiple coins, bronze, silver, and gold (and maybe more). Once you hit 1 trillion bronze, the game currency turns into 1 silver and so on. I am trying to do the same thing just without the extra currencies (bronze, silver, and gold).

alt text

Hope that makes sense

alt text

alt text

I’m still pretty confused honestly :confused: