且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何检查给定的字符串是否为整数

更新时间:2021-07-14 07:10:42

使用它..
Int32.Parse [
use this..
Int32.Parse[^]


(1)存在Int32.TryParse [
(1) There''s the Int32.TryParse[^] method for that (please read the documentation).
(2) Once TryParse returns true you have the integer and you may test if it is less than zero.


尝试一下,

Try this,

int checkNumber;
if (int.TryParse(TextBox2.Text, out checkNumber) == false)
{
    //Entered number is not interger;
}
else
{
    if (Convert.ToInt16(TextBox2.Text) < 0)
    {
        //Entered no. is negative
    }
    else
    {
        //Entered no. is positive
    }
}