How to Convert Integer to Double in C#?
A double data type in C# is a floating-point type that can store larger numbers with decimal points compared to integers. Converting from an integer to a double involves ensuring that the precision of the value is maintained while changing its data type.
using System;
class MainClass {
public static void Main (string[] args) {
// Integer variable
int myInt = 42;
// Convert integer to double
double myDouble = Convert.ToDouble(myInt);
// Output the converted double
Console.WriteLine("Converted Integer to Double: " + myDouble);
}
}
In this program, we have declared an integer variable myInt
with the value 42. Then, we used the Convert.ToDouble()
method to convert this integer to a double and store it in the variable myDouble
.
Output:
Converted Integer to Double: 42
Comments (0)