Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the string 3.0E-4 to decimal

Tags:

c#

decimal

I have a string "3.0E-4", it is supposed to be a decimal number.

Please advise how to convert to a decimal.

like image 420
khoailang Avatar asked Dec 19 '22 22:12

khoailang


2 Answers

You can use AllowExponent and AllowDecimalPoint styles combination with decimal.Parse method like;

var result = decimal.Parse("3.0E-4", 
                           NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint, 
                           CultureInfo.InvariantCulture);

enter image description here

like image 197
Soner Gönül Avatar answered Dec 21 '22 12:12

Soner Gönül


Try this:

decimal x = Decimal.Parse("3.0E-4", NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint);

or like

decimal x = Decimal.Parse("3.0E-4", NumberStyles.Any, CultureInfo.InvariantCulture);
like image 43
Rahul Tripathi Avatar answered Dec 21 '22 11:12

Rahul Tripathi