1

I am trying to make a clicker game in un. Currently I am using doubles as a way to show large numbers. However when I get to any numbers above a trillion, it goes to -1354 trillion or something like that. The code shown bellow is code to fill in large numbers as "___ Million" or "____ Billion" but once it gets past 999 Billion is shows "-1354 Trillion". It continues to act like a trillion dollars, as you can buy items with apparently negative money, but the display is all wrong.

I have tried changing the double to int, long, and float and all of them result in the same issue. I have also tried even larger numbers but they don't even work in the system. It seems like 999 Billion is the limit before it turns all hell loose.

public static string largeWholeNumbers(double number)
{
    int numOfZeros = (number.ToString()).Length; //Gets Length of number
    int zeroSets = (int)Mathf.Floor(numOfZeros / 3.001f); //Divides it by a little over 3 and floors it 
    double numDisplay = number/(power(10, ((2 *zeroSets) + (zeroSets - 1)))); //Get the thing infront of the illion (IE 134, 6 million)

    //Defines
    string numberName = "";
    int hugeNumberSet = 0;
    int hugeNumberStart = 0;
    int hugeNumberStartIndex = 0;
    int hugeNumberEnd = 0;

    if (zeroSets < 11)
    {
        zeroSets -= 2;
        numberName = numberEndings.wholeNumEndings[zeroSets];
    }

    if (zeroSets > 10)
    {
        hugeNumberSet = (int)Mathf.Floor((zeroSets - 1)/10); //Divides it by 10 to get 10, 20, 30 etc
        hugeNumberEnd = hugeNumberSet - 1;
        hugeNumberStart = zeroSets - (hugeNumberSet * 10); //Example zeroSet = 12, this = 2
        hugeNumberStartIndex = hugeNumberStart - 1; 
        if (hugeNumberStart < 1)
        {
            numberName = numberEndings.endNumEndings[hugeNumberEnd];
        }

        if (hugeNumberStart >= 1)
        {
            numberName = numberEndings.endNumEndings[hugeNumberEnd] + numberEndings.beginNumEndings[hugeNumberStartIndex];
        }
    }

    return numDisplay.ToString() + " " + numberName;
}

Expected results would be to show the number in the interface as "1 Trillion", however it shows it as "-1354 Trillion".

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • AFAIK, all the existing numeric types do not fit requirements for clickers. You need to use some kind of long arithmetics, https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic – rs232 Apr 16 '19 at 05:44
  • 1
    @rs232 depends the needs I'ld say .. [double](https://learn.microsoft.com/dotnet/api/system.double.maxvalue) has a Max value that is quite ok for a while (depending ofcourse how fast you increase) – derHugo Apr 16 '19 at 05:49
  • I suspect that your `power` function returns an int/long that is overflowing. Can you try `Mathf.pow()` instead? – Gazihan Alankus Apr 16 '19 at 06:00
  • Possible duplicate of [Arbitrary-Precision Decimals in C#](https://stackoverflow.com/questions/4523741/arbitrary-precision-decimals-in-c-sharp) – knh190 Apr 16 '19 at 06:37
  • Whole number is an integer right? Why do you use double for ints then? It's a waste of memory space. – knh190 Apr 16 '19 at 06:41
  • Also converting double to string and back and forth is weird. There're too many possible tricks can cause an overflow. – knh190 Apr 16 '19 at 06:44

0 Answers0