Checking if a variable is of data type double

后端 未结 10 1217
無奈伤痛
無奈伤痛 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:51

    So if I get your question right, you mean you only want to allow numbers right? If that's true, then maybe this will help you.

    string Str = textBox1.Text.Trim();
    double Num;
    bool isNum = double.TryParse(Str, out Num);
    if (isNum)
    MessageBox.Show(Num.ToString());
    else
    MessageBox.Show("Invalid number");
    

提交回复
热议问题