Decimal.ToDouble Method

July 28, 2014

Converts the value of the specified Decimal to the equivalent double-precision floating-point number.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static double ToDouble(
	decimal d
)

Parameters

d
Type: System.Decimal
The Decimal value to convert.

Return Value

Type: System.Double
A double-precision floating-point number equivalent to d.

This operation can produce round-off errors because a double-precision floating-point number has fewer significant digits than a Decimal.

The following code example converts Decimal numbers to Double values using ToDouble method.


// Example of the decimal.ToSingle and decimal.ToDouble methods.
using System;

class Example
{
   static string formatter = "{0,30}{1,17}{2,23}";

   // Convert the decimal argument; no exceptions are thrown.
   public static void DecimalToSgl_Dbl(System.Windows.Controls.TextBlock outputBlock, decimal argument)
   {
      object SingleValue;
      object DoubleValue;

      // Convert the argument to a float value.
      SingleValue = decimal.ToSingle(argument);

      // Convert the argument to a double value.
      DoubleValue = decimal.ToDouble(argument);

      outputBlock.Text += String.Format(formatter, argument,
          SingleValue, DoubleValue) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += String.Format("This example of the \n" +
          "  decimal.ToSingle( decimal ) and \n" +
          "  decimal.ToDouble( decimal ) \nmethods " +
          "generates the following output. It \ndisplays " +
          "several converted decimal values.\n") + "\n";
      outputBlock.Text += String.Format(formatter, "decimal argument",
          "float", "double") + "\n";
      outputBlock.Text += String.Format(formatter, "----------------",
          "-----", "------") + "\n";

      // Convert decimal values and display the results.
      DecimalToSgl_Dbl(outputBlock, 0.0000000000000000000000000001M);
      DecimalToSgl_Dbl(outputBlock, 0.0000000000123456789123456789M);
      DecimalToSgl_Dbl(outputBlock, 123M);
      DecimalToSgl_Dbl(outputBlock, new decimal(123000000, 0, 0, false, 6));
      DecimalToSgl_Dbl(outputBlock, 123456789.123456789M);
      DecimalToSgl_Dbl(outputBlock, 123456789123456789123456789M);
      DecimalToSgl_Dbl(outputBlock, decimal.MinValue);
      DecimalToSgl_Dbl(outputBlock, decimal.MaxValue);
   }
}

/*
This example of the
  decimal.ToSingle( decimal ) and
  decimal.ToDouble( decimal )
methods generates the following output. It
displays several converted decimal values.

              decimal argument            float                 double
              ----------------            -----                 ------
0.0000000000000000000000000001            1E-28                  1E-28
0.0000000000123456789123456789     1.234568E-11   1.23456789123457E-11
                           123              123                    123
                    123.000000              123                    123
           123456789.123456789     1.234568E+08       123456789.123457
   123456789123456789123456789     1.234568E+26   1.23456789123457E+26
-79228162514264337593543950335    -7.922816E+28  -7.92281625142643E+28
 79228162514264337593543950335     7.922816E+28   7.92281625142643E+28
*/


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft