C# : Check value stored inside string object is decimal or not

前端 未结 8 1116
广开言路
广开言路 2020-12-09 17:06

in C# , how can i check whether the value stored inside a string object( Ex : string strOrderId=\"435242A\") is decimal or not?

8条回答
  •  南方客
    南方客 (楼主)
    2020-12-09 18:04

    In case if we do not want use extra variable.

    string strOrderId = "435242A";
    
    bool isDecimal = isDecimal(strOrderId);
    
    
    public bool isDecimal(string value) {
    
      try {
        Decimal.Parse(value);
        return true;
      } catch {
        return false;
      }
    }
    

提交回复
热议问题