Convert Double To Int In C#

Programmers often encounter the need for data type conversions when working with C#. Converting a double data type to an int data type is a common task in this context. Yet, the inherent differences between these two data types can complicate what appears to be a straightforward conversion process.

The double data type in C# stands for floating-point numbers with double precision and is capable of handling fractional values. Conversely, the int data type is specifically designed for storing whole numbers without any fractional components and serves this purpose effectively. When converting double to int, there is a risk of losing data due to potential truncation of the fractional part of the double value. This article will delve into two methods of converting double to int in C#, examining their advantages and disadvantages in detail.

Method-1 - Utilizing Explicit Type Casting

The initial method involves employing explicit type conversion to transform a double data type into an integer. Below is the code snippet for this method:

Example

using System;
public class Program
{
    public static void Main()
    {
        double double_Value = 4.677;
        int int_Value = (int)double_Value;
        Console.WriteLine("Using the Explicit Type Casting method");
        Console.WriteLine($"\nThe Original Double Value is: {double_Value}");
        Console.WriteLine($"After converting from double to integer the Integer Value is: {int_Value}");
    }
}

Output:

Output

Using the Explicit Type Casting method
The original Double Value is: 4.677
After converting from double to integer the Integer Value is: 4

Explanation:

  • using System;: The System namespace , which has classes and methods for a variety of system-related activities, such as input and output, is included in this line.
  • public class Program: This line defines that the program has a public class.
  • public static void Main: This line defines that the program is launched from this function. It is the initial step in a console program because it is a static method called Main that has no parameters.
  • double doubleValue = 4.677;: This line declares a double variable called doubleValue and gives it the value 677 .
  • int intValue = (int)doubleValue;: In this case, the code specifically converts the doubleValue variable to an integer by adding (int) before it. With this action, the integer portion of the double value is stored in the intValue variable while the decimal portion of the value is effectively truncated. In this instance, the fractional part 677 is shortened. Therefore int_Value will be set to 4.
  • To print the results, use the lines below: "Using Explicit Type Casting method" in the console window;: This message is merely a header that identifies the output's goal. WriteLine($"Original Double Value is: doubleValue");: This line prints the original double value kept in the doubleValue variable. WriteLine($"After converting from double to integer, the Integer Value is: int_Value"): In this line, the integer value is displayed following an explicit type casting.
  • "Using Explicit Type Casting method" in the console window;: This message is merely a header that identifies the output's goal.
  • WriteLine($"Original Double Value is: doubleValue");: This line prints the original double value kept in the doubleValue variable.
  • WriteLine($"After converting from double to integer, the Integer Value is: int_Value"): In this line, the integer value is displayed following an explicit type casting.

The software showcases the original double value, set at 4.677, first, then proceeds to exhibit the same value converted to an integer, which is 4. Throughout the casting operation, the decimal part of the double value is disregarded.

Method 2 - Using Math.Round

The alternative method involves utilizing the Math.Round function to convert a double data type to an integer. Take a look at the code snippet for this technique below.

Example

using System;
public class Program
{
    public static void Main()
    {
        double double_Value = 6.583;
        int int_Value = (int)Math.Round(double_Value);
        Console.WriteLine("Using Math.Round() method");
        Console.WriteLine($"Original Double Value is: {double_Value}");
        Console.WriteLine($"After Converting from double to int the Integer Value: {int_Value}");
    }
}

Output:

Output

Using Math.Round() method
The original Double Value is: 6.583
After Converting from double to int the Integer Value: 7

Explanation:

  • using System;: The System namespace , which has classes and methods for a variety of system-related activities, such as input and output, is included in this line.
  • public class Program: This line defines that the program has a public class .
  • public static void Main: It is the main function where the program starts. It is a static method called Main that has no parameters, making it the beginning point of a console program.
  • double doubleValue = 6.583;: In this line, a double variable called doubleValue is declared, and 583 is given as its value.
  • (int) intValueMath.Round(doubleValue);: In this case, the double_Value is rounded to the nearest integer using the Round method and the result is then explicitly cast to an int. Due to its proximity to 7 compared to 6 , this operation rounds 6.583 to 7 .
  • To print the results, use the lines below: WriteLine("Using Math.Round method");: This message is only a header that identifies the output's goal. WriteLine($"Original Double Value is: doubleValue"); The double value that was initially placed in the doubleValue variable is printed in this line. WriteLine($"The Integer Value is int_Value After Converting from double to int;");: Following some math, this line outputs the integer value.typecasting and round.
  • WriteLine("Using Math.Round method");: This message is only a header that identifies the output's goal.
  • WriteLine($"Original Double Value is: doubleValue"); The double value that was initially placed in the doubleValue variable is printed in this line.
  • WriteLine($"The Integer Value is int_Value After Converting from double to int;");: Following some math, this line outputs the integer value.typecasting and round.

The software first presents the original double value, which is 6.583, followed by the rounded integer value of 7, calculated using the Math.Round function. The double value is rounded to the nearest integer following standard rounding rules.

Input Required

This code uses input(). Please provide values below: