How to format a number with thousands separator (commas) in C#

2 Answers

0 votes
using System;

class Program
{
    static void Main() {
        int x = 49876173;
        
        Console.WriteLine(String.Format("{0:n}", x)); 
        Console.WriteLine(String.Format("{0:n0}", x));
    }
}



/*
run:

49,876,173.00
49,876,173

*/

 



answered Dec 28, 2020 by avibootz
0 votes
using System;

class Program
{
    static void Main() {
        int num = 1234567;
        
        string str = num.ToString("N");
        Console.WriteLine(String.Format(str));
        
        str = num.ToString("N0");
        Console.WriteLine(str);
    }
}
 
 
 
 
/*
run:
 
1,234,567.00
1,234,567
 
*/

 

 



answered Aug 3, 2023 by avibootz

Related questions

1 answer 141 views
1 answer 194 views
1 answer 106 views
1 answer 122 views
1 answer 112 views
1 answer 125 views
2 answers 198 views
...