Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert numbers with exponential notation from string to double or decimal

Tags:

c#

.net

exponent

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);
like image 502
OMGKurtNilsen Avatar asked Oct 24 '11 15:10

OMGKurtNilsen


People also ask

How do you convert an exponential number to in SQL?

For SQL Server 2012+, use TRY_CONVERT(). Conversion failed when converting the varchar value '3.3733E+15' to data type int. The issue is that all values in the 'a' column return 1 when passed to the ISNUMERIC() function. Show activity on this post. This should work.


1 Answers

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)
like image 53
svick Avatar answered Oct 04 '22 07:10

svick