【发布时间】:2014-07-16 09:11:47
【问题描述】:
我正在使用这行代码将字符串转换为int16、int32、double等类型:
var convertedValue = value == null ? null : Convert.ChangeType(value, targetType, CultureInfo.InvariantCulture);
谁能向我解释为什么convertedValue 设置为例如205.0 当value = "20,5" 和targetType 是双倍的?
感谢您的帮助。
【问题讨论】:
-
这是什么意思,
,不是InvariantCulture中的小数分隔符 -
是的,我也想通了。调试时,我输入了我的 GUI 20.5,但在其他地方检索值时,ToString() 方法将此值更改为“20,5”。奇怪
-
为什么奇怪?
ToString()将尊重当前的文化。当您执行Convert.ChangeType时,您明确地覆盖了它。doubles 没有什么文化可以随身携带。 -
使用 CurrentCulture,但不是不变量,因为十进制的 afaik ToString(当然也是双精度)使用 CurrentCulture
-
您希望发生什么?
标签: c# type-conversion