Parsing a number from an exponential notation

I need to parse the string "1.2345E-02" (a number expressed in exponential notation) into a decimal data type, but Decimal.Parse("1.2345E-02") just throws an error

+74
c # number-formatting exponent
07 Oct '10 at 7:18
source share
8 answers

This is a floating point number, you should say that:

 decimal d = Decimal.Parse("1.2345E-02", System.Globalization.NumberStyles.Float); 
+153
Oct 07 2018-10-10T00:
source share

It works if you specify NumberStyles.Float :

 decimal x = decimal.Parse("1.2345E-02", NumberStyles.Float); Console.WriteLine(x); // Prints 0.012345 

I'm not quite sure why this is not supported by default - the default is NumberStyles.Number , which uses the styles AllowLeadingWhite, AllowTrailingWhite, AllowLeadingSign, AllowTrailingSign, AllowDecimalPoint and AllowThousands. Perhaps this is due to performance; specifying an exponent is relatively rare, I suppose.

+44
Oct 07 '10 at 7:23
source share

In addition to specifying NumberStyles, I would recommend using the decimal.TryParse function, such as:

 decimal result; if( !decimal.TryParse("1.2345E-02", NumberStyles.Any, CultureInfo.InvariantCulture, out result) ) { // do something in case it fails? } 

As an alternative to NumberStyles.Any, you can use a specific set if you are confident in your formats. eg:

 NumberStyles.AllowExponent | NumberStyles.Float 
+31
Feb 27 '12 at 12:17
source share
 decimal d = Decimal.Parse("1.2345E-02", System.Globalization.NumberStyles.Float); 
+11
Oct. 07 '10 at 7:24
source share

Be careful with the answer you choose: there is a subtext defining System.Globalization.NumberStyles.Float in Decimal.Parse, which can throw a System.FormatException because your system can expect a number formed by using ',' instead of '.

For example, in the French notation "1.2345E-02" is not valid, you must first convert it to "1.2345E-02".

In conclusion, use something line by line:

 Decimal.Parse(valueString.Replace('.',','), System.Globalization.NumberStyles.Float); 
+7
Nov 21 '16 at 11:11
source share

I found that passing to NumberStyles.Float , in some cases, changes the rules by which the string is processed, and leads to another output from NumberStyles.Number (the default rules used by decimal.Parse ).

For example, the following code will throw a FormatException in my machine:

 CultureInfo culture = new CultureInfo(""); culture.NumberFormat.NumberDecimalDigits = 2; culture.NumberFormat.NumberDecimalSeparator = "."; culture.NumberFormat.NumberGroupSeparator = ","; Decimal.Parse("1,234.5", NumberStyles.Float, culture); // FormatException thrown here 

I would recommend using the NumberStyles.Number | NumberStyles.AllowExponent input NumberStyles.Number | NumberStyles.AllowExponent NumberStyles.Number | NumberStyles.AllowExponent , as this will allow exponential numbers and will process the string according to decimal rules.

 CultureInfo culture = new CultureInfo(""); culture.NumberFormat.NumberDecimalDigits = 2; culture.NumberFormat.NumberDecimalSeparator = "."; culture.NumberFormat.NumberGroupSeparator = ","; Decimal.Parse("1,234.5",NumberStyles.Number | NumberStyles.AllowExponent, culture); // Does not generate a FormatException 

To answer the question about the poster, the correct answer should be:

 decimal x = decimal.Parse("1.2345E-02", NumberStyles.Number | NumberStyles.AllowExponent); Console.WriteLine(x); 
+3
Mar 09 '17 at 17:50
source share

Warning about using NumberStyles.Any:

"6.33E + 03" will convert to 6330, as expected. In German, decimal points are represented by commas, but 6.33E + 03 converts to 633000! This is a problem for my clients, because the culture that generates data is unknown and may differ from the culture that works with data. In my case, I always have scientific notation, so I can always replace the comma with a decimal point before parsing, but if you work with arbitrary numbers, for example, in pretty-formatted numbers like 1,234,567, then this approach does not work.

+1
Dec 18 '15 at 23:48
source share

You do not need to replace the dots (respectively commas), just specify the input IFormatProvider:

 float d = Single.Parse("1.27315", System.Globalization.NumberStyles.Float, new CultureInfo("en-US")); float d = Single.Parse("1,27315", System.Globalization.NumberStyles.Float, new CultureInfo("de-DE")); 
0
Jun 26 '19 at 12:55 on
source share



All Articles