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.
gold.ToStringreturns a result, it does not modifygold. You need to do something likeGoldDisplay.text = "Gold: " + gold.ToString("#,#");intfor an idle game. The max value is 2 billion.