Convert numbers with exponential notation from string to double or decimal
Solution 1
If your culture uses .
as the decimal separator, just double.Parse("1.50E-15")
should work.
If your culture uses something else (e.g. ,
) or you want to make sure your application works the same on every computer, you should use InvariantCulture
:
double.Parse("1.50E-15", CultureInfo.InvariantCulture)
Solution 2
The standard double.Parse
or decimal.Parse
methods do the job here.
Examples:
// AllowExponent is implicit
var number1 = double.Parse("0.5e10");
Debug.Assert(number1 == 5000000000.0);
// AllowExponent must be given explicitly
var number2 = decimal.Parse("0.5e10", NumberStyles.AllowExponent);
Debug.Assert(number2 == 5000000000m);
Also, see the MSDN article Parsing Numeric Strings for more information. As long as the NumberStyles.AllowExponent
option is specified to the Parse
method (which it is by default for double
), parsing such strings will work fine.
NB: As the questioner points out, the exponential notation of "e10" for example does not work in all cultures. Specifying en-US culture however ensures that it works. I suspect CultureInfo.InvariantCulture
should also do the trick.
Solution 3
@Noldorin is correct try this code:
string str = "-5e20";
double d = double.Parse(str);
Console.WriteLine(str);
Solution 4
the Math.Round does it well, it will reder the number so that will remove, here is how to use it:
Math.Round(Double.Parse("3,55E-15"),2)
Related videos on Youtube

OMGKurtNilsen
Updated on July 09, 2022Comments
-
OMGKurtNilsen 4 months
Is there a fast way to convert numbers with exponential notation (examples: "0.5e10" or "-5e20") to decimal or double?
Update: I found Parse a Number from Exponential Notation but the examples won't work for me unless I specified a culture.
Solution:
double test = double.Parse("1.50E-15", CultureInfo.InvariantCulture);
-
OMGKurtNilsen about 11 years@Dylan double test = double.Parse("1.509e10", NumberStyles.Float); // "Input string was not in a correct format."
-
Noldorin about 11 yearsFor
decimal.Parse
you need to specify the NumberStyles explicitly. -
Noldorin about 11 yearsThanks for confirming. This should probably belong as a comment though, as it's just re-iterating what I've done. :-)
-
Kevin Holditch about 11 yearsI know I did originally add as comment but then realised you couldnt put a code block in a comment
-
Noldorin about 11 yearsNo worries. Feel free to edit my answer, after all (you have the privelege at your rep yes?). If not, the example is there now. We probably added them at the same time.
-
OMGKurtNilsen about 11 yearsI think this wouldn't work because of my culture. When I specified "en-US" culture it worked.
-
Noldorin about 11 years@OMGKurtNilsen: Makes sense... InvariantCulture might work too.
-
OMGKurtNilsen about 11 yearsIt did work. Will edit my solution. You should also add it to your answer.
-
OMGKurtNilsen about 11 yearsAlready figured this out, but this was the first working answer.
-
svick about 11 yearsYeah, I noticed. Next time, you should post the solution as an answer and not edit the question.
-
Noldorin about 11 years@OMGKurtNilsen: Done. Hopefully that's enough for an acceptance now. :-) Ta.