Skip to content
Related Articles

Related Articles

Different Ways to Convert Double to Integer in C#
  • Last Updated : 17 May, 2020

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
Recommended Articles
Page :