2013-06-03 42 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; 
    } 
相关问题