5

I am using Unity to make an "Incremental Game" also known as an "Idle Game" and I am trying to format large numbers. For example, when gold gets to say 1000 or more, it will display as Gold: 1k instead of Gold: 1000.

using UnityEngine;
using System.Collections;

public class Click : MonoBehaviour {

    public UnityEngine.UI.Text GoldDisplay;
    public UnityEngine.UI.Text GPC;
    public double gold = 0.0;
    public int gpc = 1;

    void Update(){
        GoldDisplay.text = "Gold: " +  gold.ToString ("#,#");
        //Following is attempt at changing 10,000,000 to 10.0M
        if (gold >= 10000000) {
        GoldDisplay.text = "Gold: " + gold.ToString ("#,#M");
        }
        GPC.text = "GPC: " + gpc;
    }

    public void Clicked(){
            gold += gpc;
    }
}

I have tried other examples while searching online, which is where gold.ToString ("#,#"); came from, however none of them worked.

13
  • 3
    gold.ToString returns a result, it does not modify gold. You need to do something like GoldDisplay.text = "Gold: " + gold.ToString("#,#");
    – Blorgbeard
    Jun 23, 2015 at 1:14
  • How big will this gold number get to? Say Quadrillions?
    – Zze
    Jun 23, 2015 at 1:14
  • 2
    But you're going to have problems using int for an idle game. The max value is 2 billion.
    – Blorgbeard
    Jun 23, 2015 at 1:15
  • same with float, some idle game have issue with it, go with double
    – Fredou
    Jun 23, 2015 at 1:15
  • 1
    If you like, you can answer your own question with the code that worked. Jun 23, 2015 at 4:29

5 Answers 5

6

Slight refactoring:

public static string KMBMaker( double num )
{
    double numStr;
    string suffix;
    if( num < 1000d )
    {
        numStr = num;
        suffix = "";
    }
    else if( num < 1000000d )
    {
        numStr = num/1000d;
        suffix = "K";
    }
    else if( num < 1000000000d )
    {
        numStr = num/1000000d;
        suffix = "M";
    }
    else
    {
        numStr = num/1000000000d;
        suffix = "B";
    }
    return numStr.ToString() + suffix;
}

Use:

GoldDisplay.text = KMBMaker(gold);

Changes:

  1. Explicitly use double constants
  2. Remove duplication
  3. Separate concerns - there's no need for this method to know about text boxes
1
  • 1
    Tip for people that hate to count 0's like I do and/or want to go into even bigger suffixes. The values can also be replaced with scientific notation, aka 1000 = 1e3d 1000000 = 1e6d and so on
    – MX D
    Jun 29, 2015 at 17:21
4

i don't think there is a build-in method for that so i just maybe reinvented the wheel by writing my own way but this should be how i would maybe do it

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            for (double i = 500d; i < 5e23; i *= 100d)
            {
                Console.WriteLine(i.ToMyNumber());
            }

            Console.Read();
        }

    }

    public static class helper
    {
        private static readonly  List<string> myNum;

        static helper()
        {
            myNum = new List<string>();
            myNum.Add("");
            myNum.Add("kilo");
            myNum.Add("mill");
            myNum.Add("bill");
            myNum.Add("tril");
            myNum.Add("quad");
            myNum.Add("quin");
            myNum.Add("sext");
            // ....
        }

        public static string ToMyNumber(this double value)
        {
            string initValue = value.ToString();
            int num = 0;
            while (value >= 1000d)
            {
                num++;
                value /= 1000d;
            }

            return string.Format("{0} {1} ({2})", value, myNum[num], initValue);
        }
    }
}

which print this

500  (500)
50 kilo (50000)
5 mill (5000000)
500 mill (500000000)
50 bill (50000000000)
5 tril (5000000000000)
500 tril (500000000000000)
50 quad (5E+16)
5 quin (5E+18)
500 quin (5E+20)
50 sext (5E+22)
1

I am using this method in my project, you can use too. Maybe there is better way, I dont know.

public void KMBMaker( Text txt, double num )
    {
        if( num < 1000 )
        {
            double numStr = num;
            txt.text = numStr.ToString() + "";
        }
        else if( num < 1000000 )
        {
            double numStr = num/1000;
            txt.text = numStr.ToString() + "K";
        }
        else if( num < 1000000000 )
        {
            double numStr = num/1000000;
            txt.text = numStr.ToString() + "M";
        }
        else
        {
            double numStr = num/1000000000;
            txt.text = numStr.ToString() + "B";
        }
    }

and use this method in your update like this.

void Update()
{
     KMBMaker( GoldDisplay.text, gold );
}
1

I use this:

    private static List<string> nFormat= new List<string>();

        void Awake()
        {
         nFormat.Add("");
         nFormat.Add("K");
         nFormat.Add("M");
         nFormat.Add("B");
        //...
        }

        public static string Double2dec(double value)
        {
             int num = 0;
             while (value >= 1000d)
             {
                 num++;
                 value /= 1000d;
             }
             return value.ToString("F2") + nFormat[num];
        }
1
  • This one is the closest to what I would use. Though I'm said no one is utilizing logarithms...
    – IsMakeFire
    Apr 23, 2021 at 22:50
0

I tried some of the solutions given here, but was put off by the effort it would need to write down all abbreviations from K to NoNod, so I wrote this - though I haven't tried whether it impacts performance greatly (though it seemed to work as smooth as using scientific notation).

using System;
//System.Numerics necessary for us of BigInteger only - alternatively use double (which might be faster, but more inaccurate
using System.Numerics;

namespace WPF_Clicker
{
    public static class NumberFormatter
    {
        
        /*NumberFormat
         * Scientific   => 0.0e0 | example: 1.234e14    -> 123,400,000,000,000
         * Abbreviation  => 0.0xx | example: 123.4T      -> 123,400,000,000,000
        */
        public enum NumberFormat
        {
            Scientific,
            Abbreviation
        }
        
        public static string FormatNumber(BigInteger number, NumberFormat format = NumberFormat.Scientific)
        {
            if (format == NumberFormat.Scientific)
                return ScientificFomat(number);
            else
                return AbbreviationFormat(number);
        }

        public static string ScientificFomat(BigInteger number)
        {
            if (number < 1000)
                //if number smaller than 1,000, don't use special format
                return number.ToString("#.##0");
            else
                //return number as scientific notation with 1 place before and 3 places after decimal point
                return number.ToString("e3");
        }

        public static string AbbreviationFormat(BigInteger number)
        {
            if (number < 1000)
                //if number smaller than 1,000, don't use special format
                return number.ToString("#.##0");

            //get division of number (string length - 1) by 3 and get remainder -> appendix changes with every 4 characters
            int decider = Math.DivRem(number.ToString().Length - 1, 3, out int remainder) - 1;
            //if decider is bigger than 9, looping for every place of the decider number
            int declength = decider.ToString().Length;
            //string is necessary to decide on which appendix to use
            string decstring = decider.ToString();
            //string for the appendix
            string appendix = "";

            if (decider > 99)
                //return "infinity" if decider is greater than 99
                //can be changed if you decide to go over NoNod (decider > 99)
                return "infinity";
            else
            {
                //thousands, millions and billions are special cases
                if (decider < 3)
                {
                    if (decider == 0)
                        appendix = "K";
                    else if (decider == 1)
                        appendix = "M";
                    else if (decider == 2)
                        appendix = "B";
                    else
                        appendix = "";
                }
                else
                    //loop through for decider number string
                    //start at the last place, since these will be definite and it saves the check of whether there is more than 1 place in the decider number string
                    for (int counter = declength - 1; counter >= 0; counter--)
                    {
                        char decchar = decstring[counter];

                        //handle last place of decider number string
                        if (counter == declength - 1)
                            if (decchar == '1')
                                appendix = "U";
                            else if (decchar == '2')
                                appendix = "D";
                            else if (decchar == '3')
                                appendix = "T";
                            else if (decchar == '4')
                                appendix = "Qa";
                            else if (decchar == '5')
                                appendix = "Qi";
                            else if (decchar == '6')
                                appendix = "Sx";
                            else if (decchar == '7')
                                appendix = "Sp";
                            else if (decchar == '8')
                                appendix = "Oc";
                            else if (decchar == '9')
                                appendix = "No";
                            else
                                appendix = "";
                        else
                        //handles second place (or higher) of decider number string
                        {
                            if (decchar == '1')
                                appendix += "Dc";
                            else if (decchar == '2')
                                appendix += "Vg";
                            else if (decchar == '3')
                                appendix += "Td";
                            else if (decchar == '4')
                                appendix += "Qad";
                            else if (decchar == '5')
                                appendix += "Qid";
                            else if (decchar == '6')
                                appendix += "Sxd";
                            else if (decchar == '7')
                                appendix += "Spd";
                            else if (decchar == '8')
                                appendix += "Ocd";
                            else if (decchar == '9')
                                appendix += "Nod";
                        }
                    }

                //since the number string is at this point definitly more than 3 places long, use the remainder to set the decimal point
                string numberstr = number.ToString().Substring(0, 4).Insert(remainder + 1, ",");

                //put the number with decimal point together with the appendix | example: 123,456,789,012,345,678,901,234,567,890,123,456,789,012 -> 123,4DDc
                return numberstr + appendix;
            }
        }
    }
}

Hope this helps.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.