[英]Convert.ChangeType problems with passing a double
I am using this line of code to convert strings to int16
, int32
, double
and other types: 我正在使用以下代码
int16
字符串转换为int16
, int32
, double
和其他类型:
var convertedValue = value == null ? null : Convert.ChangeType(value, targetType, CultureInfo.InvariantCulture);
Can anyone explain to me why convertedValue
is set to eg 205.0
when value = "20,5"
and targetType
is double? 谁能向我解释为什么当
value = "20,5"
并且targetType
为double时, convertedValue
设置为例如205.0
?
Thank you for your help. 谢谢您的帮助。
Its a decimal separator. 它是一个十进制分隔符。 Like 20000 can be written in string as 20,000.
像20000一样可以写成字符串20,000。 Similarely when you convert 20,000 to double it will give you 20000.
类似地,当您将20,000转换为两倍时,将得到20000。
I had similar problem in my VB.Net project. 我在VB.Net项目中遇到了类似的问题。 The reason of this was that compiler treated values with comma (
"20,5"
) like strings, because there is no decimal point for decimal value, therefore it convert "20,5"
into 205
. 这是因为编译器将逗号(
"20,5"
)像字符串一样对待,因为十进制值没有小数点,因此将"20,5"
转换为205
。 I solved this issue by replacing comma with dot using 我通过使用点替换逗号来解决了这个问题
.ToString().Replace(",", ".")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.