The Decimal.ToDouble()
method can be used to convert a decimal to a double in C#.
The Decimal.ToDouble()
method converts a specified decimal value to its equivalent double-precision, floating-point number.
public static double ToDouble (decimal d);
d
: This is the decimal value that we want to convert to a double value.
This method returns a double-precision, floating-point number that is equivalent to d
.
// use Systemusing System;// create classclass DecimalToDouble{// main methodstatic void Main(){// create decimal valuesdecimal d1 = 3234.3434m;decimal d2 = 0.764342m;decimal d3 = -858.9990m;// convert to doubledouble r1 = Decimal.ToDouble(d1);double r2 = Decimal.ToDouble(d2);double r3 = Decimal.ToDouble(d3);Console.WriteLine(r1);Console.WriteLine(r2);Console.WriteLine(r3);}}
Lines 10–12: We create three decimal variables and initialize them with some decimal values.
Lines 15–17: We call the Decimal.ToDouble()
method on the decimal values and store the results on some double variables, r1
, r2
, and r3
.
Lines 19–21: We print the double values which result from the conversion to the console.