问题描述
我运行的ReSharper的测试版本,而且它给我以下code警告:
内部ID;
// ...
DoSomethingWith(id.ToString());
该警告是关于 id.ToString()
调用,它告诉我:字符串转换指定一个明确的文化。我理解的警告,我知道如何解决它 - 只是改变了code到更笨重的 id.ToString(CultureInfo.InvariantCulture)
。 P>
不过,我的问题是:是那个必要吗?我的意思是,明明是很重要的指定你使用类型时,如的DateTime
(不同的文化有不同的日期格式)和双击$文化C $ C>(用于小数点不同的字符)。但
Int32.ToString()
,至少在EN-US,而不变的文化,不添加任何格式的。没有逗号,没有小数点,不美元符号,什么都没有。那么,会有由文化有什么不同?
是否有一些文化,实际上的添加的,当你调用某种格式的参数的 Int32.ToString()
?或者,这是在ReSharper的测试版中的错误,而这个警告还真是不适用的Int32
(在这种情况下,我将提交一份ReSharper的错误报告)?
操作系统允许修改负号的数字。
控制面板 - >
语言和区域设置 - >
其他设置 - >
负号
所以,当前区域性可能覆盖负号。在这种情况下,你需要尊重区域设置,这是该警告的原因。您还可以更改负号编程方式:
CultureInfo的文化= Thread.CurrentThread.CurrentCulture;
//使可写克隆
文化=(的CultureInfo)culture.Clone();
culture.NumberFormat.NegativeSign =!;
I'm running a beta version of ReSharper, and it's giving me warnings for the following code:
int id;
// ...
DoSomethingWith(id.ToString());
The warning is on the id.ToString()
call, and it's telling me "Specify a culture in string conversion explicitly". I understand the warning, and I know how to fix it -- just change the code to the much more unwieldy id.ToString(CultureInfo.InvariantCulture)
.
But my question is: is that necessary? I mean, obviously it's important to specify the culture when you're using types like DateTime
(different cultures have different date formats) and Double
(different characters used for the decimal point). But Int32.ToString()
, at least in the en-US and invariant cultures, doesn't add any formatting at all. No commas, no decimal points, no dollar signs, nothing. So what would there be to vary by culture?
Are there some cultures that actually add some sort of formatting when you call the parameterless Int32.ToString()
? Or is this a bug in the ReSharper beta, and this warning really isn't applicable to Int32
(in which case I'll file a ReSharper bug report)?
The Operating System allows to change the negative sign for numbers.
Control panel ->
Language and regional settings ->
Additional settings ->
Negative sign
So, current culture could have overridden the negative sign. In this case you need to respect the regional settings, this is the reason of the warning. You can also change the negative sign programatically:
CultureInfo culture = Thread.CurrentThread.CurrentCulture;
// Make a writable clone
culture = (CultureInfo) culture.Clone();
culture.NumberFormat.NegativeSign = "!";
这篇关于是Int32.ToString()特定文化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!