2013-06-03 132 views
2

我需要將comma中的數字以分隔的格式轉換爲顯示在C#中。將數字轉換爲逗號分隔的值

例如:

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

如何C#實現這一目標?

我嘗試下面的代碼:

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

但它不工作lakhs

+0

你有沒有嘗試'的ToString( 「NO」);' – V4Vendetta

+0

可能重複 - http://stackoverflow.com/questions/105770/net-string-format-to-add-commas-in-thousands- (#{#:#}'這個看起來是我最喜歡的方式,如果它工作的話) – Sayse

+0

[String.Format in C#]的可能重複(http://stackoverflow.com/questions/16601968 /串格式在-C-尖銳) – taocp

回答

0

你試過:

ToString("#,##0.00") 
0

快速和骯髒的方式:

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); 
5

我想你可以通過創建您的需求自定義數字格式信息做到這一點

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 

如果只專門爲數字,那麼你也可以做

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

Console.WriteLine(15000000.ToString("N0", nfo)); 
2

如果你想成爲獨一無二的做,你不必在這裏額外的工作是我爲整數創建的一個函數,您可以在任何需要的時間間隔放置逗號,只需將逗號分配給每個千分之三,或者也可以選擇2或6或任何您喜歡的。

   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; 
    } 
相關問題