我需要将数字转换为逗号分隔格式,以在 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
。
我需要将数字转换为逗号分隔格式,以在 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
。
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));
String.Format("{0:n}", 1234);
String.Format("{0:n0}", 9876); // no decimals
如果你想要独特并且愿意多做一些不必要的工作,这里有一个我为整数数字创建的函数。你可以在任何间隔处放置逗号,只需将3放置在每千位上即可,或者你也可以选择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;
}
你尝试过了吗:
ToString("#,##0.00")
快速而粗略的方法:
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);
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
ToString("N0");
吗? - V4Vendetta{0:#,#}
) - Sayse