Search Unity

how do I format my score, when my number reaches 1000 or above then it is formated like this 1,000

Discussion in 'Scripting' started by shahriyordeveloping, May 18, 2021.

  1. shahriyordeveloping

    shahriyordeveloping

    Joined:
    May 11, 2021
    Posts:
    2
    Hello all,

    I want to format my score , here i wants that when my number reaches 1000 or above then it is formated like this 1,000, how can i do this in C# (csharp) ?

    Thanks
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Bunny83 likes this.
  3. Visssarion

    Visssarion

    Joined:
    Aug 7, 2020
    Posts:
    2
    Here's function to transform number into string like you want to:
    Code (CSharp):
    1. string NumberWithCommas(int number)
    2.     {
    3.         string output="";
    4.  
    5.         for (int i=0; number>0; i++) // i - numerical place
    6.         {
    7.             if ((i % 3 == 0) && i!=0)
    8.             {
    9.                 output = ',' + output;
    10.             }
    11.             output = (number % 10).ToString() + output;
    12.             number /= 10;
    13.         }
    14.         return output;
    15.     }