Checking if an object is a number in C#

前端 未结 11 1573
粉色の甜心
粉色の甜心 2020-11-29 19:35

I\'d like to check if an object is a number so that .ToString() would result in a string containing digits and +,-,.

11条回答
  •  既然无缘
    2020-11-29 20:12

    Assuming your input is a string...

    There are 2 ways:

    use Double.TryParse()

    double temp;
    bool isNumber = Double.TryParse(input, out temp);
    

    use Regex

     bool isNumber = Regex.IsMatch(input,@"-?\d+(\.\d+)?");
    

提交回复
热议问题