![](/img/trans.png)
[英]How to convert String to Integer for calculations in Visual Basic
[英]How do I convert from a string to an integer in Visual Basic?
如何從字符串轉換為整數? 這是我試過的:
Price = CInt(Int(txtPrice.Text))
我拿出了Int
,我仍然有例外。
使用
Convert.toInt32(txtPrice.Text)
這是假設VB.NET 。
從名稱“txtPrice”來判斷,你真的不想要一個Integer而是一個Decimal。 所以改為使用:
Convert.toDecimal(txtPrice.Text)
如果是這種情況,請確保您指定的是十進制而不是整數。
你可以嘗試一下:
Dim Price As Integer
Int32.TryParse(txtPrice.Text, Price)
請試試這個,VB.NET 2010:
Integer.TryParse(txtPrice.Text, decPrice)
decPrice = Convert.ToInt32(txtPrice.Text)
來自Mola Tshepo Kingsley(WWW.TUT.AC.ZA)
Convert.ToIntXX不喜歡傳遞小數字串。
為了安全使用
Convert.ToInt32(Convert.ToDecimal(txtPrice.Text))
你可以試試這些:
Dim valueStr as String = "10"
Dim valueIntConverted as Integer = CInt(valueStr)
另一個例子:
Dim newValueConverted as Integer = Val("100")
使用Val(txtPrice.text)
我還會通過在價格文本框的按鍵事件中插入一些驗證代碼來僅允許數字和點字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.