如何在 Visual Basic 中将字符串转换为整数?
问题描述:
如何将字符串转换为整数?这是我尝试过的:
How do I convert from a string to an integer? Here's what I tried:
Price = CInt(Int(txtPrice.Text))
我取出了Int
,还是出现异常.
I took out the Int
and I still got an exception.
答
使用
Convert.toInt32(txtPrice.Text)
这是假设 VB.NET.
从名称txtPrice"来看,您确实不想要整数而是小数.所以改为使用:
Judging by the name "txtPrice", you really don't want an Integer but a Decimal. So instead use:
Convert.toDecimal(txtPrice.Text)
如果是这种情况,请确保您将其分配给十进制而不是整数.
If this is the case, be sure whatever you assign this to is Decimal not an Integer.