C# Program to convert a Double to an Integer Value

To convert a double value to an integer value in C#, you can use several methods. The most common approach is using the Convert.ToInt32() method, which performs rounded conversion, or explicit casting for truncation.

The Int32 data type represents a 32-bit signed integer that can store values from -2,147,483,648 to 2,147,483,647.

Syntax

Following are the different syntaxes for converting double to integer −

// Using Convert.ToInt32() - rounds to nearest integer
int result = Convert.ToInt32(doubleValue);

// Using explicit casting - truncates decimal part
int result = (int)doubleValue;

// Using Math.Round() with casting - custom rounding
int result = (int)Math.Round(doubleValue);

Using Convert.ToInt32() Method

The Convert.ToInt32() method rounds the double value to the nearest integer using banker's rounding −

using System;

public class Demo {
    public static void Main() {
        double val1 = 21.34;
        double val2 = 21.67;
        double val3 = 21.5;
        
        int res1 = Convert.ToInt32(val1);
        int res2 = Convert.ToInt32(val2);
        int res3 = Convert.ToInt32(val3);
        
        Console.WriteLine("Converted double {0} to integer {1}", val1, res1);
        Console.WriteLine("Converted double {0} to integer {1}", val2, res2);
        Console.WriteLine("Converted double {0} to integer {1}", val3, res3);
    }
}

The output of the above code is −

Converted double 21.34 to integer 21
Converted double 21.67 to integer 22
Converted double 21.5 to integer 22

Using Explicit Casting

Explicit casting truncates the decimal part without rounding −

using System;

public class Demo {
    public static void Main() {
        double val1 = 21.34;
        double val2 = 21.99;
        double val3 = -15.78;
        
        int res1 = (int)val1;
        int res2 = (int)val2;
        int res3 = (int)val3;
        
        Console.WriteLine("Truncated double {0} to integer {1}", val1, res1);
        Console.WriteLine("Truncated double {0} to integer {1}", val2, res2);
        Console.WriteLine("Truncated double {0} to integer {1}", val3, res3);
    }
}

The output of the above code is −

Truncated double 21.34 to integer 21
Truncated double 21.99 to integer 21
Truncated double -15.78 to integer -15

Comparison of Conversion Methods

Method Behavior Example: 21.7 ? Result
Convert.ToInt32() Rounds to nearest integer 21.7 ? 22
(int) casting Truncates decimal part 21.7 ? 21
Math.Round() + casting Custom rounding control 21.7 ? 22

Using Math.Round() for Custom Rounding

using System;

public class Demo {
    public static void Main() {
        double val = 21.5;
        
        int roundUp = (int)Math.Round(val, MidpointRounding.AwayFromZero);
        int roundEven = (int)Math.Round(val, MidpointRounding.ToEven);
        
        Console.WriteLine("Original value: {0}", val);
        Console.WriteLine("Round away from zero: {0}", roundUp);
        Console.WriteLine("Round to even (banker's): {0}", roundEven);
    }
}

The output of the above code is −

Original value: 21.5
Round away from zero: 22
Round to even (banker's): 22

Conclusion

Converting double to integer in C# can be done using Convert.ToInt32() for rounded conversion or explicit casting (int) for truncation. Choose the method based on whether you need rounding or truncation behavior in your application.

Updated on: 2026-03-17T07:04:35+05:30

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements