Conversion error in C # throw exception input line in invalid format

I ran into a problem while converting a string to decimal. My C # code reads the following data from an exel file and throws me the following error

The input string was not in the correct format "-1.799999999999999999E-2".

I do not see this value in my excel value.

Data examples

-1.70152
9.335628333
-150.0145233
39.159625

C # code

TestModel.lat = Convert.ToDecimal(c.CellValue.Text);

Is my conversion correct. I lose any values ​​during the conversion. What is the best datataype to convert to inorder to keep the original value as it is?

+4
source share
2 answers

Your approach should be:

TestModel.lat = Decimal.Parse(c.CellValue.Text, NumberStyles.Float);

Use is even better Decimal.TryParse, and you should specifyInvariantCulture

decimal d = 0;
Decimal.TryParse("1.2345E-02", System.Globalization.NumberStyles.Float, CultureInfo.InvariantCulture, out d);

TestModel.lat = d;
+2
source

1) ur excel 2) , 3) - > - > ok.it .do . 4) ur

-3

Source: https://habr.com/ru/post/1663494/


All Articles