Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

double
decimal
c#
communitycreator

How to convert a decimal to a double in C#

Theodore Kelechukwu Onyejiaku

Overview

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.

Syntax

public static double ToDouble (decimal d);

Parameters

d: This is the decimal value that we want to convert to a double value.

Return value

This method returns a double-precision, floating-point number that is equivalent to d.

Code example

// use System
using System;
// create class
class DecimalToDouble
{
    // main method
    static void Main()
    {
        // create decimal values
        decimal d1 = 3234.3434m;
        decimal d2 = 0.764342m;
        decimal d3 = -858.9990m; 

        // convert to double
        double r1 = Decimal.ToDouble(d1);
        double r2 = Decimal.ToDouble(d2);
        double r3 = Decimal.ToDouble(d3);

        Console.WriteLine(r1);
        Console.WriteLine(r2);
        Console.WriteLine(r3);
    }
}
Convert decimal values to double in C#

Code explanation

  • 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.

RELATED TAGS

double
decimal
c#
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring