Formatting large numbers in C #

I use Unity to create an "incremental game", also known as an "Idle Game", and I'm trying to format large numbers. For example, when gold says 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 searched for other examples while searching the Internet where gold.ToString ("#,#"); came from gold.ToString ("#,#"); However, none of them worked.

+6
source share
3 answers

I use this method in my project, you can also use it. Maybe there is a better way, I don’t 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
source

Minor 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; } 

Using:

 GoldDisplay.text = KMBMaker(gold); 

Changes:

  • Explicitly use double constants
  • Remove duplication
  • Separate problems - this method does not need to be aware of text windows
+5
source

I don’t think there is a built-in method for this, so I can just invent a wheel by writing my own way, but this should be the way I could 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); } } } 

who 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) 
+3
source

Source: https://habr.com/ru/post/989564/


All Articles