2011-10-10 93 views

回答

61

使用

Convert.toInt32(txtPrice.Text) 

這是假設VB.NET

通過名稱「txtPrice」判斷,你真的不想要一個Integer而是一個Decimal。所以改爲使用:

Convert.toDecimal(txtPrice.Text) 

如果是這種情況,請確保無論您將其分配給什麼是十進制不是整數。

+1

在Vb.net中。此代碼將得到一個異常'輸入字符串的格式不正確。 –

+0

我剛試過,但沒有奏效。這次我收到格式錯誤。我仍在嘗試。我得到了差不多一個半小時, – swydell

+1

Dim Price As Integer Dim result As Boolean = Int32.TryParse(txtPrice.Text,Price) –

1

Convert.ToIntXX不喜歡被傳遞的小數串。

爲了安全使用

Convert.ToInt32(Convert.ToDecimal(txtPrice.Text)) 
18

,您可以嘗試:

Dim Price As Integer 
Int32.TryParse(txtPrice.Text, Price) 
+2

我認爲這確實是最好的答案。使用Int32或Decimal的TryParse()方法將能夠處理輸入到TextBox中的其他內容,例如貨幣符號和此類事物。它也不會拋出異常,而只是返回一個布爾值來指示它是否通過。 –

1

請試試這個,VB.NET 2010:

  1. Integer.TryParse(txtPrice.Text, decPrice)
  2. decPrice = Convert.ToInt32(txtPrice.Text)

從莫拉Tshepo金斯利(WWW.TUT.AC.ZA)

1

你可以試試這些:

Dim valueStr as String = "10" 

Dim valueIntConverted as Integer = CInt(valueStr) 

又如:

Dim newValueConverted as Integer = Val("100") 
相關問題