2

Possible Duplicate:
Parse a Number from Exponential Notation
Does Decimal.Parse() support scientific notation?

I am trying to convert such values as 1E-08 to a decimal in C# because decimals are the preffered datatype for handling funds yet I get an error upon decimal.Parse() "Input string was not in a correct format." wouldn't converting to float first and then to decimal defeat the purpose?

Community
  • 1
  • 1
user1841964
  • 101
  • 1
  • 2
  • 8

2 Answers2

6

Yes, converting to float would indeed defeat the purpose. The good thing is, you don't have to do that here!

You can use an overload for Parse that takes a NumberStyles specifier:

decimal d = decimal.Parse("1E-08",
     System.Globalization.NumberStyles.AllowExponent);

Of course, if you are merely specifying a hard-coded decimal, you can use the decimal literal format:

decimal d = 1E-08M;
akatakritos
  • 9,836
  • 1
  • 23
  • 29
4

You can try with :-

 decimal x = decimal.Parse("1E-08", NumberStyles.Float);
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331