Convert Long to Integer in C#

  1. Convert Long to Integer With the Type Casting Method in C
  2. Convert Long to Integer With the Convert.ToInt32() Method in C

This tutorial will discuss the methods of converting a long variable to an integer variable in C#.

Convert Long to Integer With the Type Casting Method in C

Type casting converts one data type to another. Since the long data type takes more bytes than the integer data type, we have to use the explicit type casting method for converting the long data type to the integer data type. See the following example.

using System;

namespace convert_long_to_int
{
    class Program
    {
        static void Main(string[] args)
        {
            long l = 12345;
            int i = (int)l;
            Console.WriteLine("long = {0}", l);
            Console.WriteLine("Integer = {0}", i);
        }
    }
}

Output:

long = 12345
Integer = 12345

In the above code, we used the explicit type casting operator (int) to convert the long variable l to the integer variable i. It will give you an incorrect result if l is larger than 231 - 1. Check the example below.

using System;

namespace convert_long_to_int
{
    class Program
    {
        static void Main(string[] args)
        {
            long l = 2147483647;
            int i = (int)l;
            Console.WriteLine("long = {0}", l);
            Console.WriteLine("Integer = {0}", i);
            
            l = 2147483648;
            i = (int)l;
            Console.WriteLine("long = {0}", l);
            Console.WriteLine("Integer = {0}", i);
            
            l = 2147483649;
            i = (int)l;
            Console.WriteLine("long = {0}", l);
            Console.WriteLine("Integer = {0}", i);
            
            l = 4147483649;
            i = (int)l;
            Console.WriteLine("long = {0}", l);
            Console.WriteLine("Integer = {0}", i);
            
        }
    }
}

Output:

long = 2147483647
Integer = 2147483647
long = 2147483648
Integer = -2147483648
long = 2147483649
Integer = -2147483647
long = 4147483649
Integer = -147483647

Convert Long to Integer With the Convert.ToInt32() Method in C

The Convert class converts between different base data types in C#. Since both integer and long are base data types, we can convert from the long data type to the integer data type with the Convert.ToInt32() method in C#. The Convert.ToInt32() method is used to convert any base data type to a 32-bit integer data type. The following code example shows us how we can convert a variable of long data type to a variable of the integer data type with the Convert.ToInt32() method in C#.

using System;

namespace convert_long_to_int
{
    class Program
    {
        static void Main(string[] args)
        {
            long l = 12345;
            int i = Convert.ToInt32(l);
            Console.WriteLine("long = {0}", l);
            Console.WriteLine("Integer = {0}", i);
        }
    }
}

Output:

long = 12345
Integer = 12345

In the above code, we converted the long variable l to the integer variable i with the Convert.ToInt32() function in C#. This method gives an exception if the value of the long variable is too big for the integer variable to handle.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp Integer

  • Convert Int to Enum in C#
  • Random Number in a Range in C#