Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert this scientific notation to decimal?

Tags:

c#

decimal

After search in google, using below code still can not be compiled:

decimal h = Convert.ToDecimal("2.09550901805872E-05");     decimal h2 = Decimal.Parse(   "2.09550901805872E-05",     System.Globalization.NumberStyles.AllowExponent); 
like image 979
ControlPoly Avatar asked Jul 11 '13 07:07

ControlPoly


People also ask

How do you convert to a decimal?

How to Convert a Percent to a Decimal. Divide a percent by 100 and remove the percent sign to convert from a percent to a decimal. The shortcut way to convert from a percentage to a decimal is by removing the percent sign and moving the decimal point 2 places to the left.

How do you write 0.00001 in scientific notation?

Hence, the scientific notation of \[0.00001\] is $ 1 \times {10^{ - 5}} $ . So, the correct answer is “ $ 1 \times {10^{ - 5}} $ ”. Note: Scientific notation is a way of expressing numbers that are too large or too small to be conveniently written in decimal form.


2 Answers

You have to add NumberStyles.AllowDecimalPoint too:

Decimal.Parse("2.09550901805872E-05", NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint); 

MSDN is clear about that:

Indicates that the numeric string can be in exponential notation. The AllowExponent flag allows the parsed string to contain an exponent that begins with the "E" or "e" character and that is followed by an optional positive or negative sign and an integer. In other words, it successfully parses strings in the form nnnExx, nnnE+xx, and nnnE-xx. It does not allow a decimal separator or sign in the significand or mantissa; to allow these elements in the string to be parsed, use the AllowDecimalPoint and AllowLeadingSign flags, or use a composite style that includes these individual flags.

like image 182
MarcinJuraszek Avatar answered Oct 18 '22 19:10

MarcinJuraszek


use System.Globalization.NumberStyles.Any

decimal h2 = Decimal.Parse("2.09550901805872E-05", System.Globalization.NumberStyles.Any); 
like image 26
Damith Avatar answered Oct 18 '22 19:10

Damith