0

My c.ShareWhite is a string, but i cant convert this to double with using Double.Parse

double a = Double.Parse(c.ShareWhite.ToString());

error:

nhandled exception. System.FormatException: Input string was not in a correct format.
   at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
   at System.Double.Parse(String s)

can someone tell ne how to parse string to double?

4
  • 3
    What value c.ShareWhite.ToString() gives you? What culture is set? Commented Jun 20, 2020 at 13:41
  • Can you show us what does Console.WriteLine(c.ShareWhite) show? Commented Jun 20, 2020 at 13:45
  • Please tell us what is the content of ShareWhite Commented Jun 20, 2020 at 13:45
  • double a=Convert.ToDouble(c.ShareWhite.ToString()); try this Commented Jun 20, 2020 at 14:03

1 Answer 1

2

First of all, I recommend using TryParse() method which is implemented for type wrapper classes like Int32, Double, ...etc.

Docs here: https://learn.microsoft.com/en-us/dotnet/api/system.double.tryparse?view=netcore-3.1

Basically it returns a bool indicating if the parse was succesfull instead of breaking the program with an exception and places the result of the conversion into the given variable.

double d;
bool res = Double.TryParse(c.ShareWhite, out d);

You have to make sure your string is a valid double and not anything else nor an empty string because both cases can give you format exception if you use a simple Parse method.

You should pick a breakpoint in your IDE before the step where you try to parse (https://learn.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2019) and check what kind of string value is passed to the (Try)Parse method.

It can be the some steps before or complelety from the beginning you are working with a bad value.

Sign up to request clarification or add additional context in comments.

3 Comments

have you checked your code , there is no overload method which is taking second parameter as out ..refer learn.microsoft.com/en-us/dotnet/api/…
It clearly says in the start of the answer that it's TryParse, not Parse
"type wrapper classes" isn't a thing. There's .NET types and c# type aliases for primitives, but they still refer to the same underlying types. It's not Java where such a thing does exist