Javatpoint Logo
Javatpoint Logo

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:

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:

  1. 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.
  2. public class Program: This line defines that the program has a public class.
  3. 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.
  4. double double_Value = 4.677;: This line declares a double variable called double_Value and gives it the value 677.
  5. int int_Value = (int)double_Value;: In this case, the code specifically converts the double_Value variable to an integer by adding (int) before it. With this action, the integer portion of the double value is stored in the int_Value 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.
  6. 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: double_Value");: This line prints the original double value kept in the double_Value 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

Output:

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

Explanation:

  1. 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.
  2. public class Program: This line defines that the program has a public class.
  3. 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.
  4. double double_Value = 6.583;: In this line, a double variable called double_Value is declared, and 583 is given as its value.
  5. (int) int_ValueMath.Round(double_Value);: 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.
  6. 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: double_Value"); The double value that was initially placed in the double_Value 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.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA