How do I parse a string with a decimal point to a double?

前端 未结 19 1294
孤街浪徒
孤街浪徒 2020-11-22 06:47

I want to parse a string like \"3.5\" to a double. However,

double.Parse(\"3.5\") 

yields 35 and

double.Pars         


        
19条回答
  •  礼貌的吻别
    2020-11-22 07:24

            var doublePattern = @"(?[0-9]+)(?:\,|\.)(?[0-9]+)";
            var sourceDoubleString = "03444,44426";
            var match = Regex.Match(sourceDoubleString, doublePattern);
    
            var doubleResult = match.Success ? double.Parse(match.Groups["integer"].Value) + (match.Groups["fraction"].Value == null ? 0 : double.Parse(match.Groups["fraction"].Value) / Math.Pow(10, match.Groups["fraction"].Value.Length)): 0;
            Console.WriteLine("Double of string '{0}' is {1}", sourceDoubleString, doubleResult);
    

提交回复
热议问题