How to convert/format numbers? (eg. 123K, 456M, 789B)
a simple way to format numbers?
1000 - 1K 1000000 - 10M
you get it, sorry this is so little but im a little annoyed since i just wrote a paragraph and a half but when attempting to post it erased it all....
can someone help me out with this and perhaps explain what it all means my knowledge of C# next to useless
I know what you mean about the post getting erased after spending a ton of time working on it. I've had a few posts get erased after spending close to half an hour getting rid of spelling mistakes (which I'm very prone to) and other errors. Some times, I wait a $$anonymous$$ute and then re-read my post because I can think faster than I can type, and I sometimes leave out a word or two just to keep up with my thoughts. A few times, the word that gets skipped is something small like "not", which can completely change the question I'm asking. So, yeah, I feel your pain.
Tried to make as short as possible, came up with this:
`private static List<Tuple<int, string>> ZeroesAndLetters = new List<Tuple<int, string>>()
{
new Tuple<int, string>(15, "Q"),
new Tuple<int, string>(12, "T"),
new Tuple<int, string>(9, "B"),
new Tuple<int, string>(6, "$$anonymous$$"),
new Tuple<int, string>(3, "$$anonymous$$"),
};
public static string GetPointsShortened(ulong num)
{
int zeroCount = num.ToString().Length;
for (int i = 0; i < ZeroesAndLetters.Count; i++)
if (zeroCount >= ZeroesAndLetters[i].Item1)
return (num / $$anonymous$$ath.Pow(10, ZeroesAndLetters[i].Item1)).ToString() + ZeroesAndLetters[i].Item2;
return num.ToString();
}`
In the ZeroesAndLetters list just add the numbers you need.
15, 12, 9 and ... are the number of zeroes, and Q, T, B and ... are shortened names.
Answer by Mindmapfreak · Oct 05, 2016 at 10:51 AM
The following code should do what you want. You can easily add additional abbrevations.
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
public class Test : MonoBehaviour
{
void Start()
{
//Some tests
Debug.Log("123000 is "+ AbbrevationUtility.AbbreviateNumber(123000));
Debug.Log("456000000 is " + AbbrevationUtility.AbbreviateNumber(456000000));
Debug.Log("789000000000 is " + AbbrevationUtility.AbbreviateNumber(789000000000));
Debug.Log("1000 is " + AbbrevationUtility.AbbreviateNumber(1000));
Debug.Log("1000000 is " + AbbrevationUtility.AbbreviateNumber(1000000));
}
}
public static class AbbrevationUtility
{
private static readonly SortedDictionary<int, string> abbrevations = new SortedDictionary<int, string>
{
{1000,"K"},
{1000000, "M" },
{1000000000, "B" }
};
public static string AbbreviateNumber(float number)
{
for (int i = abbrevations.Count - 1; i >= 0; i--)
{
KeyValuePair<int, string> pair = abbrevations.ElementAt(i);
if (Mathf.Abs(number) >= pair.Key)
{
int roundedNumber = Mathf.FloorToInt(number / pair.Key);
return roundedNumber.ToString() + pair.Value;
}
}
return number.ToString();
}
}
Got it working, only problem i have now is making it pick up past billions, i set the score as a float so it would pass 2 billion but now get problems while trying to create T (Trillion) as 1000000000000 and got The best overloaded collection initalizer method System.Collections.Generic.SortedDictionary<int,string>.Add(int, string)' has some invalid arguments as well as Argument
#1' cannot convert long' expression to type
int' also tried switching ints to floats but that just created more errors :(
The maximum possible value for int is 2,147,483,647. You have to change int to long if you want to work with larger numbers. The maximum value for long is 9,223,372,036,854,775,807.
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
public class Test : $$anonymous$$onoBehaviour
{
void Start()
{
//Some tests
Debug.Log("123000 is " + AbbrevationUtility.AbbreviateNumber(123000));
Debug.Log("456000000 is " + AbbrevationUtility.AbbreviateNumber(456000000));
Debug.Log("789000000000 is " + AbbrevationUtility.AbbreviateNumber(789000000000));
Debug.Log("1000 is " + AbbrevationUtility.AbbreviateNumber(1000));
Debug.Log("1000000 is " + AbbrevationUtility.AbbreviateNumber(1000000));
Debug.Log("135000000000000 is " + AbbrevationUtility.AbbreviateNumber(135000000000000));
}
}
public static class AbbrevationUtility
{
private static readonly SortedDictionary<long, string> abbrevations = new SortedDictionary<long, string>
{
{1000,"$$anonymous$$"},
{1000000, "$$anonymous$$" },
{1000000000, "B" },
{1000000000000,"T"}
};
public static string AbbreviateNumber(float number)
{
for (int i = abbrevations.Count - 1; i >= 0; i--)
{
$$anonymous$$eyValuePair<long, string> pair = abbrevations.ElementAt(i);
if ($$anonymous$$athf.Abs(number) >= pair.$$anonymous$$ey)
{
int roundedNumber = $$anonymous$$athf.FloorToInt(number / pair.$$anonymous$$ey);
return roundedNumber.ToString() + pair.Value;
}
}
return number.ToString();
}
}
how the hell does cookie clicker go so high O_o either way thanks for your help :)
Still some bugs :P past B so T and Q it has to pass 1000000028671 for T and 1000000020545535 for Q.... so even 1 above either of those will bring it to 1T or 1Q but otherwise 1000B and 1000T... kinda annoying but thats just unity i guess :/
@$$anonymous$$indmapfreak How about 12.5$$anonymous$$ or 987.5$$anonymous$$ ??
private static readonly SortedDictionary abbrevations = new SortedDictionary { {1000,"$$anonymous$$"}, {1000000, "$$anonymous$$" }, {1000000000, "B" } };
public static string Format$$anonymous$$(float number)
{
for (int i = abbrevations.Count - 1; i >= 0; i--)
{
$$anonymous$$eyValuePair<int, string> pair = abbrevations.ElementAt(i);
if ($$anonymous$$athf.Abs(number) >= pair.$$anonymous$$ey)
{
float rest = number % pair.$$anonymous$$ey;
float k = (number - rest) / pair.$$anonymous$$ey;
float f = $$anonymous$$athf.Round(rest / (pair.$$anonymous$$ey / 10));
string roundedNumber;
if (f == 0)
{
roundedNumber = k.ToString();
}
else
{
if(f == 10)
{
f = 9;
}
roundedNumber = k.ToString() + "." + f.ToString();
}
return roundedNumber + pair.Value;
}
}
return number.ToString();
}
Answer by camlam999 · Oct 05, 2016 at 01:40 PM
@ChrisAngelMindmapfreak I just made a quick new project to test this out, made a format script with your code in it and an "add" script which adds 50 to the 3d text as a score, i got no clue how to set this up or anything, half of it ive never seen, i try looking for videos that explain this but most my knowledge is from 1 of AwfulMedia's playlist :(
How does your code look at the moment? You can paste the code of the AbbrevationUtility-class into any file and use it with AbbrevationUtility.AbbreviateNumber(YOURNU$$anonymous$$BERHERE).