我如何在 C# 中用空格分隔数千个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17527847/
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
How would I separate thousands with space in C#
提问by Mikayil Abdullayev
Assume I have the following decimal number that I have to format so that every thousand should be separated with a space:
假设我有以下十进制数,我必须对其进行格式化,以便每一千位都用空格分隔:
897.11 to 897.11
1897.11 to 1 897.11
12897.11 to 12 897.11
123897.11 to 123 897.11
I have tried Decimal.ToString("0 000.00"). Although this works pretty well when the number is 1897.11. But when it's 897.11 I get 0 897.11.
我试过 Decimal.ToString("0 000.00")。尽管当数字为 1897.11 时这很有效。但是当它是 897.11 时,我得到 0 897.11。
采纳答案by Tim S.
Pass in a custom NumberFormatInfowith a custom NumberGroupSeparatorproperty, and use the #,#formatto tell it to do number groups. This example uses the invariant culture's number format as its basis.
传入一个NumberFormatInfo带有自定义NumberGroupSeparator属性的自定义,并使用#,#格式告诉它做数字组。此示例使用不变区域性的数字格式作为其基础。
var nfi = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
nfi.NumberGroupSeparator = " ";
string formatted = 1234897.11m.ToString("#,0.00", nfi); // "1 234 897.11"
回答by Charles Bretana
with a custom format string, but use '#' not '0's a 0 is a mandatory token it will be there no matter how long the number is, the pound sign (#) is optional token, it will only be used for digits actually in the number.
使用自定义格式字符串,但使用“#”而不是“0” 0 是强制性标记,无论数字多长,它都会存在,井号 (#) 是可选标记,实际上仅用于数字在数量上。
Decimal.ToString("# ###.00")
NOTE. I'll leave this here, but @Tim S' answer is a better one.
笔记。我会把这个留在这里,但@Tim S 的回答更好。
When I try this I see it only generates the first separator... I guess the separator only works for commas and periods here. As a workaround (up to the maximum size number you expect), try this instead.
当我尝试这个时,我看到它只生成第一个分隔符......我猜这里的分隔符只适用于逗号和句点。作为一种解决方法(最多达到您期望的最大大小),请尝试使用此方法。
Decimal.ToString("# ### ### ### ###.00")
回答by Matthieu M
try this
尝试这个
int testNumber = 134566548;
Console.WriteLine(string.Format("{0:N}", testNumber));
You will received : 134 566 548,00
您将收到:134 566 548,00
Don't forget your culture.
不要忘记你的文化。
回答by Adam Houldsworth
You need a custom number format provider where you change the character yourself:
您需要一个自定义数字格式提供程序,您可以在其中自己更改字符:
static void Main(string[] args)
{
decimal d = 2000000;
var f = new NumberFormatInfo {NumberGroupSeparator = " "};
var s = d.ToString("n", f); // 2 000 000.00
}
Here I also specify to format it as a number (using "n"), please see this link for other formatting options:
在这里,我还指定将其格式化为数字(使用"n"),请参阅此链接以了解其他格式化选项:
Standard Numeric Format Strings
In this case, it won't work if you format as a currency or a percentage, because as you may already note the NumberFormatInfohas separate properties for those.
在这种情况下,如果您将格式设置为货币或百分比,它将不起作用,因为您可能已经注意到它们NumberFormatInfo具有单独的属性。
The current culture format info can be found on System.Globalization.CultureInfo.CurrentCulture.NumberFormat.
当前的文化格式信息可以在 上找到System.Globalization.CultureInfo.CurrentCulture.NumberFormat。
回答by Fiach Reid
Norwegian locale uses spaces as a separator, so if you were designing a website or application specifically for Norwegians, then you could use:
挪威语语言环境使用空格作为分隔符,因此如果您专门为挪威人设计网站或应用程序,则可以使用:
(123897.11).ToString("n",new CultureInfo("nb-NO"))
Which results in
这导致
"123?897,11"
Obviously, if you're not writing an application for the Norwegian market, then this would be a silly solution.
显然,如果您不是为挪威市场编写应用程序,那么这将是一个愚蠢的解决方案。
回答by Md. Nazrul Islam
Sweden Use thousand separator as Space(" ") and Decimal point separator as Comma(,). We can achieve this in the following way.
瑞典 使用千位分隔符作为空格(" ") 和小数点分隔符作为逗号(,)。我们可以通过以下方式实现这一点。
decimal myNumbber = 5878.476M;
var swedishCulture = new CultureInfo("sv-SE");
swedishCulture.NumberFormat.NumberDecimalSeparator = ",";
swedishCulture.NumberFormat.NumberGroupSeparator = " ";
var s = myNumbber.ToString("#,###.00", swedishCulture);
Which gives the output
这给出了输出
"5 878,48"

