的ToString(" 0")与的ToString(CultureInfo.InvariantCulture) [英] ToString("0") versus ToString(CultureInfo.InvariantCulture)

查看:597
本文介绍了的ToString(" 0")与的ToString(CultureInfo.InvariantCulture)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,以确保某些数字在我的应用程序输出时没有任何分隔符,分组等,无论当前的环境是什么。似乎以下两种方法产生相同的结果(有可能更多):

I would like to make sure that certain numbers in my application are printed without any separators, groupings etc. no matter what the current environment is. It seems that the following two methods produce the same results (there are possibly more):

123456789.ToString("0");
123456789.ToString(CultureInfo.InvariantCulture);

您知道任何边缘情况或怪癖?哪一个更正确?哪一个你会用?

Are you aware of any edge cases or quirks? Which one is more "correct"? Which one would you use?

我以前用的是第二个,但最近我发现第一个并开始使用它,因为它不需要额外的使用System.Globalization

I used to use the second one, but recently I found the first one and started using it because it does not require the additional using System.Globalization.

推荐答案

根据答案,这里的讨论,我做了一些更多的调查。以下是我发现:

Based on the answers and the discussion here, I did some more investigation. Here is what I found:

  • 在使用 12345678.ToString()不带任何参数,.NET使用一般的通用格式说明G这如果只有NumberFormatInfo.NegativeSign,的NumberFormatInfo影响。 NumberDecimalSeparator,NumberFormatInfo.NumberDecimalDigits和NumberFormatInfo.PositiveSign。对我来说,这是说在任何文化中 12345678.ToString()应该总是产生12345678。

  • When you use 12345678.ToString() without any arguments, .NET uses general the general format specifier "G" which if affected only by NumberFormatInfo.NegativeSign, NumberFormatInfo. NumberDecimalSeparator, NumberFormatInfo.NumberDecimalDigits and NumberFormatInfo.PositiveSign. To me this says that in any culture 12345678.ToString() should always produce "12345678".

如果你想为独立的数字,您需要使用数字格式说明N(或者,当然,提供自定义格式字符串)。数字分组也适用于C和P。

If you want to separate digits, you need to use the numeric format specifier "N" (or, of course, to provide a custom format string). The digit grouping also applies to "C" and "P".

不变的文化确实确实指定数字分组(由3个数字,当然)和数字分隔。这样 123456789.ToString(CultureInfo.InvariantCulture)生产123456789的原因是不变的文化,不是因为,但它是因为默认的通用数值格式说明G的

The invariant culture does indeed specify digit grouping (by 3 digits, of course) and a digit separator. So the reason that 123456789.ToString(CultureInfo.InvariantCulture) produces "123456789" is not because of the invariant culture, but it’s because of the default general numeric format specifier "G".

所以,我要说的是,结论是,这是完全正常的不要担心任何额外的参数,只是使用

So I would say that the conclusion is that it’s perfectly OK not to worry about any extra arguments and just use:

12345678.ToString()

我认为这最好从所有的情况下,因为这意味着通常你不需要甚至称的ToString(),因为大多数的打印/写功能接受各种参数,做好的ToString()给你。

I think that this the best from all cases because it means that usually you don’t need to even call ToString() because most of the print/write functions accept all sorts of arguments and do the ToString() for you.

这篇关于的ToString(" 0")与的ToString(CultureInfo.InvariantCulture)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆