Creating and Formatting Huge Numbers

Hey mates,

i’m currently making a Clicker Game and want to ask you about ideas for number formattings.
My current solution is this :

    private static string FormatCash(double Value)
    {
        if (Value>= 10000000000000000)
        {
            return (Value/ 1000000000000000D).ToString("0.#AA");
        }
        if (Value>= 1000000000000000)
        {
            return (Value/ 1000000000000000D).ToString("0.##AA");
        }
        if (Value>= 10000000000000)
        {
            return (Value/ 1000000000000D).ToString("0.#BB");
        }
        if (Value>= 1000000000000)
        {
            return (Value/ 1000000000000D).ToString("0.##BB");
        }
        if (Value>= 10000000000)
        {
            return (Value/ 1000000000D).ToString("0.#B");
        }
        if (Value>= 1000000000)
        {
            return (Value/ 1000000000D).ToString("0.##B");
        }

        if (Value>= 100000000)
        {
            return (Value/ 1000000D).ToString("0.#M");
        }
        if (Value>= 1000000)
        {
            return (Value/ 1000000D).ToString("0.##M");
        }
        if (Value>= 100000)
        {
            return (Value/ 1000D).ToString("0.#K");
        }
        if (Value>= 1000)
        {
            return (Value/ 1000D).ToString("0.##K");
        }

        return Value.ToString("#,0");
    }

But when ill add “huger” number Visual Studio and unity say this : “Integral constant is too large”
Do i have to use Int64 for the huger number formats ?

Another idea was to create small point numbers and let the user think he has billion ore more Score. But how do i do that.

Thanks in advance

1 Like

any reason for using such large numbers?

Take a look here for the ranges of built in data types.
You could use a decimal for maximum size especially if you are dealing with money as float and double may suffer from precision issues when you get into very high values. You will pay a small performance cost with decimal over double though.

Most clickers an get away with floats or doubles. These allow for very big numbers out of the box.

Sure you loose accuracy after a dozen or so significant figures. However as clickers only show three or four figures, no one is going to know. When you have a bagillion cookies, and are getting a million cookies a second, no one will see that you miss one cookie here and there.

1 Like

LoL found a javaScript on a website formatting Huge numbers. How to contert to c# ? :smile:

function numberFormat(number)
{
    var temp = number;
  //var tabUnits = [" million", " billion", " trillion", " quadrillion", " quintillion", " sextillion", " septillion", " octillion", " nonillion", " decillion", " undecillion", " duodecillion", " tredecillion", " quattuordecillion", " quindecillion", " sexdecillion", " septendecillion", " octodecillion", " novemdecillion", " vigintillion", " unvigintillion", " duovigintillion", " tresvigintillion", " quattuorvigintillion", " quinquavigintillion", " sesvigintillion", " septemvigintillion", " octovigintillion", " novemvigintillion", " trigintillion", " untrigintillion", " duotrigintillion", " duotrigintillion", " trestrigintillion", " quattuortrigintillion", " quinquatrigintillion", " sestrigintillion", " septentrigintillion", " octotrigintillion", " noventrigintillion", " quadragintillion"];
  var tabUnits = ["K","M","B","T","aa","bb","cc","dd","ee","ff","gg","hh","ii","jj","kk","ll","mm","nn","oo","pp","qq","rr","ss","tt","uu","vv","ww","xx","yy","zz"];
  var highnumber = false;
  var bignumber = 1000;
  var tabposition = -1;
  var p_atomepersecond = true;
    var unit;
    if (number >= bignumber) {
        highnumber = true;
        while (number >= bignumber) { bignumber *= 1000; tabposition++; }
        //while (number >= bignumber && tabposition < 4 ) { bignumber *= 1000; tabposition++; }
        number /= (bignumber / 1000);
        unit = tabUnits[tabposition];
    } else unit = "";
    if (unit == undefined) return temp.toExponential(2);
    var toround = (highnumber == true) ? (p_atomepersecond == true) ? 100 : 100 : 10;
    var res = Math.round(number * toround) / toround;
    return [res.toLocaleString().replace(",", ".") + '' + unit];
}

Source : Tap Titans Heroes Calculator

Here is one I prepared earlier. For formatting note the ToString method.

first and foremost you must ask yourself. do you really need numbers that big? going past the bounds of an unsigned long can be done a number of ways. but usually its more trouble than its worth.

If you really want to hold huge numbers, then one thing you can do is create a SuperNumber struct, You break up the big number into smaller digits that uses an array (i.e. superNumber[digit] = valueofdigit) of int, short, or bytes. due to the sheer size of a supernumber you can’t simply convert it to an int, long, etc. without losing data, so you typically will have to show its value through a ToString() call.

You’ll have to perform math on it manually, most likely writing some implicit operators. Just know that when calculating exponents on huge numbers, for the love of god use a exponentiation by squaring algorithm, or better if you can find one. I once did it the normal way when I wrote an RSA encryption program, and after about a minute I found that the normal exponent operator is not efficient for huge numbers.

That being said and due to typical performance of all math operations, its best that you try to do as much of the math as possible with the normal number datatypes before converting to SuperNumber.

of course that is just one way. you could also make a class that implements a scientific notation, discarding least significant digits (which is practically a float).