Answer the question
In order to leave comments, you need to log in
How to reduce large numbers in Unity2d?
I'm making a clicker and I've run into an issue like shrinking numbers in unity. For example, I need to display the number 1000 as 1k or 1100 as 1.1k, 1000000 as 1M, etc.
My money is designated as a variable private int score1;
Can you help?
Answer the question
In order to leave comments, you need to log in
Well, divide them by 1000 and add k.
Without writing the code, you will not do it, no one will write the code for you.
In a clicker for any, there will be such a large value that they will not fit into any long longs. Approximately threw a class, you need to test ...
class LongNumber
{
public static readonly String[] symbols = { " ", "k", "m", "b", "$", "!", "#" };
List<short> values = new List<short>();
public LongNumber(){
foreach(var s in symbols )
values.Add(0);
}
public string getValue(bool isFull = false){//true полный вывод, false только более
if (isFull)
return String.Join(" ",values.Select((v, i) => v + symbols[i]));
var inx = symbols.Length;
while(inx > 0 && values[--inx] == 0){};
return String.Format("{0} {1}", values[inx], symbols[inx]);
}
public LongNumber add(long value, String sym = " "){//добавить
if (value < 0){
sub(-value);
return this;
}
if (!symbols.Contains(sym))
throw new FormatException("Неизвестный символ");
var inx = symbols.ToList().IndexOf(sym);
value += values[inx];
values[inx] = Convert.ToInt16(value % 1000);
if (value >= 1000)
{
if (inx == symbols.Length - 1)
throw new OverflowException("Достинуто максимально большое значение");
add(Convert.ToInt64(Math.Floor((double)value / 1000)), symbols[inx + 1]);
}
return this;
}
public static LongNumber valToNumber(long val,string sym = " "){//Число в LongNumber
return new LongNumber().add(val,sym);
}
public bool isLarger(LongNumber ln){//Сравнение , true если this больше
for (int i = values.Count - 1; i >= 0; i--)
if (values[i] > ln.values[i])
return true;
else if (values[i] < ln.values[i])
break;
return false;
}
public LongNumber sub(long value, String sym = " "){//вычесть
if (value < 0){
add(-value);
return this;
}
if (!symbols.Contains(sym))
throw new FormatException("Неизвестный символ");
if(LongNumber.valToNumber(value,sym).isLarger(this))
throw new OverflowException("Вычитаемое число больше");
var inx = symbols.ToList().IndexOf(sym);
value = values[inx] - value;
values[inx] = Convert.ToInt16(value%1000);
if (value < 0){
if (values[inx] < 0)
values[inx] += 1000;
sub(Convert.ToInt64(Math.Ceiling(Math.Abs((double)value / 1000))), symbols[inx + 1]);
}
return this;
}
}
class Program
{
static void Main(string[] args)
{
LongNumber ln = new LongNumber();
Console.WriteLine(ln.getValue());
ln.add(1000);
Console.WriteLine(ln.getValue());
ln.add(123456);
Console.WriteLine(ln.getValue(true));
ln.add(1000,"m");
Console.WriteLine(ln.getValue(true));
ln.sub(124456);
Console.WriteLine(ln.getValue(true));
ln.sub(1,"b");
Console.WriteLine(ln.getValue(true));
ln.add(1234564564);
Console.WriteLine(ln.getValue(true));
ln.sub(234565000);
Console.WriteLine(ln.getValue(true));
ln.sub(999999564);
Console.WriteLine(ln.getValue(true));
ln.sub(-123);
Console.WriteLine(ln.getValue());
ln.add(-123);
Console.WriteLine(ln.getValue());
Console.ReadKey();
}
}
0
1 k
456 124k 0m 0b 0$ 0! 0#
456 124k 0m 1b 0$ 0! 0#
0 0k 0m 1b 0$ 0! 0#
0 0k 0m 0b 0$ 0! 0#
564 564k 234m 1b 0$ 0! 0#
564 999k 999m 0b 0$ 0! 0#
0 0k 0m 0b 0$ 0! 0#
123
0
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question