回答
使用
Convert.toInt32(txtPrice.Text)
這是假設VB.NET。
通過名稱「txtPrice」判斷,你真的不想要一個Integer而是一個Decimal。所以改爲使用:
Convert.toDecimal(txtPrice.Text)
如果是這種情況,請確保無論您將其分配給什麼是十進制不是整數。
在Vb.net中。此代碼將得到一個異常'輸入字符串的格式不正確。 –
我剛試過,但沒有奏效。這次我收到格式錯誤。我仍在嘗試。我得到了差不多一個半小時, – swydell
Dim Price As Integer Dim result As Boolean = Int32.TryParse(txtPrice.Text,Price) –
Convert.ToIntXX不喜歡被傳遞的小數串。
爲了安全使用
Convert.ToInt32(Convert.ToDecimal(txtPrice.Text))
,您可以嘗試:
Dim Price As Integer
Int32.TryParse(txtPrice.Text, Price)
我認爲這確實是最好的答案。使用Int32或Decimal的TryParse()方法將能夠處理輸入到TextBox中的其他內容,例如貨幣符號和此類事物。它也不會拋出異常,而只是返回一個布爾值來指示它是否通過。 –
請試試這個,VB.NET 2010:
Integer.TryParse(txtPrice.Text, decPrice)
decPrice = Convert.ToInt32(txtPrice.Text)
從莫拉Tshepo金斯利(WWW.TUT.AC.ZA)
你可以試試這些:
Dim valueStr as String = "10"
Dim valueIntConverted as Integer = CInt(valueStr)
又如:
Dim newValueConverted as Integer = Val("100")
- 1. 在Visual Basic中將字符串轉換爲整數
- 2. 如何將字符串轉換爲日期? Visual Basic中
- 3. 在Visual Basic中將字符串轉換爲int數組?
- 4. 將字節轉換爲Visual Basic中的字符串
- 5. 打印整數字符串在Visual Basic
- 6. 從字符串轉換爲整數
- 7. Visual Basic中的字符串換行
- 8. 從字符串轉換爲整數引發異常。 Visual Studio '15
- 9. Visual Basic強制轉換 - 從字符串轉換爲類型'Double'無效
- 10. 從字符串中提取整數並轉換爲整數?
- 11. 如何將DataFrame的列名從字符串轉換爲整數?
- 12. 如何將字符串中的字節轉換爲整數? Python
- 13. Visual Basic將數組整數(x)轉換爲單個字符的x值
- 14. Visual Basic 6如何將整數轉換爲布爾值?
- 15. 從輸入轉換整數的字符串爲整數
- 16. 如何將字符串值轉換(或轉換)爲整數值?
- 17. 如何從整數隊列轉換爲字符串?
- 18. 如何將字符串/列表從文件轉換爲整數
- 19. 如何將時間「duration」從字符串值轉換爲整數?
- 20. 從串口讀取字符串Visual Basic
- 21. 如何將指數數字字符串轉換爲整數字符串
- 22. 如何將字符串轉換爲目標C中的整數?
- 23. 如何在Excel的PowerQuery中將整數轉換爲字符串
- 24. 如何將字符串轉換爲PHP中的整數?
- 25. 如何將字符串轉換爲PHP中的整數?
- 26. 如何將字符串轉換爲VHDL中的整數?
- 27. 如何將整數轉換爲gnuplot中的字符串?
- 28. 如何將整數轉換爲GLib中的字符串?
- 29. 如何將字符串轉換爲shell腳本中的整數?
- 30. 如何將字符串轉換爲iMacros中的整數?
你遇到什麼樣的異常,如果你用vb.net代碼刪除小數? –