Windows apps
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Decimal Explicit Conversion (Double to Decimal)

Silverlight

Converts a double-precision floating-point number to a Decimal.

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

Syntax

public static explicit operator decimal (
double value
)

Parameters

value
Type: System.Double
A double-precision floating-point number.

Return Value

Type: System.Decimal
A Decimal that represents the converted double-precision floating point number.

Exceptions

ExceptionCondition
OverflowException

value is less than MinValue or greater than MaxValue.

-or-

Examples

The following code example converts Double values to Decimal numbers using the Double to Decimal conversion. This conversion requires the op_Explicit operator in Visual Basic.

// Example of the explicit conversion from double to decimal.
using System;

class Example
{
const string formatter = "{0,25:E16}{1,33}";

// Get the exception type name; remove the namespace prefix.
public static string GetExceptionType(Exception ex)
{
string exceptionType = ex.GetType().ToString();
return exceptionType.Substring(
exceptionType.LastIndexOf('.') + 1);
}

// Convert the double argument; catch exceptions that are thrown.
public static void DecimalFromDouble(System.Windows.Controls.TextBlock outputBlock, double argument)
{
object decValue;

// Convert the double argument to a decimal value.
try
{
decValue = (decimal)argument;
}
catch (Exception ex)
{
decValue = GetExceptionType(ex);
}

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

public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
outputBlock.Text +=
"This example of the explicit conversion from double " +
"to decimal \ngenerates the following output.\n" + "\n";
outputBlock.Text += String.Format(formatter, "double argument",
"decimal value") + "\n";
outputBlock.Text += String.Format(formatter, "---------------",
"-------------") + "\n";

// Convert double values and display the results.
DecimalFromDouble(outputBlock, 1.234567890123E-30);
DecimalFromDouble(outputBlock, 1.2345678901234E-25);
DecimalFromDouble(outputBlock, 1.23456789012345E-20);
DecimalFromDouble(outputBlock, 1.234567890123456E-10);
DecimalFromDouble(outputBlock, 1.2345678901234567);
DecimalFromDouble(outputBlock, 1.23456789012345678E+12);
DecimalFromDouble(outputBlock, 1.234567890123456789E+28);
DecimalFromDouble(outputBlock, 1.234567890123456789E+30);
}
}

/*
This example of the explicit conversion from double to decimal
generates the following output.

double argument                    decimal value
---------------                    -------------
1.2345678901230000E-030                                0
1.2345678901233999E-025   0.0000000000000000000000001235
1.2345678901234499E-020   0.0000000000000000000123456789
1.2345678901234560E-010       0.000000000123456789012346
1.2345678901234567E+000                 1.23456789012346
1.2345678901234568E+012                 1234567890123.46
1.2345678901234568E+028    12345678901234600000000000000
1.2345678901234569E+030                OverflowException
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.