如何在 Visual Basic 中将字符串转换为整数? [英] How do I convert from a string to an integer in Visual Basic?

查看:21
本文介绍了如何在 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.

这篇关于如何在 Visual Basic 中将字符串转换为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆