简体   繁体   中英

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"

Looking at this expression in isolation you can see that something is wrong. Why are you calling ToString() on a string? Calling ToString() on a string simply returns the original string.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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