Hit enter after type your search item

How to convert a string to int in C#?

The string to int conversion may be required for different reasons. Commonly, this is required if you are taking the user input for numbers and this may be taken as “text” or string. So, you have to make sure before performing any actions based on that data to convert that string into int, float or other numeric data types.

In  C#, there are different ways of converting a string to int, float, long etc.

  • Parse or TryParse methods
  • Using the Convert class methods e.g. ToDecimal, ToInt16, ToInt32, ToInt64 etc.

The section below shows you how to use these ways for converting the string to numbers.

The example of using Parse/TryParse for string to int conversion

The example below shows converting a string to int by using the Parse method. In the code, a positive and a negative “string” numbers are converted by int32.Parse method:

The Code:

The output of above code:

c_string to int

This is how Parse and TryParse methods work:

  • Both ignore the leading and trailing spaces in the given string.
  • If space exists between characters in a string then an error will generate in case of Parse method. If you are using TryParse method then it returns false.
  • The characters in the given string to be converted into numbers must form the appropriate numeric type. For example, “101”, “110.45”.
  • These numbers will produce an error in case of Parse e.g. “1 01”, “110 .45” as a space character exists. In the case of TryParse, it returns false.

The example of converting a decimal string

Similarly, by using the float keyword with Parse method, you may convert a decimal string to float number in C#.

As such, float specifies a 32-bit floating point value, the precision can be 7 digits. If your string with the decimal number is greater than 7 precision then use the double data type.

See the code below for learning how to convert a string with the decimal number to float by Parse method:

The output:

c# string-to-float

The example of using TryParse

The TryParse method is useful for conversion if there are chances of an incorrect string as a number. The Parse method can also be programmed, however, the TryParse has built-in capability by returning a Boolean as return value.

For example, you are taking the user input from a text box and there are chances that the entered number may contain spaces or characters other than valid numbers. In that case, if TryParse method is used, it will return false.

The simple usage of TryParse method is:

bool var1 = int.TryParse(string_number, out number)

See a simple example below to display the converted number by using the TryParse method:

As the string contains a valid number, so it is converted and its value is assigned to the int_conv variable.

What if a string is not a valid number?

Now, have a look at the code and output below if the string to be converted into number is a non-valid number:

You can see, I set the value of the string variable as 101A, which is an invalid number. Unlike the Parse method, this did not generate an error. Instead, it returned the false value that we dealt with in the if..else block.

The output of the above code should be:

An invalid string!

An example of string to int by Convert class

By using Convert class methods, you may convert a string to numbers. For a string to int conversion, use the Convert.ToInt32 method. For converting a string “number” to decimal, use the ToDecimal, ToDouble etc.

In the following example, a string is converted by Convert.ToInt32(String) to an integer number. Have a look:

The output:

Result after Convert method = 1010

Following numeric conversion methods are also available in the Convert class:

  • For short type : ToInt16(str)
  • For Integer : ToInt32(str)
  • For long type: ToInt64(str)

The example of string to float by Convert class

Similarly, you may use the ToSingle() method of the Convert class for converting a “decimal string” to float.

See the following example:

The result:

Float Result = 555.5555

For greater precision, use the ToDouble(str) method.

This div height required for enabling the sticky sidebar