在 C# (asp.net MVC3) 中用逗号和小数格式化数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16035506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
format a number with commas and decimals in C# (asp.net MVC3)
提问by Krishan
I Need to display a number with commas and decimal point.
我需要显示一个带逗号和小数点的数字。
Eg:
Case 1 : Decimal number is 432324 (This does not have commas or decimal Points)
Need to display it as 432,324.00
but not 432,324
例如:案例 1:十进制数是 432324(这没有逗号或小数点)需要将其显示为432,324.00
但不是 432,324
Case 2 : Decimal number is 2222222.22 (This does not have commas) Need to display it as 2,222,222.22
Case 2 : Decimal number is 2222222.22 (这个没有逗号) 需要显示为2,222,222.22
I tried ToString("#,##0.##"). But It is Not Formatting it
我试过了 ToString("#,##0.##")。但它不是格式化它
采纳答案by Roys
int number = 1234567890;
Convert.ToDecimal(number).ToString("#,##0.00");
You will get the result 1,234,567,890.00.
你会得到结果1,234,567,890.00。
回答by Belogix
Your question is not very clear but this should achieve what you are trying to do:
您的问题不是很清楚,但这应该可以实现您的目标:
decimal numericValue = 3494309432324.00m;
string formatted = numericValue.ToString("#,##0.00");
Then formattedwill contain: 3,494,309,432,324.00
然后formatted将包含:3,494,309,432,324.00
回答by Patrick D'Souza
Try with
试试
ToString("#,##0.00")
From MSDN
来自 MSDN
*The "0" custom format specifier serves as a zero-placeholder symbol. If the value that is being formatted has a digit in the position where the zero appears in the format string, that digit is copied to the result string; otherwise, a zero appears in the result string. The position of the leftmost zero before the decimal point and the rightmost zero after the decimal point determines the range of digits that are always present in the result string.
*“0”自定义格式说明符用作零占位符符号。如果正在格式化的值在格式字符串中出现零的位置有一个数字,则将该数字复制到结果字符串中;否则,结果字符串中会出现零。小数点前最左边的零和小数点后最右边的零的位置决定了结果字符串中始终存在的数字范围。
The "00" specifier causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used. For example, formatting 34.5 with "00" would result in the value 35.*
“00”说明符使值四舍五入到小数点前最接近的数字,其中始终使用远离零的四舍五入。例如,使用“00”格式化 34.5 将导致值 35.*
回答by Jeppe Stig Nielsen
Maybe you simply want the standard format string "N", as in
也许您只是想要标准格式字符串"N",如
number.ToString("N")
It will use thousand separators, and a fixed number of fractional decimals. The symbol for thousands separators and the symbol for the decimal point depend on the format provider (typically CultureInfo) you use, as does the number of decimals (which will normally by 2, as you require).
它将使用千位分隔符和固定数量的小数。千位分隔符的符号和小数点的符号取决于CultureInfo您使用的格式提供程序(通常为),以及小数位数(通常为 2,根据您的需要)。
If the format provider specifies a different number of decimals, and if you don't want to change the format provider, you can give the number of decimals after the N, as in .ToString("N2").
如果格式提供程序指定了不同的小数位数,并且您不想更改格式提供程序,则可以在 后指定小数位数N,如.ToString("N2").
Edit: The sizes of the groups between the commas are governed by the
编辑:逗号之间的组的大小由
CultureInfo.CurrentCulture.NumberFormat.NumberGroupSizes
array, given that you don't specify a special format provider.
数组,假设您没有指定特殊格式提供程序。
回答by DarkoM
CultureInfo us = new CultureInfo("en-US");
TotalAmount.ToString("N", us)
回答by Matt Welson
All that is needed is "#,0.00", c# does the rest.
所需要的只是“#,0.00”,其余的由 C# 完成。
Num.ToString("#,0.00"")
Num.ToString("#,0.00"")
- The "#,0" formats the thousand separators
- "0.00" forces two decimal points
- “#,0”格式化千位分隔符
- “0.00”强制两位小数
回答by Wade Hatler
I had the same problem. I wanted to format numbers like the "General" format in spreadsheets, meaning show decimals if they're significant, but chop them off if not. In other words:
我有同样的问题。我想像电子表格中的“常规”格式一样格式化数字,这意味着如果小数很重要就显示小数,否则将它们砍掉。换句话说:
1234.56 => 1,234.56
1234 => 1,234
1234.56 => 1,234.56
1234 => 1,234
It needs to support a maximum number of places after the decimal, but don't put trailing zeros or dots if not required, and of course, it needs to be culture friendly. I never really figured out a clean way to do it using String.Formatalone, but a combination of String.Format and Regex.Replacewith some culture help from NumberFormatInfo.CurrentInfodid the job (LinqPad C# Program).
它需要支持小数点后的最大位数,但如果不需要,不要添加尾随零或点,当然,它需要文化友好。我从来没有真正想出一种单独使用String.Format的干净方法,但是 String.Format 和Regex.Replace的组合以及NumberFormatInfo.CurrentInfo 的一些文化帮助完成了这项工作(LinqPad C# 程序)。
string FormatNumber<T>(T number, int maxDecimals = 4) {
return Regex.Replace(String.Format("{0:n" + maxDecimals + "}", number),
@"[" + System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator + "]?0+$", "");
}
void Main(){
foreach (var test in new[] { 123, 1234, 1234.56, 123456.789, 1234.56789123 } )
Console.WriteLine(test + " = " + FormatNumber(test));
}
Produces:
产生:
123 = 123
1234 = 1,234
1234.56 = 1,234.56
123456.789 = 123,456.789
1234.56789123 = 1,234.5679
回答by JoshYates1980
回答by Sagar Mahajan
string Mynewcurrency = DisplayIndianCurrency("7743450.00");
private string DisplayIndianCurrency(string EXruppesformate)
{
string fare = EXruppesformate;
decimal parsed = decimal.Parse(fare, CultureInfo.InvariantCulture);
CultureInfo hindi = new CultureInfo("en-IN");
// string text = string.Format(hindi, "{0:c}", parsed);if you want <b>Rs 77,43,450.00</b>
string text = string.Format(hindi, "{0:N}", parsed); //if you want <b>77,43,450.00</b>
return ruppesformate = text;
}
回答by Abolfazl Rastgou
Try with
试试
ToString("#,##0.###")
Produces:
产生:
1234.55678 => 1,234.556
1234 => 1,234
1234.55678 => 1,234.556
1234 => 1,234

