How to convert this scientific notation to decimal?

后端 未结 6 1141
误落风尘
误落风尘 2020-12-11 00:38

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

decimal h = Convert.ToDecimal(\"2.09550901805872E-05\");   

decimal h2 = Decima         


        
6条回答
  •  情歌与酒
    2020-12-11 01:00

    Since decimal separator ("." in your string) can vary from culture to culture it's safier to use InvariantCulture. Do not forget to allow this decimal separator (NumberStyles.Float)

      decimal h = Decimal.Parse(
        "2.09550901805872E-05", 
         NumberStyles.Float | NumberStyles.AllowExponent,
         CultureInfo.InvariantCulture);
    

    Perharps, more convenient code is when we use NumberStyles.Any:

      decimal h = Decimal.Parse(
        "2.09550901805872E-05", 
         NumberStyles.Any, 
         CultureInfo.InvariantCulture);
    

提交回复
热议问题