How to convert a string 3.0E-4 to decimal

I have a string "3.0E-4", it must be a decimal number.

Please advise how to convert to decimal.

+4
source share
3 answers

You can use AllowExponentand AllowDecimalPointa decimal.Parsemethod like;

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

enter image description here

+7
source

Try the following:

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

or how

decimal x = Decimal.Parse("3.0E-4", NumberStyles.Any, CultureInfo.InvariantCulture);
+3
source

.TryParse (.Parse , ):

void Main()
{
    var str="3.0E-4";
    float d;
    if (float.TryParse(str, out d))
    {
        Console.WriteLine("d = " + d.ToString());
    }
    else
    {
        Console.WriteLine("Not a valid decimal!");
    }
}

enter image description here

, TryParse.

+1

Source: https://habr.com/ru/post/1613840/


All Articles