Convert Double To Int In C#

Developers frequently have to do data type conversions in C#. A double data type to an int data type conversion is one of these. However, the intrinsic disparities between these two categories can make this seemingly simple process difficult.

The double data type in C#, which represents floating-point numbers with double precision, supports fractional values. On the other hand, the int data type is used to store whole numbers only-no fractional parts-and is utilized for this purpose. Due to the possibility of the fractional part of the double being truncated during conversion, converting from double to int can result in data loss. In this article, the two ways for converting double to int in C# will be covered, along with an analysis of their benefits and drawbacks.

Method-1 - Utilizing Explicit Type Casting

The first approach uses explicit type casting to change a double into an int. The following is the method's code:

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 program displays the initial double value, which is 4.677 , followed by the converted double value to an integer, in that order, which is 4 . During the type-casting process, the fractional component of the double value is discarded.

Method 2 - Using Math.Round

The second approach entails applying the Math.Round function to transform a double into an int. View this method's code now

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 program initially displays the initial double value , which is 6.583 , followed by the rounded integer value, which is 7 , obtained by using Math.Round . The double value is rounded via Math.Round method to the closest integer by accepted rounding conventions.

Input Required

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