4

I am facing problem while converting string to decimal. My C# code is reading the following data from an exel file and throwing me the following error

Input string was not in the correct format "-1.7999999999999999E-2".

I can't see this value in my excel value.

Example data

-1.70152
9.335628333
-150.0145233
39.159625

The c# code

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

Is my conversion correct. I am loosing out any values during the conversion. Whats the best datataype to convert to inorder to retain the original value as it is?

diiN__________
  • 7,393
  • 6
  • 42
  • 69
Tom
  • 8,175
  • 41
  • 136
  • 267

2 Answers2

2

Your approach must be:

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

Even better you should use Decimal.TryParse and you should specify InvariantCulture

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

TestModel.lat = d;
mybirthname
  • 17,949
  • 3
  • 31
  • 55
-3

1) open ur excel 2) click on any cell which have this value 3)right click on cell->format cell-> select general and click ok.it will show you your original value.do changes to all cells. 4) now try to read with this file with ur code