简体   繁体   English

C#中的双重类型解析错误

[英]Double type parsing error in C#

this is my code and i get this error: "Input string was not in a correct format." 这是我的代码,并且出现以下错误:“输入字符串的格式不正确。”

var variable1= double.Parse("0.03".ToString(CultureInfo.InvariantCulture));

but for this code i didn't get error: 但是对于此代码,我没有得到错误:

var variable1= double.Parse("0.03",CultureInfo.InvariantCulture);

what's the reason? 什么原因?

"0.03".ToString(CultureInfo.InvariantCulture)

evaluates to "0.03" 评估为"0.03"

Looking at this expression in isolation you can see that something is wrong. 孤立地看这个表达式,您会发现有些问题。 Why are you calling ToString() on a string? 为什么要在字符串上调用ToString() Calling ToString() on a string simply returns the original string. 在字符串上调用ToString()只会返回原始字符串。

Anyway, moving on. 无论如何,继续前进。 Your function call is therefore the same as 因此,您的函数调用与

double.Parse("0.03")

And that probably results in an error because your local decimal separator is not "." 这可能会导致错误,因为您的本地小数点分隔符不是"." .

You meant to write 你想写

double.Parse("0.03", CultureInfo.InvariantCulture)

You can use 您可以使用

double.Parse("0.03", CultureInfo.InvariantCulture)

Double.Parse Method (String, NumberStyles) Double.Parse方法(字符串,NumberStyles)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM