Different Ways to Convert Double to Integer in C#

Given a Double real number, the task is to convert it into Integer in C#.  There are mainly 3 ways to convert Double to Integer as follows:

Examples: 

Input: double = 3452.234
Output: 3452 
 
Input: double = 98.23
Output: 98 

1. Using Typecasting: This technique is a very simple and user friendly. 

Example: 
 

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for type conversion from double to int
using System;
using System.IO;
using System.Text;
namespace GFG {
class Geeks {
  // Main Method
  static void Main(string[] args) {
    double a = 3452.345;
    int b = 0;
  
    // type conversion
    b = (int)a;
  
    Console.WriteLine(b);
  }
}
}

chevron_right


Output: 
 



3452

2. Using Math.round(): This method returns the nearest integer. 
 

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Math.Round(Double) method
using System;
  
class Geeks {
  
  // Main method
  static void Main(string[] args) {
    Double dx1 = 3452.645;
  
    // Output value will be 12
    Console.WriteLine(Math.Round(dx1));
  }
}

chevron_right


Output: 
 

3452

3. UsingDecimal.ToInt32(): This method is used to convert the value of the specified Decimal to the equivalent 32-bit signed integer. 

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to convert Double to Integer
using System;
public
class Demo {
public
  static void Main() {
    double val = 3452.345;
    int res = Convert.ToInt32(val);
    Console.WriteLine(res);
  }
}

chevron_right


Output: 
 

3452



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.