Checking if a variable is of data type double

后端 未结 10 1351
無奈伤痛
無奈伤痛 2021-02-08 02:19

I need to check if a variable I have is of the data type double. This is what I tried:

try
{
    double price = Convert.ToDouble(txtPrice.Text);
}
c         


        
10条回答
  •  生来不讨喜
    2021-02-08 02:56

    Use the Double.TryParse method:

    double price;
    if (Double.TryParse(txtPrice.Text, out price))
    {
        Console.WriteLine(price);
    }
    else
    {
        Console.WriteLine("Not a double!");
    }
    

提交回复
热议问题