Open In App

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# 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);
  }
}
}

Output: 
 

3452

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




// 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));
  }
}

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# 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);
  }
}

Output: 
 

3452

Article Tags :