C# 用逗号代替小数将数字格式化为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1559185/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:50:32  来源:igfitidea点击:

Formatting Numbers as Strings with Commas in place of Decimals

c#.netformattingglobalizationnumbers

提问by DaveDev

I have the following number: 4.3

我有以下号码: 4.3

I'd like to display this number as 4,3for some of our European friends.

我想4,3为我们的一些欧洲朋友显示这个数字。

I was under the impression that the following line would do the trick:

我的印象是以下行可以解决问题:

string ret = string.Format("{0:0,0}", 4.3); // returns "04", not "4,3"

Am I using the incorrect string?

我是否使用了错误的字符串?

采纳答案by JDunkerley

I think:

我认为:

string.Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE"), "{0:0.0}", 4.3); 

should do what you want.

应该做你想做的。

回答by Lucero

The number formatting by default uses the NumberFormatInfofrom the current CultureInfo, so that it displays using the regional settings on the computer.

默认情况下,数字格式使用来自当前CultureInfoNumberFormatInfo,以便它使用计算机上的区域设置进行显示。

Therefore, you don't really need to do anything special about it, except maybe making sure that the correct CultureInfo is being used.

因此,除了确保使用正确的 CultureInfo 之外,您实际上不需要对其进行任何特殊处理。

As for the question, yes the string is invalid. A "," denotes a thousand-separator, not the decimal-separator. Have a look the the NumberFormatInfo and the custom number formats.

至于问题,是的,字符串无效。“,”表示千位分隔符,而不是十进制分隔符。看看 NumberFormatInfo 和自定义数字格式

回答by bzlm

For a more general solution, you can set the thread's UI cultureto the culture used by your European friends. This will affect the presentation of numbers, currency, dates, etc.

对于更通用的解决方案,您可以将线程的 UI 文化设置为您的欧洲朋友使用的文化。这将影响数字、货币、日期等的显示。

回答by Thomas Levesque

Use a CultureInfo that has commas instead of decimal point (French for instance) :

使用带有逗号而不是小数点的 CultureInfo(例如法语):

string.Format(CultureInfo.GetCultureInfo("fr-FR"), "{0:0.0}", 4.3);

回答by LukeH

NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ",";
nfi.NumberGroupSeparator = ".";

double num = 4.3;
string ret = num.ToString(nfi);    // 4,3

回答by Foxfire

This depends on what you want to do. If you want to make your entire application ready for some other language output just use

这取决于你想做什么。如果您想让整个应用程序为其他语言输出做好准备,只需使用

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo ("de-DE");

If you want to have a mixed language application (that means some of the outputs are formatted in one language some in another) you have to use the overloads of the specific String functions like:

如果你想要一个混合语言应用程序(这意味着一些输出以一种语言格式化另一种),你必须使用特定字符串函数的重载,例如:

var culture =  System.Globalization.CultureInfo.GetCultureInfo("de-DE");   
String.Format (culture, "{0:0.0}", 4.3);

The first method is preferred if it is possible because you only need one single line in your application and everything will be as expected. In the second you have to add the culture parameter to every String ouput method in your entire application.

如果可能,首选第一种方法,因为您的应用程序中只需要一行,一切都会如预期的那样。在第二个中,您必须将culture 参数添加到整个应用程序中的每个String 输出方法。

回答by Guffa

Yes, you are using the wrong string, and also the problem can't be solved by only providing a formatting string.

是的,您使用了错误的字符串,并且仅提供格式字符串也无法解决问题。

What your formatting string does is to format the number using the pattern "0", then aligned to the length 0.

格式化字符串的作用是使用模式格式化数字"0",然后与长度对齐0

When you specify the decimal separator in a formatting string it's always a period regardless of the current culture. The decimal separator in the result of the formatting on the other hand is always the one used by the current culture. So, to get a comma as decimal separator in the result you have to make sure that the culture used for the formatting is one that uses comma as decimal separator.

当您在格式化字符串中指定小数点分隔符时,无论当前文化如何,它始终是一个句点。另一方面,格式化结果中的小数点分隔符始终是当前区域性使用的分隔符。因此,要在结果中使用逗号作为小数点分隔符,您必须确保用于格式化的区域性是使用逗号作为小数点分隔符的区域性。

You can either set the current culture for the thread so that it's used by default by the formatting, or specify a culture in the call:

您可以设置线程的当前区域性,以便默认情况下通过格式使用它,或者在调用中指定一个区域性:

string ret = String.Format(CultureInfo.GetCultureInfo(1053), "{0:0.0}", 4.3);

回答by Torbj?rn Hansson

Settings the thread culture will do this conversion automatically. However, if you want to format this with a custom string you can do the following:

设置线程文化将自动执行此转换。但是,如果您想使用自定义字符串对其进行格式化,您可以执行以下操作:

string ret = string.Format("{0:0.00}", 4.3);

or

或者

string ret = (4.3f).ToString("0.00");

The "." is the format specifier for decimal point for the current culture. More info for custom number formats are found at: http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

这 ”。” 是当前区域性的小数点格式说明符。有关自定义数字格式的更多信息,请访问:http: //msdn.microsoft.com/en-us/library/0c899ak8.aspx

You can always check what character for the decimal point is with:

您始终可以使用以下命令检查小数点的字符:

string decimalChar = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;

回答by adrianbanks

Number formatting can be handled for you by the framework if you use the correct culturewhen manipulating the number.

如果您在操作数字时使用正确的文化,框架可以为您处理数字格式。

Console.WriteLine(4.3);

Console.WriteLine(4.3.ToString(CultureInfo.GetCultureInfo("fr-fr")));

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("fr-fr");
Console.WriteLine(4.3);

If you want to do a "one-off" display, the second approach will work. If you want to display every number correctly, you really should be setting the current thread's culture. That way, any number manipulation will handle decimal separators, grouping characters and any other culture-specific things correctly.

如果您想进行“一次性”展示,则第二种方法会奏效。如果你想正确显示每个数字,你真的应该设置当前线程的文化。这样,任何数字操作都将正确处理小数点分隔符、字符分组和任何其他特定于文化的事物。

The same goes for parsing numbers. If a user enters 1,234, how do you know whether they have entered 1.234(the comma being the decimal separator) or 1234(the comma being a grouping separator)? This is where the culture helps out as it knows how to display numbers and can also be used to parse them correctly:

解析数字也是如此。如果用户输入1,234,你怎么知道他们输入了1.234(逗号是小数点分隔符)还是1234(逗号是分组分隔符)?这是文化提供帮助的地方,因为它知道如何显示数字,也可以用来正确解析它们:

Console.WriteLine(double.Parse("1,234"));

Console.WriteLine(double.Parse("1,234", CultureInfo.GetCultureInfo("fr-fr")));

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("fr-fr");
Console.WriteLine(double.Parse("1,234"));

The above will output 1234(the comma is a decimal separator in my en-us default culture), 1.234(the comma is a decimal separator in French) and 1,234(again, the comma is a decimal separator in French, and also the thread's culture is set To French so it displays using this culture - hence the comma as a decimal separator in the output).

以上将输出1234(逗号是我的默认文化1.234中的小数点分隔符),(逗号是法语中的小数点分隔符)和1,234(同样,逗号是法语中的小数点分隔符,并且设置了线程的文化对于法语,因此它使用这种文化显示 - 因此逗号作为输出中的小数点分隔符)。