How can I convert String to Int?

后端 未结 30 2264
情歌与酒
情歌与酒 2020-11-21 05:35

I have a TextBoxD1.Text and I want to convert it to an int to store it in a database.

How can I do this?

30条回答
  •  自闭症患者
    2020-11-21 06:20

    You can do like below without TryParse or inbuilt functions:

    static int convertToInt(string a)
    {
        int x = 0;
        for (int i = 0; i < a.Length; i++)
        {
            int temp = a[i] - '0';
            if (temp != 0)
            {
                x += temp * (int)Math.Pow(10, (a.Length - (i+1)));
            }
        }
        return x;
    }
    

提交回复
热议问题