3

I need to convert numbers into a comma separated format to display in C#.

For Example:

1000 to 1,000
45000 to 45,000
150000 to 1,50,000
21545000 to 2,15,45,000

How to achieve this in C#?

I tried the below code:

int number = 1000;
number.ToString("#,##0");

But it is not working for lakhs.

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
Stephen L
  • 351
  • 2
  • 6
  • 18
  • Did you try `ToString("N0");` – V4Vendetta Jun 03 '13 at 06:35
  • possible duplicate - http://stackoverflow.com/questions/105770/net-string-format-to-add-commas-in-thousands-place-for-a-number (`{0:#,#}` from this looks to be my favourite way if it works) – Sayse Jun 03 '13 at 06:36
  • possible duplicate of [String.Format in C#](http://stackoverflow.com/questions/16601968/string-format-in-c-sharp) – taocp Jun 03 '13 at 23:51

6 Answers6

7

I guess you can do this by creating a custom number format info for your needs

NumberFormatInfo nfo = new NumberFormatInfo();
nfo.CurrencyGroupSeparator = ",";
// you are interested in this part of controlling the group sizes
nfo.CurrencyGroupSizes = new int[] { 3, 2 };
nfo.CurrencySymbol = "";

Console.WriteLine(15000000.ToString("c0", nfo)); // prints 1,50,00,000

if specifically only for numbers then you could also do

nfo.NumberGroupSeparator = ",";
nfo.NumberGroupSizes = new int[] { 3, 2 };

Console.WriteLine(15000000.ToString("N0", nfo));
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
3

Here's a similar thread to yours add commas in thousands place for a number

and here's the solution that worked perfectly for me

     String.Format("{0:n}", 1234);

     String.Format("{0:n0}", 9876); // no decimals
Mohamed Rozza
  • 567
  • 8
  • 12
2

If you want to be unique and do extra work that you don't have to here is a function I created for integer numbers you can place commas at whatever interval you want, just put 3 for a comma for each thousandths or you could alternatively do 2 or 6 or whatever you like.

             public static string CommaInt(int Number,int Comma)
    {
     string IntegerNumber = Number.ToString();
     string output="";
     int q = IntegerNumber.Length % Comma;
     int x = q==0?Comma:q;
     int i = -1;
     foreach (char y in IntegerNumber)
     {
             i++;
             if (i == x) output += "," + y;
             else if (i > Comma && (i-x) % Comma == 0) output += "," + y;
             else output += y;

     }
     return output;
    }
CodeCamper
  • 6,609
  • 6
  • 44
  • 94
1

Have you tried:

ToString("#,##0.00")
Quinton Bernhardt
  • 4,773
  • 19
  • 28
0

Quick and dirty way:

Int32 number = 123456789;
String temp = String.Format(new CultureInfo("en-IN"), "{0:C0}", number);
//The above line will give Rs. 12,34,56,789. Remove the currency symbol
String indianFormatNumber = temp.Substring(3);
unlimit
  • 3,672
  • 2
  • 26
  • 34
0

An easy solution would be to pass a format into the ToString() method:

string format = "$#,##0.00;-$#,##0.00;Zero";
   decimal positiveMoney = 24508975.94m;
   decimal negativeMoney = -34.78m;
   decimal zeroMoney = 0m;
   positiveMoney.ToString(format);  //will return $24,508,975.94
   negativeMoney.ToString(format);  //will return -$34.78 
   zeroMoney.ToString(format);      //will return Zero

Hope this helps,