Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

BigInteger Explicit Conversion (BigInteger to Double)

Defines an explicit conversion of a BigInteger object to a Double value.

Namespace:  System.Numerics
Assemblies:   System.Runtime.Numerics (in System.Runtime.Numerics.dll)
  System.Numerics (in System.Numerics.dll)

public static explicit operator double (
	BigInteger value
)

Parameters

value
Type: System.Numerics.BigInteger

The value to convert to a Double.

Return Value

Type: System.Double
An object that contains the value of the value parameter.

The overloads of the Explicit method define the types to which or from which a BigInteger object can be converted. Language compilers do not perform this conversion automatically because it can involve data loss. Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CDbl in Visual Basic) is used.

Because the BigInteger value can be outside the range of the Double data type, this operation is a narrowing conversion. If the conversion is unsuccessful, it does not throw an OverflowException. Instead, if the BigInteger value is less than Double.MinValue, the resulting Double value is Double.NegativeInfinity. If the BigInteger value is greater than Double.MaxValue, the resulting Double value is Double.PositiveInfinity.

The conversion of a BigInteger to a Double may involve a loss of precision. In some cases, the loss of precision may cause the casting or conversion operation to succeed even if the BigInteger value is outside the range of the Double data type. The following example provides an illustration. It assigns the maximum value of a Double to two BigInteger variables, increments one BigInteger variable by 9.999e291, and tests the two variables for equality. As expected, the call to the Equals(BigInteger) method shows that they are unequal. However, the conversion of the larger BigInteger value back to a Double succeeds, although the BigInteger value now exceeds Double.MaxValue.

// Increase a BigInteger so it exceeds Double.MaxValue.
BigInteger number1 = (BigInteger) Double.MaxValue;
BigInteger number2 = number1;
number2 = number2 + (BigInteger) 9.999e291;
// Compare the BigInteger values for equality.
Console.WriteLine("BigIntegers equal: {0}", number2.Equals(number1));

// Convert the BigInteger to a Double. 
double dbl = (double) number2;

// Display the two values.
Console.WriteLine("BigInteger: {0}", number2);
Console.WriteLine("Double:     {0}", dbl); 
// The example displays the following output: 
//       BigIntegers equal: False 
//       BigInteger: 1.7976931348623158081352742373E+308 
//       Double:     1.79769313486232E+308      

The following example illustrates the conversion of BigInteger to Double values.

// BigInteger to Double conversion.
BigInteger goodDouble = (BigInteger) 102.43e22;
BigInteger badDouble = (BigInteger) Double.MaxValue;  
badDouble = badDouble * 2;

double doubleFromBigInteger;

// successful conversion using cast operator.
doubleFromBigInteger = (double) goodDouble;
Console.WriteLine(doubleFromBigInteger);

// Convert an out-of-bounds BigInteger value to a Double.
doubleFromBigInteger = (double) badDouble;
Console.WriteLine(doubleFromBigInteger);

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1
Show:
© 2015 Microsoft