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. 
Program to format a number with thousands separator in C/C++. 1 Convert the given integer N to its equivalent string. 2 Iterate over the characters of the given string from the right to the left. 3 After traversing every 3 characters, insert a ‘,’ separator.
The Currency ("C") Format Specifier. The "C" (or currency) format specifier converts a number to a string that represents a currency amount. The precision specifier indicates the desired number of decimal places in the result string.
A standard numeric format string can be used to define the formatting of a numeric value in one of two ways: It can be passed to an overload of the ToString method that has a format parameter. The following example formats a numeric value as a currency string in the current culture (in this case,...
The Decimal ("D") Format Specifier. The "D" (or decimal) format specifier converts a number to a string of decimal digits (0-9), prefixed by a minus sign if the number is negative. This format is supported only for integral types.
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:
double constantsi 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)
                        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 );
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With