3 Ways to Convert String to Int or Number in C#

In this article you will learn:
  • How to convert the string to a number or int in c#

When programming using c# most of the time your code required to convert String into Number or Int. Most of the time after converting you might get input string was not in a correct format error. In this article, I will show you how to convert a string into number or int without getting any error in c sharp (c#).

Following Exception Generated when converting one datatype into another.

Exception Condition
FormatException value is not a number in a valid format.
OverflowException value represents a number that is less than MinValue or greater than MaxValue.

Conversion Method 1: Using Int32.Parse() Method.

using System;

namespace ConvertStringtoNumber
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string s1="10";
            string s2="20";
            int num1=Int32.Parse(s1);
            int num2=Int32.Parse(s2);

            int add=num1+num2;
            Console.WriteLine("Addition of 10 and 20 is {0}",add.ToString());

        }
    }
}
Ouput
Addition of 10 and 20 is 30
_

Conversion Method 2: Using TryPars() Method:

Int32.TryPars(string s, out int number) takes 2 parameter. First parameter is string which you want to convert into number and second parameter is integer variable that will store converted value.

using System;

namespace ConvertStringtoNumber
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string s1="10";
            string s2="20";
            int num1=0,num2=0;
            Int32.TryParse(s1, out num1);
            Int32.TryParse(s2, out num2);

            int add=num1+num2;
            Console.WriteLine("Addition of 10 and 20 is {0}",add.ToString());

        }
    }
}
Output
Addition of 10 and 20 is 30
_

Conversion Method 3: Using Convert method:

Programming Example

using System;

namespace ConvertStringtoNumber
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string s1="10";
            string s2="20";
            int num1=0,num2=0;
            
            num1=Convert.ToInt32(s1);
            num2=Convert.ToInt32(s2);

            int add=num1+num2;
            Console.WriteLine("Addition of 10 and 20 is {0}",add.ToString());

        }
    }
}
Output
Addition of 10 and 20 is 30
_

Other Datatype Conversion

1. Decimal – ToDecimal(string)
decimal num1=Convert.ToDecimal("10");
2. Float – ToSingle(string)
float num1=Convert.ToSingle("10");
3. double – ToDouble(string)
double num1=Convert.ToDouble("10");
4. short – ToInt16(string)
short num1=Convert.ToInt16("2");
5. int – ToInt32(string)
int num1=Convert.ToInt32("10");
6. long – ToInt64(string)
long num1=Convert.ToInt64("10");
7. ushort - ToUInt16(String)
ushort num1=Convert.ToUInt16("10");
8. uint - ToUInt32(String)
uint num1=Convert.ToUInt32("10");
9. ulong - ToUInt64(String)
ulong num1=Convert.ToUInt64("10");

Summary

In this tutorial you learned how can you convert string into int or number. You also learned to convert string into different data type in c# like decimal, float, double, short, int, long, ushort, uint and ulong with complete programming example. You also learn which type of format can be generated during this type of conversion how can handle them.

More Articles

 

Share your thought