73

How do I convert from a string to an integer? Here's what I tried:

Price = CInt(Int(txtPrice.Text))

I took out the Int and I still got an exception.

2
  • What kind of exception do you experience?if you're using vb.net your code delete the decimal places. Commented Oct 10, 2011 at 5:38
  • I use CInt() and experienced no problems Commented Apr 11, 2018 at 2:34

8 Answers 8

126

Use

Convert.toInt32(txtPrice.Text)

This is assuming VB.NET.

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.

Sign up to request clarification or add additional context in comments.

5 Comments

In Vb.net..This code will get an exception 'Input string was not in a correct format.;.
I just tried it but it didn't work. I got a format error this time. I'm still trying. I got almost 1 1/2 hours left,
Dim Price As Integer Dim result As Boolean = Int32.TryParse(txtPrice.Text, Price)
The only way I can get a format error with this is to actually have an error (enter a character/symbol). Fixing this would require simple input validation which isn't stated as a requirement.
This solution throws an error for me, and doesn't seem to be very robust for production because of this registry key issue support.microsoft.com/kb/942460 CInt(<string>) works well.
28

You can try it:

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

1 Comment

I think this is really the best answer. Using Int32 or Decimal's TryParse() method would be able to handle other things being entered into the TextBox, like currency symbols and things of that nature. It also won't throw an exception, but rather just return a boolean indicating whether it passed or failed.
14

You can use the following to convert string to int:

  • CInt(String) for ints
  • CDec(String) for decimals

For details refer to Type Conversion Functions (Visual Basic).

Comments

5

Please try this, VB.NET 2010:

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

From Mola Tshepo Kingsley (WWW.TUT.AC.ZA)

Comments

3

Convert.ToIntXX doesn't like being passed strings of decimals.

To be safe use

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

Comments

2

You can try these:

Dim valueStr as String = "10"

Dim valueIntConverted as Integer = CInt(valueStr)

Another example:

Dim newValueConverted as Integer = Val("100")

Comments

1

Use Val(txtPrice.text)

I would also allow only number and the dot char by inserting some validation code in the key press event of the price text box.

Comments

0

If there might be invalid characters in the textbox it will throw an exception. The Val command pulls numbers and strips invalid characters. It returns a double. So you want to convert the result of Val to whatever type you need.

Price = Convert.toInt32(Val(txtPrice.Text))

This will return 0 instead of throwing an error on invalid input. If that isn't desired you should be checking that the input is valid before you convert.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.