Thursday, May 31, 2012

String to Double issues in C# .NET


If you are simply using 

double test = Double.Parse("12.45");

and it doesn't work due to formating issues, here is the right post for you. Almost certainly you're using the wrong CultureInfo. Try specifying CultureInfo.InvariantCulture to parse in. I suspect it's currently assuming that "." is a thousands separator rather than a decimal point.

CultureInfo Invc = new CultureInfo("");
Invc = CultureInfo.InvariantCulture;

x1 = Double.Parse("12.45", Invc);

alternative:

x1 = Double.Parse("12.45", CultureInfo.InvariantCulture);

No comments:

Post a Comment