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

前端 未结 8 1115
广开言路
广开言路 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:07

    Think simple.

    decimal decNumber = decimal.Parse("9.99");
    if (decNumber % 1 > 0)
    {
       //decimal area
    }
    else
    {
       //int area
    }
    
    0 讨论(0)
  • 2020-12-09 18:08

    This simple code will allow integer or decimal value and rejects alphabets and symbols.

          foreach (char ch in strOrderId)
            {
                if (!char.IsDigit(ch) && ch != '.')
                {
    
                  MessageBox.Show("This is not a decimal \n");
                  return;
                }
               else
               {
               //this is a decimal value
               }
    
            }
    
    0 讨论(0)
提交回复
热议问题