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#
using System;
using System.IO;
using System.Text;
namespace GFG {
class Geeks {
static void Main( string [] args) {
double a = 3452.345;
int b = 0;
b = ( int )a;
Console.WriteLine(b);
}
}
}
|
Output:
3452
2. Using Math.round(): This method returns the nearest integer.
C#
using System;
class Geeks {
static void Main( string [] args) {
Double dx1 = 3452.645;
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#
using System;
public
class Demo {
public
static void Main() {
double val = 3452.345;
int res = Convert.ToInt32(val);
Console.WriteLine(res);
}
}
|
Output:
3452
Share your thoughts in the comments
Please Login to comment...