How to Converting Double to Integer in C#?
In this program, we have a double value 10.75
that we want to convert to an integer. The conversion is performed by casting the double value to an integer using (int)doubleValue
. The (int)
is a type cast operator indicating the conversion to the integer type.
using System;
class Program
{
static void Main(string[] args)
{
// Double value to be converted
double doubleValue = 10.75;
// Convert double to integer
int intValue = (int)doubleValue;
// Output the results
Console.WriteLine("Double value: " + doubleValue);
Console.WriteLine("Integer value: " + intValue);
}
}
Output:
Let's run the program and see the output:
Double value: 10.75
Integer value: 10
As expected, the double value 10.75
is converted to an integer, resulting in 10
. Notice that the fractional part .75
is truncated during the conversion process.
Comments (0)