D
D
dark_spectator2018-11-10 20:18:25
C++ / C#
dark_spectator, 2018-11-10 20:18:25

Reducing large numbers in Unity3D. How to do it?

There is a number of type decimal in C#.
Conditionally 1,000,000,000,000
It is desirable to display it in the form of 1T (Trilion), while it is desirable that there be the possibility of such a display:
1,250,000,000,000 = 1.25T
I have already rummaged through many articles, but have not found a solution.
How can such a reduction be implemented?
1000 = 1K || 1 250 = 1.25K
1 000 000 = 1M || 1,250,000 = 1.25M
etc.
Everything I could find, but it doesn't work very well.

char[] names = { 'K', 'M', 'B', 'T', ...};
    private string __CutDigit(string digit)
    {
        var dotIdx = digit.IndexOf('.');
        if (dotIdx < 0) dotIdx = digit.Length;
        var triples = dotIdx / 3;
        var num = dotIdx % 3;

        return digit.Substring(0, num) + '.' + digit[num] + names[triples];
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2018-11-10
@dark_spectator

static string[] names = { "", "K", "M", "B", "T" };
  
static string FormatMoney(decimal digit)
{
  int n = 0;
  while (n + 1 < names.Length && digit >= 100m)
  {
    digit /= 1000m;
    n++;
  }
  return string.Format("{0}{1}", digit, names[n]);
}

Counting digit >= 100m can result in numbers like 0.11K or 0.25M.
If fractional numbers are needed only greater than 1, then the comparison must be done with 1000.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question