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

前端 未结 19 1291
孤街浪徒
孤街浪徒 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:05

    string testString1 = "2,457";
    string testString2 = "2.457";    
    double testNum = 0.5;
    char decimalSepparator;
    decimalSepparator = testNum.ToString()[1];
    
    Console.WriteLine(double.Parse(testString1.Replace('.', decimalSepparator).Replace(',', decimalSepparator)));
    Console.WriteLine(double.Parse(testString2.Replace('.', decimalSepparator).Replace(',', decimalSepparator)));
    

提交回复
热议问题