-2

I am having this kind of error: Input string was not in a correct format

Looking from debug I can see that this is the line that is causing the error:

double num = Math.Round(double.Parse(value), 4);

I have made some researches but my piece of code looks like is correct, so I am still not getting where I am doing wrong.

Any idea?

Ugnius Malūkas
  • 2,649
  • 7
  • 29
  • 42
Lara
  • 179
  • 2
  • 11
  • 6
    `value` is not a number. Please share with us what it is. – mjwills May 17 '18 at 07:51
  • Error "Input string was not in a correct format" is telling you the reason. Did you value is a number or string? – Saadi May 17 '18 at 07:51
  • 2
    Probably as mjwills said, problem of `,` vs `.` as decimal separator. – xanatos May 17 '18 at 07:52
  • string value="IND.0.F26.T1.C1.1 (1|3)"; is a cell code since the program converts .xlx to .xml – Lara May 17 '18 at 07:57
  • 3
    And what number you expect "IND.0.F26.T1.C1.1 (1|3)" to be converted to? – Evk May 17 '18 at 08:01
  • Any String being parsed may only contain digits, no letters or any other characters. You need to retrieve the value from the cell first and than call the function on that retrieved value. – tgr May 17 '18 at 08:01
  • The example string contains mixed alphanumeric values, use `Split` or `Substring` to search possible numeric values before using `double.Parse`. – Tetsuya Yamamoto May 17 '18 at 08:07
  • @TetsuyaYamamoto and how can i do it? – Lara May 17 '18 at 08:13
  • What exactly numeric value you want to parse with? Usually `double` value uses floating-point notation with decimal separator, if you want to convert string to integer value use `int.Parse`. – Tetsuya Yamamoto May 17 '18 at 08:17
  • `how can i do it?` How can you do **what**? What **exact** number do you expect for the value `IND.0.F26.T1.C1.1 (1|3)`? – mjwills May 17 '18 at 08:35

1 Answers1

0

You can use the following code to get the expected results without it throwing an error.

double number;
if (Double.TryParse(value, out number))
{
   // If the value is parsed as double (is ok).
} else {
   // If the value is not parsed as number (like an error).
}
Nyerguds
  • 5,360
  • 1
  • 31
  • 63
Jamez Fatout
  • 402
  • 4
  • 13
  • i am getting the number value from this row double num = Math.Round(double.Parse(value), 4); , so how is possible for me to use your code that checks if number is parsed or not – Lara May 17 '18 at 08:06
  • if(Double.TryParse(yourValue, out number)) { .... } – Jamez Fatout May 17 '18 at 09:35