将数字格式化为字符串,使用逗号代替小数

时间:2023-01-14 18:34:34

I have the following number: 4.3

我有以下号码:4.3

I'd like to display this number as 4,3 for 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?

我使用的是不正确的字符串吗?

9 个解决方案

#1


29  

I think:

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

should do what you want.

应该做你想做的事。

#2


30  

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

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

#3


11  

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输出方法。

#4


3  

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

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

#5


2  

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

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

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和自定义数字格式。

#6


1  

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);

#7


1  

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);

#8


0  

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;

#9


0  

Number formatting can be handled for you by the framework if you use the correct culture when 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(逗号是我的en-us默认文化中的小数分隔符),1.234(逗号是法语中的小数分隔符)和1,234(同样,逗号是法语中的小数分隔符,也是线程的将文化设置为法语,以便使用此文化显示 - 因此逗号作为输出中的小数分隔符)。

#1


29  

I think:

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

should do what you want.

应该做你想做的事。

#2


30  

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

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

#3


11  

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输出方法。

#4


3  

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

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

#5


2  

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

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

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和自定义数字格式。

#6


1  

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);

#7


1  

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);

#8


0  

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;

#9


0  

Number formatting can be handled for you by the framework if you use the correct culture when 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(逗号是我的en-us默认文化中的小数分隔符),1.234(逗号是法语中的小数分隔符)和1,234(同样,逗号是法语中的小数分隔符,也是线程的将文化设置为法语,以便使用此文化显示 - 因此逗号作为输出中的小数分隔符)。