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

发布于 2024-08-07 02:07:10 字数 215 浏览 1 评论 0原文

我有以下号码:4.3

我想为一些欧洲朋友将此号码显示为 4,3

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

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

我使用了错误的字符串吗?

I have the following number: 4.3

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

入怼 2024-08-14 02:07:10
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ",";
nfi.NumberGroupSeparator = ".";

double num = 4.3;
string ret = num.ToString(nfi);    // 4,3
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ",";
nfi.NumberGroupSeparator = ".";

double num = 4.3;
string ret = num.ToString(nfi);    // 4,3
韵柒 2024-08-14 02:07:10

我想:

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

应该做自己想做的事。

I think:

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

should do what you want.

热鲨 2024-08-14 02:07:10

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

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

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

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

如果可能的话,第一种方法是首选,因为您在应用程序中只需要一行,一切都会按预期进行。在第二个中,您必须将区域性参数添加到整个应用程序中的每个字符串输出方法。

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.

童话 2024-08-14 02:07:10

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

因此,您实际上不需要对此做任何特殊的事情,除了确保使用正确的 CultureInfo 之外。

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

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

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

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.

白芷 2024-08-14 02:07:10

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

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.

青衫儰鉨ミ守葔 2024-08-14 02:07:10

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

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

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

string.Format(CultureInfo.GetCultureInfo("fr-FR"), "{0:0.0}", 4.3);
做个少女永远怀春 2024-08-14 02:07:10

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

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

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

您可以设置线程的当前区域性,以便格式化时默认使用它,或者在调用中指定区域性:

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

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.

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);
半岛未凉 2024-08-14 02:07:10

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

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

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

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

您可以随时检查小数点的字符是什么:

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

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

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

string decimalChar = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
层林尽染 2024-08-14 02:07:10

如果您使用正确的 文化在操纵数字时。

Console.WriteLine(4.3);

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

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

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

解析数字也是如此。如果用户输入 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"));

上面将输出 1234 (逗号是我的 en-us 默认区域性中的小数分隔符)、1.234(逗号是法语中的小数分隔符)和 1,234(同样,逗号是法语中的小数分隔符,并且线程的区域性也设置为法语因此它使用这种区域性进行显示 - 因此逗号作为输出中的小数分隔符)。

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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文