将数字格式化为字符串,并用逗号代替小数
我有以下号码: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我想:
应该做自己想做的事。
I think:
should do what you want.
这取决于您想做什么。如果您想让整个应用程序为其他语言输出做好准备,只需使用
如果您想要一个混合语言应用程序(这意味着某些输出以一种语言格式化,另一些以另一种语言格式化),则必须使用特定语言的重载字符串函数如下:
如果可能的话,第一种方法是首选,因为您在应用程序中只需要一行,一切都会按预期进行。在第二个中,您必须将区域性参数添加到整个应用程序中的每个字符串输出方法。
This depends on what you want to do. If you want to make your entire application ready for some other language output just use
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:
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.
默认情况下,数字格式使用 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.
对于更通用的解决方案,您可以设置线程的 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.
使用带有逗号而不是小数点的 CultureInfo(例如法语):
Use a CultureInfo that has commas instead of decimal point (French for instance) :
是的,您使用了错误的字符串,而且仅提供格式化字符串也无法解决问题。
格式化字符串的作用是使用模式
"0"
格式化数字,然后与长度0
对齐。当您在格式字符串中指定小数分隔符时,无论当前区域性如何,它始终是句点。另一方面,格式化结果中的小数分隔符始终是当前区域性使用的分隔符。因此,要在结果中使用逗号作为小数分隔符,您必须确保用于格式化的区域性是使用逗号作为小数分隔符的区域性。
您可以设置线程的当前区域性,以便格式化时默认使用它,或者在调用中指定区域性:
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 length0
.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:
设置线程区域性将自动执行此转换。但是,如果您想使用自定义字符串对其进行格式化,您可以执行以下操作:
或
“.”。是当前区域性的小数点格式说明符。有关自定义数字格式的详细信息,请访问:http://msdn.microsoft。 com/en-us/library/0c899ak8.aspx
您可以随时检查小数点的字符是什么:
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:
or
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:
如果您使用正确的 文化在操纵数字时。
如果您想做“一次性”显示,则第二种方法可行。如果您想正确显示每个数字,您确实应该设置 当前线程的文化。这样,任何数字操作都将正确处理小数分隔符、分组字符和任何其他特定于文化的事物。
解析数字也是如此。如果用户输入
1,234
,您如何知道他们输入的是1.234
(逗号是小数分隔符)还是1234
(逗号是分组分隔符)?这就是区域性提供帮助的地方,因为它知道如何显示数字,也可以用来正确解析它们:上面将输出
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.
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 entered1.234
(the comma being the decimal separator) or1234
(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: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) and1,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).