Convert Double to Int in C#

Muhammad Maisam Abbas Jan 30, 2023 Mar 06, 2021
  1. Convert Double to Int With Explicit Typecasting in C#
  2. Convert Double to Int With the Convert.ToInt32() Function in C#
  3. Convert Double to Int With the Math.Round() Function in C#
Convert Double to Int in C#

This tutorial will introduce the methods to convert a double value to an int value in C#.

Convert Double to Int With Explicit Typecasting in C#

As we know, a double data type takes more bytes than an integer. We need to use explicit typecasting to convert a double value to an int value in C#. The following code example shows us how we can use explicit typecasting for converting a double value to an int value in C#.

using System;

namespace convert_double_to_int
{
    class Program
    {
        static void Main(string[] args)
        {
            double d = 7.7;
            int x = (int) d;
            Console.WriteLine(x);
        }
    }
}

Output:

7

We converted the double value d to the integer value x with explicit typecasting in C#. We get 7 as the output because explicit typecasting completely ignores the values after the decimal point. Explicit typecasting is not recommended because a lot of data loss occurs while using explicit typecasting.

Convert Double to Int With the Convert.ToInt32() Function in C#

The Convert.ToInt32() function converts a value to integer value. The Convert.ToInt32() function converts the value to an equivalent 32-bit signed integer. The following code example shows us how we can use the Convert.ToInt32() function to convert a double value to an integer value in C#.

using System;

namespace convert_double_to_int
{
    class Program
    {
        static void Main(string[] args)
        {
            double d = 7.7;
            int x = Convert.ToInt32(d);
            Console.WriteLine(x);
        }
    }
}

Output:

8

In the above code, we converted the double value d to an integer value x with the Convert.ToInt32() function in C#.

Convert Double to Int With the Math.Round() Function in C#

The Math.Round() function is used to round a decimal value to its nearest integer value. The Math.Round() returns a decimal value rounded to the nearest integer value. The following code example shows us how to use the Math.Round() function to convert a decimal value to an integer value in C#.

using System;

namespace convert_double_to_int
{
    class Program
    {
        static void Main(string[] args)
        {
            double d = 7.7;
            int x = (int) Math.Round(d);
            Console.WriteLine(x);
        }
    }
}

Output:

8

In the above code, we converted the double value d to the integer value x with the Math.Round() function in C#. To store the value inside the integer variable x, we had to use explicit typecasting because the Math.Round() function returns a double value.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Csharp Double

Related Article - Csharp Integer