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

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

    The below is less efficient, but I use this logic. This is valid only if you have two digits after decimal point.

    double val;
    
    if (temp.Text.Split('.').Length > 1)
    {
        val = double.Parse(temp.Text.Split('.')[0]);
    
        if (temp.Text.Split('.')[1].Length == 1)
            val += (0.1 * double.Parse(temp.Text.Split('.')[1]));
        else
            val += (0.01 * double.Parse(temp.Text.Split('.')[1]));
    }
    else
        val = double.Parse(RR(temp.Text));
    

提交回复
热议问题