共用方式為


BigInteger.Explicit 運算子

定義

定義物件與其他類型之間的 BigInteger 明確轉換。

多載

名稱 Description
Explicit(BigInteger to BFloat16)
Explicit(Single to BigInteger)

定義了將一個 Single 值明確轉換成一個 BigInteger 值的方法。

Explicit(Complex to BigInteger)

明確地將一個 Complex 值轉換成一個大整數。

Explicit(BigInteger to UIntPtr)

明確地將一個大整數轉換成一個 UIntPtr 值。

Explicit(BigInteger to UInt64)

定義了將 BigInteger 物件明確轉換為無符號 64 位元整數值的過程。

此 API 不符合 CLS 規範。 相容的替代方案為 Double

Explicit(BigInteger to UInt32)

定義物件明確轉換 BigInteger 成無符號的 32 位元整數值。

此 API 不符合 CLS 規範。 相容的替代方案為 Int64

Explicit(BigInteger to UInt16)

定義了將物件明確轉換 BigInteger 成無符號 16 位元整數值的方法。

此 API 不符合 CLS 規範。 相容的替代方案為 Int32

Explicit(BigInteger to UInt128)

明確地將一個大整數轉換成一個 UInt128 值。

Explicit(BigInteger to Single)

定義了將 BigInteger 物件明確轉換為單精度浮點數值的方法。

Explicit(BigInteger to SByte)

定義物件明確轉換為 BigInteger 有符號的 8 位元值。

此 API 不符合 CLS 規範。 相容的替代方案為 Int16

Explicit(BigInteger to Int64)

定義了物件的明確轉換 BigInteger 成 64 位元有號整數值。

Explicit(BigInteger to IntPtr)

明確地將一個大整數轉換成一個 IntPtr 值。

Explicit(BigInteger to Int16)

定義物件明確轉換為 BigInteger 16 位元有號整數值。

Explicit(BigInteger to Int128)

明確地將一個大整數轉換成一個 Int128 值。

Explicit(BigInteger to Half)

明確地將一個大整數轉換成一個 Half 值。

Explicit(BigInteger to Double)

定義 BigInteger 了物件明確轉換為值 Double 的方式。

Explicit(BigInteger to Decimal)

定義 BigInteger 了物件明確轉換為值 Decimal 的方式。

Explicit(BigInteger to Char)

明確地將一個大整數轉換成一個 Char 值。

Explicit(BigInteger to Byte)

定義物件的明確轉換 BigInteger 成無符號位元組值。

Explicit(BFloat16 to BigInteger)
Explicit(Half to BigInteger)

明確地將一個 Half 值轉換成一個大整數。

Explicit(Double to BigInteger)

定義了將一個 Double 值明確轉換成一個 BigInteger 值的方法。

Explicit(Decimal to BigInteger)

定義 Decimal 了物件明確轉換為值 BigInteger 的方式。

Explicit(BigInteger to Int32)

定義了將物件明確轉換為 BigInteger 32 位元有符號整數值的方法。

Explicit(BigInteger to BFloat16)

public:
 static explicit operator System::Numerics::BFloat16(System::Numerics::BigInteger value);
public static explicit operator System.Numerics.BFloat16(System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> System.Numerics.BFloat16
Public Shared Narrowing Operator CType (value As BigInteger) As BFloat16

參數

value
BigInteger

傳回

適用於

Explicit(Single to BigInteger)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

定義了將一個 Single 值明確轉換成一個 BigInteger 值的方法。

public:
 static explicit operator System::Numerics::BigInteger(float value);
public static explicit operator System.Numerics.BigInteger(float value);
static member op_Explicit : single -> System.Numerics.BigInteger
Public Shared Narrowing Operator CType (value As Single) As BigInteger

參數

value
Single

將 轉換為 的值 BigInteger

傳回

一個包含參數值 value 的物件。

例外狀況

範例

以下範例將數值BigInteger陣列Single中的個別元素轉換為物件,然後顯示每次轉換的結果。 請注意,值的任何 Single 小數部分在轉換過程中都會被截斷。

float[] singles = { Single.MinValue, -1.430955172e03f, 2.410970032e05f,
                    Single.MaxValue, Single.PositiveInfinity,
                     Single.NegativeInfinity, Single.NaN };
BigInteger number;

Console.WriteLine("{0,37} {1,37}\n", "Single", "BigInteger");

foreach (float value in singles)
{
   try {
      number = (BigInteger) value;
      Console.WriteLine("{0,37} {1,37}", value, number);
   }
   catch (OverflowException) {
      Console.WriteLine("{0,37} {1,37}", value, "OverflowException");
   }
}
// The example displays the following output:
//           Single                            BigInteger
//
//    -3.402823E+38   -3.4028234663852885981170418348E+38
//        -1430.955                                 -1430
//           241097                                241097
//     3.402823E+38    3.4028234663852885981170418348E+38
//         Infinity                     OverflowException
//        -Infinity                     OverflowException
//              NaN                     OverflowException
let singles =
    [| Single.MinValue
       -1.430955172e03f
       2.410970032e05f
       Single.MaxValue
       Single.PositiveInfinity
       Single.NegativeInfinity
       Single.NaN |]

printfn "%37s %37s\n" "Single" "BigInteger"

for value in singles do
    try
        let number = BigInteger(value)
        printfn "%37O %37O" value number
    with :? OverflowException ->
        printfn "%37O %37s" value "OverflowException"
// The example displays the following output:
//           Single                            BigInteger
//
//    -3.402823E+38   -3.4028234663852885981170418348E+38
//        -1430.955                                 -1430
//           241097                                241097
//     3.402823E+38    3.4028234663852885981170418348E+38
//         Infinity                     OverflowException
//        -Infinity                     OverflowException
//              NaN                     OverflowException
Dim singles() As Single = { Single.MinValue, -1.430955172e03, 2.410970032e05, 
                            Single.MaxValue, Single.PositiveInfinity, 
                            Single.NegativeInfinity, Single.NaN }
Dim number As BigInteger

Console.WriteLine("{0,37} {1,37}", "Single", "BigInteger")
Console.WriteLine()
For Each value As Single In singles
   Try
      number = CType(value, BigInteger)
      Console.WriteLine("{0,37} {1,37}", value, number)
   Catch e As OverflowException
      Console.WriteLine("{0,37} {1,37}", value, "OverflowException")
   End Try
Next     
' The example displays the following output:
'           Single                            BigInteger
' 
'    -3.402823E+38   -3.4028234663852885981170418348E+38
'        -1430.955                                 -1430
'           241097                                241097
'     3.402823E+38    3.4028234663852885981170418348E+38
'         Infinity                     OverflowException
'        -Infinity                     OverflowException
'              NaN                     OverflowException

備註

參數中 value 任意的分數部分在轉換前會被截斷。

方法的 Explicit(Decimal to BigInteger) 過載定義了物件可轉換為的 BigInteger 類型。 由於從 Single 轉換到 BigInteger 可能涉及截斷 中 value任意的分數部分,語言編譯器並不會自動執行此轉換。 相反地,只有在使用鑄造運算子(C# 中)或轉換函數(如 CType Visual Basic 中)時,才會執行轉換。 否則,他們會顯示編譯器錯誤。

對於不支援自訂運算子的語言,另一種方法是 BigInteger.BigInteger(Single)

適用於

Explicit(Complex to BigInteger)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

明確地將一個 Complex 值轉換成一個大整數。

public:
 static explicit operator System::Numerics::BigInteger(System::Numerics::Complex value);
public static explicit operator System.Numerics.BigInteger(System.Numerics.Complex value);
static member op_Explicit : System.Numerics.Complex -> System.Numerics.BigInteger
Public Shared Narrowing Operator CType (value As Complex) As BigInteger

參數

value
Complex

要轉換的值。

傳回

value 轉換成大整數。

適用於

Explicit(BigInteger to UIntPtr)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

重要

此 API 不符合 CLS 規範。

明確地將一個大整數轉換成一個 UIntPtr 值。

public:
 static explicit operator UIntPtr(System::Numerics::BigInteger value);
[System.CLSCompliant(false)]
public static explicit operator UIntPtr(System.Numerics.BigInteger value);
[<System.CLSCompliant(false)>]
static member op_Explicit : System.Numerics.BigInteger -> unativeint
Public Shared Narrowing Operator CType (value As BigInteger) As UIntPtr

參數

value
BigInteger

要轉換的值。

傳回

UIntPtr

unativeint

value 轉換成 UIntPtr 價值。

屬性

適用於

Explicit(BigInteger to UInt64)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Double

定義了將 BigInteger 物件明確轉換為無符號 64 位元整數值的過程。

此 API 不符合 CLS 規範。 相容的替代方案為 Double

public:
 static explicit operator System::UInt64(System::Numerics::BigInteger value);
[System.CLSCompliant(false)]
public static explicit operator ulong(System.Numerics.BigInteger value);
[<System.CLSCompliant(false)>]
static member op_Explicit : System.Numerics.BigInteger -> uint64
Public Shared Narrowing Operator CType (value As BigInteger) As ULong

參數

value
BigInteger

將該值轉換為無符號的 64 位元整數。

傳回

一個包含參數值 value 的物件。

屬性

例外狀況

範例

以下範例說明將 轉換為 BigIntegerUInt64 的過程。 它也會處理OverflowException因為值不在資料型別範圍UInt64而拋BigInteger出的 that。

// BigInteger to UInt64 conversion.
BigInteger goodULong = 2000000000;
BigInteger badULong = BigInteger.Pow(goodULong, 3);

ulong uLongFromBigInteger;

// Successful conversion using cast operator.
uLongFromBigInteger = (ulong) goodULong;
Console.WriteLine(uLongFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   uLongFromBigInteger = (ulong) badULong;
   Console.WriteLine(uLongFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badULong, e.Message);
}
Console.WriteLine();
// BigInteger to UInt64 conversion.
let goodULong = bigint 2000000000
let badULong = BigInteger.Pow(goodULong, 3)

// Successful conversion using cast operator.
let uLongFromBigInteger = uint64 goodULong
printfn $"{uLongFromBigInteger}"

// Handle conversion that should result in overflow.
try
    let uLongFromBigInteger = uint64 badULong
    printfn $"{uLongFromBigInteger}"
with :? OverflowException as e ->
    printfn $"Unable to convert {badULong}:\n   {e.Message}"
' BigInteger to UInt64 conversion.
Dim goodULong As BigInteger = 2000000000
Dim badULong As BigInteger = BigInteger.Pow(goodULong, 3)

Dim uLongFromBigInteger As ULong

' Convert using CType function.
uLongFromBigInteger = CType(goodULong, ULong)
Console.WriteLine(uLongFromBigInteger)
' Convert using CULng function.
uLongFromBigInteger = CULng(goodULong)
Console.WriteLine(uLongFromBigInteger)

' Handle conversion that should result in overflow.
Try
   uLongFromBigInteger = CType(badULong, ULong)
   Console.WriteLine(uLongFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badULong, vbCrLf, e.Message)
End Try
Console.WriteLine()

備註

方法的 Explicit(Decimal to BigInteger) 過載定義了物件可轉換為的 BigInteger 類型。 語言編譯器不會自動執行此轉換,因為這可能導致資料遺失。 相反地,只有在使用 casting operator(C# 中)或 conversion 函式(如 CTypeCULng Visual Basic 中)時,才會執行轉換。 否則,他們會顯示編譯器錯誤。

由於此操作定義了縮小轉換,若BigInteger值超出資料型態範圍UInt64,執行時可能會拋出 。OverflowException 若轉換成功,所得 UInt64 值的精確度不會損失。

適用於

Explicit(BigInteger to UInt32)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Int64

定義物件明確轉換 BigInteger 成無符號的 32 位元整數值。

此 API 不符合 CLS 規範。 相容的替代方案為 Int64

public:
 static explicit operator System::UInt32(System::Numerics::BigInteger value);
[System.CLSCompliant(false)]
public static explicit operator uint(System.Numerics.BigInteger value);
[<System.CLSCompliant(false)>]
static member op_Explicit : System.Numerics.BigInteger -> uint32
Public Shared Narrowing Operator CType (value As BigInteger) As UInteger

參數

value
BigInteger

將該值轉換為無符號的 32 位元整數。

傳回

一個包含參數值 value 的物件。

屬性

例外狀況

範例

以下範例說明將 轉換為 BigIntegerUInt32 的過程。 它也會處理OverflowException因為值不在資料型別範圍UInt32而拋BigInteger出的 that。

// BigInteger to UInt32 conversion.
BigInteger goodUInteger = 200000;
BigInteger badUInteger = 65000000000;

uint uIntegerFromBigInteger;

// Successful conversion using cast operator.
uIntegerFromBigInteger = (uint) goodInteger;
Console.WriteLine(uIntegerFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   uIntegerFromBigInteger = (uint) badUInteger;
   Console.WriteLine(uIntegerFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badUInteger, e.Message);
}
Console.WriteLine();
// BigInteger to UInt32 conversion.
let goodUInteger = bigint 200000
let badUInteger = bigint 65000000000L

// Successful conversion using cast operator.
let uIntegerFromBigInteger = uint goodInteger
printfn $"{uIntegerFromBigInteger}"

// Handle conversion that should result in overflow.
try
    let uIntegerFromBigInteger = uint badUInteger
    printfn $"{uIntegerFromBigInteger}"
with :? OverflowException as e ->
    printfn $"Unable to convert {badUInteger}:\n   {e.Message}"
' BigInteger to UInt32 conversion.
Dim goodUInteger As BigInteger = 200000
Dim badUInteger As BigInteger = 65000000000

Dim uIntegerFromBigInteger As UInteger

' Convert using CType function.
uIntegerFromBigInteger = CType(goodInteger, UInteger)
Console.WriteLine(uIntegerFromBigInteger)
' Convert using CUInt function.
uIntegerFromBigInteger = CUInt(goodInteger)
Console.WriteLine(uIntegerFromBigInteger)

' Handle conversion that should result in overflow.
Try
   uIntegerFromBigInteger = CType(badUInteger, UInteger)
   Console.WriteLine(uIntegerFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badUInteger, vbCrLf, e.Message)
End Try
Console.WriteLine()

備註

方法的 Explicit(Decimal to BigInteger) 過載定義了物件可轉換為的 BigInteger 類型。 語言編譯器不會自動執行此轉換,因為這可能導致資料遺失。 相反地,只有在使用 casting operator(C# 中)或 conversion 函式(如 CTypeCUInt Visual Basic 中)時,才會執行轉換。 否則,他們會顯示編譯器錯誤。

由於此操作定義了縮小轉換,若BigInteger值超出資料型態範圍UInt32,執行時可能會拋出 。OverflowException 若轉換成功,所得 UInt32 值的精確度不會損失。

適用於

Explicit(BigInteger to UInt16)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Int32

定義了將物件明確轉換 BigInteger 成無符號 16 位元整數值的方法。

此 API 不符合 CLS 規範。 相容的替代方案為 Int32

public:
 static explicit operator System::UInt16(System::Numerics::BigInteger value);
[System.CLSCompliant(false)]
public static explicit operator ushort(System.Numerics.BigInteger value);
[<System.CLSCompliant(false)>]
static member op_Explicit : System.Numerics.BigInteger -> uint16
Public Shared Narrowing Operator CType (value As BigInteger) As UShort

參數

value
BigInteger

這個值用來轉換成無符號的 16 位元整數。

傳回

一個包含參數值 value 的物件。

屬性

例外狀況

範例

以下範例說明將 轉換為 BigIntegerUInt16 的過程。 它也會處理OverflowException因為值不在資料型別範圍UInt16而拋BigInteger出的 that。

// BigInteger to UInt16 conversion.
BigInteger goodUShort = 20000;
BigInteger badUShort = 66000;

ushort uShortFromBigInteger;

// Successful conversion using cast operator.
uShortFromBigInteger = (ushort) goodUShort;
Console.WriteLine(uShortFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   uShortFromBigInteger = (ushort) badUShort;
   Console.WriteLine(uShortFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badUShort, e.Message);
}
Console.WriteLine();
// BigInteger to UInt16 conversion.
let goodUShort = bigint 20000
let badUShort = bigint 66000

// Successful conversion using cast operator.
let uShortFromBigInteger = uint16 goodUShort
printfn $"{uShortFromBigInteger}"

// Handle conversion that should result in overflow.
try
    let uShortFromBigInteger = uint16 badUShort
    printfn $"{uShortFromBigInteger}"
with :? OverflowException as e ->
    printfn $"Unable to convert {badSByte}:\n   {e.Message}"
' BigInteger to UInt16 conversion.
Dim goodUShort As BigInteger = 20000
Dim badUShort As BigInteger = 66000

Dim uShortFromBigInteger As UShort

' Convert using CType function.
uShortFromBigInteger = CType(goodUShort, UShort)
Console.WriteLine(uShortFromBigInteger)
' Convert using CUShort function.
uShortFromBigInteger = CUShort(goodUShort)
Console.WriteLine(uShortFromBigInteger)

' Handle conversion that should result in overflow.
Try
   uShortFromBigInteger = CType(badUShort, UShort)
   Console.WriteLine(uShortFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badUShort, vbCrLf, e.Message)
End Try
Console.WriteLine()

備註

方法的 Explicit(Decimal to BigInteger) 過載定義了物件可轉換為的 BigInteger 類型。 語言編譯器不會自動執行此轉換,因為這可能導致資料遺失。 相反地,只有在使用 casting operator(C# 中)或 conversion 函式(如 CTypeCUShort Visual Basic 中)時,才會執行轉換。 否則,他們會顯示編譯器錯誤。

由於此操作定義了縮小轉換,若BigInteger值超出資料型態範圍UInt16,執行時可能會拋出 。OverflowException 若轉換成功,所得 UInt16 值的精確度不會損失。

適用於

Explicit(BigInteger to UInt128)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

重要

此 API 不符合 CLS 規範。

明確地將一個大整數轉換成一個 UInt128 值。

public:
 static explicit operator UInt128(System::Numerics::BigInteger value);
[System.CLSCompliant(false)]
public static explicit operator UInt128(System.Numerics.BigInteger value);
[<System.CLSCompliant(false)>]
static member op_Explicit : System.Numerics.BigInteger -> UInt128
Public Shared Narrowing Operator CType (value As BigInteger) As UInt128

參數

value
BigInteger

要轉換的值。

傳回

value 轉換成 UInt128 價值。

屬性

適用於

Explicit(BigInteger to Single)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

定義了將 BigInteger 物件明確轉換為單精度浮點數值的方法。

public:
 static explicit operator float(System::Numerics::BigInteger value);
public static explicit operator float(System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> single
Public Shared Narrowing Operator CType (value As BigInteger) As Single

參數

value
BigInteger

將該值轉換為單精度浮點數值。

傳回

一個包含參數最接近表示 value 的物件。

範例

以下範例說明將 轉換為 BigIntegerSingle 的過程。

// BigInteger to Single conversion.
BigInteger goodSingle = (BigInteger) 102.43e22F;
BigInteger badSingle = (BigInteger) float.MaxValue;
badSingle = badSingle * 2;

float singleFromBigInteger;

// Successful conversion using cast operator.
singleFromBigInteger = (float) goodSingle;
Console.WriteLine(singleFromBigInteger);

// Convert an out-of-bounds BigInteger value to a Single.
singleFromBigInteger = (float) badSingle;
Console.WriteLine(singleFromBigInteger);
// BigInteger to Single conversion.
let goodSingle = bigint 102.43e22F
let badSingle = bigint Single.MaxValue * bigint 2

// Successful conversion using cast operator.
let singleFromBigInteger = float32 goodSingle
printfn $"{singleFromBigInteger}"

// Convert an out-of-bounds BigInteger value to a Single.
let singleFromBigInteger = float32 badSingle
printfn $"{singleFromBigInteger}"
' BigInteger to Single conversion.
Dim goodSingle As BigInteger = 102.43e22
Dim badSingle As BigInteger = CType(Single.MaxValue, BigInteger)  
badSingle = badSingle * 2

Dim singleFromBigInteger As Single

' Convert using CType function.
singleFromBigInteger = CType(goodSingle, Single)
Console.WriteLine(singleFromBigInteger)
' Convert using CSng function.
singleFromBigInteger = CSng(goodSingle)
Console.WriteLine(singleFromBigInteger)

' Convert an out-of-bounds BigInteger value to a Single.
singleFromBigInteger = CType(badSingle, Single)
Console.WriteLine(singleFromBigInteger)

備註

方法的 Explicit(Decimal to BigInteger) 過載定義了物件可轉換為的 BigInteger 類型。 語言編譯器不會自動執行此轉換,因為這可能導致資料遺失或精度損失。 相反地,只有在使用 casting operator(C# 中)或 conversion 函式(如 CTypeCSng Visual Basic 中)時,才會執行轉換。 否則,他們會顯示編譯器錯誤。

由於該 BigInteger 值可能超出資料型別範圍 Single ,此操作屬於縮小轉換。 若轉換失敗,則不會拋出 OverflowException。 相反地,如果 值 BigIntegerSingle.MinValue於 ,所得 Single 值為 Single.NegativeInfinity。 若值 BigInteger 大於 Single.MaxValue,則所得 Single 值為 Single.PositiveInfinity

將 a BigInteger 轉換為 可能 Single 涉及精度損失。 在某些情況下,精度的損失可能導致鑄造或轉換操作成功,即使該 BigInteger 值超出資料型態範圍 Single 。 下列範例提供一個實例。 它將 a Single 的最大值指派給兩個 BigInteger 變數,將其中一個 BigInteger 變數增加 9.999e291,並測試兩個變數的相等性。 如預期,呼叫該 BigInteger.Equals(BigInteger) 方法顯示它們不相等。 然而,將較大 BigInteger 的值轉換回 a Single 是成功的,儘管 BigInteger 此時的值已 Single.MaxValue超過 。

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

// Convert the BigInteger to a Single.
float sng = (float) number2;

// Display the two values.
Console.WriteLine("BigInteger: {0}", number2);
Console.WriteLine("Single:     {0}", sng);
// The example displays the following output:
//       BigIntegers equal: False
//       BigInteger: 3.4028235663752885981170396038E+38
//       Single:     3.402823E+38
// Increase a BigInteger so it exceeds Single.MaxValue.
let number1 = bigint Single.MaxValue

let number2 = number1 + bigint 9.999e30
// Compare the BigInteger values for equality.
printfn $"BigIntegers equal: {number2.Equals number1}"

// Convert the BigInteger to a Single.
let sng = float32 number2

// Display the two values.
printfn $"BigInteger: {number2}"
printfn $"Single:     {sng}"
// The example displays the following output:
//       BigIntegers equal: False
//       BigInteger: 3.4028235663752885981170396038E+38
//       Single:     3.402823E+38
' Increase a BigInteger so it exceeds Single.MaxValue.
Dim number1 As BigInteger = CType(Single.MaxValue, BigInteger)
Dim number2 As BigInteger = number1
number2 = number2 + 9.999e30
' Compare the BigInteger values for equality.
Console.WriteLine("BigIntegers equal: {0}", number2.Equals(number1))

' Convert the BigInteger to a Single.
Dim sng As Single = CType(number2, Single)

' Display the two values.
Console.WriteLine("BigInteger: {0}", number2)
Console.WriteLine("Single:     {0}", sng)      
' The example displays the following output:
'       BigIntegers equal: False
'       BigInteger: 3.4028235663752885981170396038E+38
'       Single:     3.402823E+38

適用於

Explicit(BigInteger to SByte)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Int16

定義物件明確轉換為 BigInteger 有符號的 8 位元值。

此 API 不符合 CLS 規範。 相容的替代方案為 Int16

public:
 static explicit operator System::SByte(System::Numerics::BigInteger value);
[System.CLSCompliant(false)]
public static explicit operator sbyte(System.Numerics.BigInteger value);
[<System.CLSCompliant(false)>]
static member op_Explicit : System.Numerics.BigInteger -> sbyte
Public Shared Narrowing Operator CType (value As BigInteger) As SByte

參數

value
BigInteger

將該值轉換為有符號的 8 位元值。

傳回

一個包含參數值 value 的物件。

屬性

例外狀況

value 小於 SByte.MinValue 或大於 SByte.MaxValue

範例

以下範例說明將 轉換為 BigIntegerSByte 的過程。 它也會處理OverflowException因為值不在資料型別範圍SByte而拋BigInteger出的 that。

// BigInteger to SByte conversion.
BigInteger goodSByte = BigInteger.MinusOne;
BigInteger badSByte = -130;

sbyte sByteFromBigInteger;

// Successful conversion using cast operator.
sByteFromBigInteger = (sbyte) goodSByte;
Console.WriteLine(sByteFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   sByteFromBigInteger = (sbyte) badSByte;
   Console.WriteLine(sByteFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badSByte, e.Message);
}
Console.WriteLine();
// BigInteger to SByte conversion.
let goodSByte = BigInteger.MinusOne
let badSByte = bigint -130

// Successful conversion using cast operator.
let sByteFromBigInteger = sbyte goodSByte
printfn $"{sByteFromBigInteger}"

// Handle conversion that should result in overflow.
try
    let sByteFromBigInteger = sbyte badSByte
    printfn $"{sByteFromBigInteger}"
with :? OverflowException as e ->
    printfn $"Unable to convert {badSByte}:\n   {e.Message}"
' BigInteger to SByte conversion.
Dim goodSByte As BigInteger = BigInteger.MinusOne
Dim badSByte As BigInteger = -130

Dim sByteFromBigInteger As SByte

' Convert using CType function.
sByteFromBigInteger = CType(goodSByte, SByte)
Console.WriteLine(sByteFromBigInteger)
' Convert using CSByte function.
sByteFromBigInteger = CSByte(goodSByte)
Console.WriteLine(sByteFromBigInteger)

' Handle conversion that should result in overflow.
Try
   sByteFromBigInteger = CType(badSByte, SByte)
   Console.WriteLine(sByteFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badSByte, vbCrLf, e.Message)
End Try
Console.WriteLine()

備註

方法的 Explicit(Decimal to BigInteger) 過載定義了物件可轉換為的 BigInteger 類型。 語言編譯器不會自動執行此轉換,因為這可能導致資料遺失。 相反地,只有在使用 casting operator(C# 中)或 conversion 函式(如 CTypeCSByte Visual Basic 中)時,才會執行轉換。 否則,他們會顯示編譯器錯誤。

由於此操作定義了縮小轉換,若BigInteger值超出資料型態範圍SByte,執行時可能會拋出 。OverflowException 若轉換成功,所得 SByte 值的精確度不會損失。

適用於

Explicit(BigInteger to Int64)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

定義了物件的明確轉換 BigInteger 成 64 位元有號整數值。

public:
 static explicit operator long(System::Numerics::BigInteger value);
public static explicit operator long(System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> int64
Public Shared Narrowing Operator CType (value As BigInteger) As Long

參數

value
BigInteger

將該值轉換為 64 位元有符號整數。

傳回

一個包含參數值 value 的物件。

例外狀況

value 小於 Int64.MinValue 或大於 Int64.MaxValue

範例

以下範例說明將 轉換為 BigIntegerInt64 的過程。 它也會處理OverflowException因為值不在資料型別範圍Int64而拋BigInteger出的 that。

// BigInteger to Int64 conversion.
BigInteger goodLong = 2000000000;
BigInteger badLong = BigInteger.Pow(goodLong, 3);

long longFromBigInteger;

// Successful conversion using cast operator.
longFromBigInteger = (long) goodLong;
Console.WriteLine(longFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   longFromBigInteger = (long) badLong;
   Console.WriteLine(longFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badLong, e.Message);
}
Console.WriteLine();
// BigInteger to Int64 conversion.
let goodLong = 2000000000
let badLong = BigInteger.Pow(goodLong, 3)

// Successful conversion using cast operator.
let longFromBigInteger = int64 goodLong
printfn $"{longFromBigInteger}"

// Handle conversion that should result in overflow.
try
    let longFromBigInteger = int64 badLong
    printfn $"{longFromBigInteger}"
with :? OverflowException as e ->
    printfn $"Unable to convert {badLong}:\n   {e.Message}"
' BigInteger to Int64 conversion.
Dim goodLong As BigInteger = 2000000000
Dim badLong As BigInteger = BigInteger.Pow(goodLong, 3)

Dim longFromBigInteger As Long

' Convert using CType function.
longFromBigInteger = CType(goodLong, Long)
Console.WriteLine(longFromBigInteger)
' Convert using CLng function.
longFromBigInteger = CLng(goodLong)
Console.WriteLine(longFromBigInteger)

' Handle conversion that should result in overflow.
Try
   longFromBigInteger = CType(badLong, Long)
   Console.WriteLine(longFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badLong, vbCrLf, e.Message)
End Try
Console.WriteLine()

備註

方法的 Explicit(Decimal to BigInteger) 過載定義了物件可轉換為的 BigInteger 類型。 語言編譯器不會自動執行此轉換,因為這可能導致資料遺失。 相反地,只有在使用 casting operator(C# 中)或 conversion 函式(如 CTypeCLng Visual Basic 中)時,才會執行轉換。 否則,他們會顯示編譯器錯誤。

由於此操作定義了縮小轉換,若BigInteger值超出資料型態範圍Int64,執行時可能會拋出 。OverflowException

適用於

Explicit(BigInteger to IntPtr)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

明確地將一個大整數轉換成一個 IntPtr 值。

public:
 static explicit operator IntPtr(System::Numerics::BigInteger value);
public static explicit operator IntPtr(System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> nativeint
Public Shared Narrowing Operator CType (value As BigInteger) As IntPtr

參數

value
BigInteger

要轉換的值。

傳回

IntPtr

nativeint

value 轉換成 IntPtr 價值。

適用於

Explicit(BigInteger to Int16)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

定義物件明確轉換為 BigInteger 16 位元有號整數值。

public:
 static explicit operator short(System::Numerics::BigInteger value);
public static explicit operator short(System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> int16
Public Shared Narrowing Operator CType (value As BigInteger) As Short

參數

value
BigInteger

這個值會轉換成 16 位元有符號整數。

傳回

一個包含參數值 value 的物件。

例外狀況

value 小於 Int16.MinValue 或大於 Int16.MaxValue

範例

以下範例說明將 轉換為 BigIntegerInt16 的過程。 它也會處理OverflowException因為值不在資料型別範圍Int16而拋BigInteger出的 that。

// BigInteger to Int16 conversion.
BigInteger goodShort = 20000;
BigInteger badShort = 33000;

short shortFromBigInteger;

// Successful conversion using cast operator.
shortFromBigInteger = (short) goodShort;
Console.WriteLine(shortFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   shortFromBigInteger = (short) badShort;
   Console.WriteLine(shortFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badShort, e.Message);
}
Console.WriteLine();
// BigInteger to Int16 conversion.
let goodShort = bigint 20000
let badShort = bigint 33000

// Successful conversion using cast operator.
let shortFromBigInteger = int16 goodShort
printfn $"{shortFromBigInteger}"

// Handle conversion that should result in overflow.
try
    let shortFromBigInteger = int16 badShort
    printfn $"{shortFromBigInteger}"
with :? OverflowException as e ->
    printfn $"Unable to convert {badShort}:\n   {e.Message}"
' BigInteger to Int16 conversion.
Dim goodShort As BigInteger = 20000
Dim badShort As BigInteger = 33000

Dim shortFromBigInteger As Short

' Convert using CType function.
shortFromBigInteger = CType(goodShort, Short)
Console.WriteLine(shortFromBigInteger)
' Convert using CShort function.
shortFromBigInteger = CShort(goodShort)
Console.WriteLine(shortFromBigInteger)

' Handle conversion that should result in overflow.
Try
   shortFromBigInteger = CType(badShort, Short)
   Console.WriteLine(shortFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badShort, vbCrLf, e.Message)
End Try
Console.WriteLine()

備註

方法的 Explicit(Decimal to BigInteger) 過載定義了物件可轉換為的 BigInteger 類型。 語言編譯器不會自動執行此轉換,因為這可能導致資料遺失。 相反地,只有在使用 casting operator(C# 中)或 conversion 函式(如 CTypeCShort Visual Basic 中)時,才會執行轉換。 否則,他們會顯示編譯器錯誤。

由於此操作定義了縮小轉換,若BigInteger值超出資料型態範圍Int16,執行時可能會拋出 。OverflowException 若轉換成功,所得 Int16 值的精確度不會損失。

適用於

Explicit(BigInteger to Int128)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

明確地將一個大整數轉換成一個 Int128 值。

public:
 static explicit operator Int128(System::Numerics::BigInteger value);
public static explicit operator Int128(System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> Int128
Public Shared Narrowing Operator CType (value As BigInteger) As Int128

參數

value
BigInteger

要轉換的值。

傳回

value 轉換成 Int128 價值。

適用於

Explicit(BigInteger to Half)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

明確地將一個大整數轉換成一個 Half 值。

public:
 static explicit operator Half(System::Numerics::BigInteger value);
public static explicit operator Half(System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> Half
Public Shared Narrowing Operator CType (value As BigInteger) As Half

參數

value
BigInteger

要轉換的值。

傳回

value 轉換成 Half 價值。

適用於

Explicit(BigInteger to Double)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

定義 BigInteger 了物件明確轉換為值 Double 的方式。

public:
 static explicit operator double(System::Numerics::BigInteger value);
public static explicit operator double(System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> double
Public Shared Narrowing Operator CType (value As BigInteger) As Double

參數

value
BigInteger

將 轉換為 的值 Double

傳回

一個包含參數值 value 的物件。

範例

以下範例說明將 轉換為 BigIntegerDouble 的過程。

// 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);
// BigInteger to Double conversion.
let goodDouble = bigint 102.43e22
let badDouble = bigint Double.MaxValue * bigint 2

// successful conversion using cast operator.
let doubleFromBigInteger = double goodDouble
printfn $"{doubleFromBigInteger}"

// Convert an out-of-bounds BigInteger value to a Double.
let doubleFromBigInteger = double badDouble
printfn $"{doubleFromBigInteger}"
' BigInteger to Double conversion.
Dim goodDouble As BigInteger = 102.43e22
Dim badDouble As BigInteger = CType(Double.MaxValue, BigInteger)  
badDouble = badDouble * 2

Dim doubleFromBigInteger As Double

' Convert using CType function.
doubleFromBigInteger = CType(goodDouble, Double)
Console.WriteLine(doubleFromBigInteger)
' Convert using CDbl function.
doubleFromBigInteger = CDbl(goodDouble)
Console.WriteLine(doubleFromBigInteger)

' Convert an out-of-bounds BigInteger value to a Double.
doubleFromBigInteger = CType(badDouble, Double)
Console.WriteLine(doubleFromBigInteger)

備註

方法的 Explicit(Decimal to BigInteger) 過載定義了物件可轉換為的 BigInteger 類型。 語言編譯器不會自動執行此轉換,因為這可能導致資料遺失。 相反地,只有在使用 casting operator(C# 中)或 conversion 函式(如 CTypeCDbl Visual Basic 中)時,才會執行轉換。

由於該 BigInteger 值可能超出資料型別範圍 Double ,此操作屬於縮小轉換。 若轉換失敗,則不會拋出 OverflowException。 相反地,如果 值 BigIntegerDouble.MinValue於 ,所得 Double 值為 Double.NegativeInfinity。 若值 BigInteger 大於 Double.MaxValue,則所得 Double 值為 Double.PositiveInfinity

將 a BigInteger 轉換為 可能 Double 涉及精度損失。 在某些情況下,精度的損失可能導致鑄造或轉換操作成功,即使該 BigInteger 值超出資料型態範圍 Double 。 下列範例提供一個實例。 它將 a Double 的最大值指派給兩個 BigInteger 變數,將其中一個 BigInteger 變數增加 9.999e291,並測試兩個變數的相等性。 如預期,呼叫該 BigInteger.Equals(BigInteger) 方法顯示它們不相等。 然而,將較大 BigInteger 的值轉換回 a Double 是成功的,儘管 BigInteger 此時的值已 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
// Increase a BigInteger so it exceeds Double.MaxValue.
let number1 = bigint Double.MaxValue
let number2 = number1 + bigint 9.999e291
// Compare the BigInteger values for equality.
printfn $"BigIntegers equal: {number2.Equals number1}"

// Convert the BigInteger to a Double.
let dbl = float number2

// Display the two values.
printfn $"BigInteger: {number2}"
printfn $"Double:     {dbl}"
// The example displays the following output:
//       BigIntegers equal: False
//       BigInteger: 1.7976931348623158081352742373E+308
//       Double:     1.79769313486232E+308
' Increase a BigInteger so it exceeds Double.MaxValue.
Dim number1 As BigInteger = CType(Double.MaxValue, BigInteger)
Dim number2 As BigInteger = number1
number2 = number2 + 9.999e291
' Compare the BigInteger values for equality.
Console.WriteLine("BigIntegers equal: {0}", number2.Equals(number1))

' Convert the BigInteger to a Double.
Dim dbl As Double = CType(number2, Double)

' 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

適用於

Explicit(BigInteger to Decimal)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

定義 BigInteger 了物件明確轉換為值 Decimal 的方式。

public:
 static explicit operator System::Decimal(System::Numerics::BigInteger value);
public static explicit operator decimal(System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> decimal
Public Shared Narrowing Operator CType (value As BigInteger) As Decimal

參數

value
BigInteger

將 轉換為 的值 Decimal

傳回

一個包含參數值 value 的物件。

例外狀況

範例

以下範例說明將 轉換為 BigIntegerDecimal 的過程。 它也會處理OverflowException因為值不在資料型別範圍Decimal而拋BigInteger出的 that。

// BigInteger to Decimal conversion.
BigInteger goodDecimal = 761652543;
BigInteger badDecimal = (BigInteger) Decimal.MaxValue;
badDecimal += BigInteger.One;

Decimal decimalFromBigInteger;

// Successful conversion using cast operator.
decimalFromBigInteger = (decimal) goodDecimal;
Console.WriteLine(decimalFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   decimalFromBigInteger = (decimal) badDecimal;
   Console.WriteLine(decimalFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badDecimal, e.Message);
}
Console.WriteLine();
// BigInteger to Decimal conversion.
let goodDecimal = 761652543
let badDecimal = bigint Decimal.MaxValue + BigInteger.One

// Successful conversion using cast operator.
let decimalFromBigInteger = decimal goodDecimal
printfn $"{decimalFromBigInteger}"

// Handle conversion that should result in overflow.
try
    let decimalFromBigInteger = decimal badDecimal
    printfn $"{decimalFromBigInteger}"
with :? OverflowException as e ->
    printfn $"Unable to convert {badDecimal}:\n   {e.Message}"
' BigInteger to Decimal conversion.
Dim goodDecimal As BigInteger = 761652543
Dim badDecimal As BigInteger = CType(Decimal.MaxValue, BigInteger) 
badDecimal += BigInteger.One

Dim decimalFromBigInteger As Decimal

' Convert using CType function.
decimalFromBigInteger = CType(goodDecimal, Decimal)
Console.WriteLine(decimalFromBigInteger)
' Convert using CDec function.
decimalFromBigInteger = CDec(goodDecimal)
Console.WriteLine(decimalFromBigInteger)

' Handle conversion that should result in overflow.
Try
   decimalFromBigInteger = CType(badDecimal, Decimal)
   Console.WriteLine(decimalFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badDecimal, vbCrLf, e.Message)
End Try
Console.WriteLine()

備註

方法的 Explicit(Decimal to BigInteger) 過載定義了物件可轉換為的 BigInteger 類型。 語言編譯器不會自動執行此轉換,因為這可能導致資料遺失。 相反地,只有在使用 casting operator(C# 中)或 conversion 函式(如 CTypeCDec Visual Basic 中)時,才會執行轉換。

由於此操作定義了縮小轉換,若BigInteger值超出資料型態範圍Decimal,執行時可能會拋出 。OverflowException

適用於

Explicit(BigInteger to Char)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

明確地將一個大整數轉換成一個 Char 值。

public:
 static explicit operator char(System::Numerics::BigInteger value);
public static explicit operator char(System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> char
Public Shared Narrowing Operator CType (value As BigInteger) As Char

參數

value
BigInteger

要轉換的值。

傳回

value 轉換成 Char 價值。

適用於

Explicit(BigInteger to Byte)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

定義物件的明確轉換 BigInteger 成無符號位元組值。

public:
 static explicit operator System::Byte(System::Numerics::BigInteger value);
public static explicit operator byte(System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> byte
Public Shared Narrowing Operator CType (value As BigInteger) As Byte

參數

value
BigInteger

將 轉換為 的值 Byte

傳回

一個包含參數值 value 的物件。

例外狀況

value 小於 Byte.MinValue 或大於 Byte.MaxValue

範例

以下範例說明將 轉換為 BigIntegerByte 的過程。 它也會處理OverflowException因為值不在資料型別範圍Byte而拋BigInteger出的 that。

// BigInteger to Byte conversion.
BigInteger goodByte = BigInteger.One;
BigInteger badByte = 256;

byte byteFromBigInteger;

// Successful conversion using cast operator.
byteFromBigInteger = (byte) goodByte;
Console.WriteLine(byteFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   byteFromBigInteger = (byte) badByte;
   Console.WriteLine(byteFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badByte, e.Message);
}
Console.WriteLine();
// BigInteger to Byte conversion.
let goodByte = BigInteger.One
let badByte = bigint 256

// Successful conversion using cast operator.
let byteFromBigInteger = byte goodByte
printfn $"{byteFromBigInteger}"

// Handle conversion that should result in overflow.
try
    let byteFromBigInteger = byte badByte
    printfn $"{byteFromBigInteger}"
with :? OverflowException as e ->
    printfn $"Unable to convert {badByte}:\n   {e.Message}"
' BigInteger to Byte conversion.
Dim goodByte As BigInteger = BigInteger.One
Dim badByte As BigInteger = 256

Dim byteFromBigInteger As Byte   

' Convert using CType function.
byteFromBigInteger = CType(goodByte, Byte)
Console.WriteLine(byteFromBigInteger)
' Convert using CByte function.
byteFromBigInteger = CByte(goodByte)
Console.WriteLine(byteFromBigInteger)

' Handle conversion that should result in overflow.
Try
   byteFromBigInteger = CType(badByte, Byte)
   Console.WriteLine(byteFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badByte, vbCrLf, e.Message)
End Try
Console.WriteLine()

備註

方法的 Explicit(Decimal to BigInteger) 過載定義了物件可轉換為的 BigInteger 類型。 語言編譯器不會自動執行此轉換,因為這可能導致資料遺失。 相反地,只有在使用 casting operator(C# 中)或 conversion 函式(如 CTypeCByte Visual Basic 中)時,才會執行轉換。 否則,他們會顯示編譯器錯誤。

由於此操作定義了縮小轉換,若BigInteger值超出資料型態範圍Byte,執行時可能會拋出 。OverflowException 若轉換成功,所得 Byte 值的精確度不會損失。

適用於

Explicit(BFloat16 to BigInteger)

public:
 static explicit operator System::Numerics::BigInteger(System::Numerics::BFloat16 value);
public static explicit operator System.Numerics.BigInteger(System.Numerics.BFloat16 value);
static member op_Explicit : System.Numerics.BFloat16 -> System.Numerics.BigInteger
Public Shared Narrowing Operator CType (value As BFloat16) As BigInteger

參數

value
BFloat16

傳回

適用於

Explicit(Half to BigInteger)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

明確地將一個 Half 值轉換成一個大整數。

public:
 static explicit operator System::Numerics::BigInteger(Half value);
public static explicit operator System.Numerics.BigInteger(Half value);
static member op_Explicit : Half -> System.Numerics.BigInteger
Public Shared Narrowing Operator CType (value As Half) As BigInteger

參數

value
Half

要轉換的值。

傳回

value 轉換成大整數。

適用於

Explicit(Double to BigInteger)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

定義了將一個 Double 值明確轉換成一個 BigInteger 值的方法。

public:
 static explicit operator System::Numerics::BigInteger(double value);
public static explicit operator System.Numerics.BigInteger(double value);
static member op_Explicit : double -> System.Numerics.BigInteger
Public Shared Narrowing Operator CType (value As Double) As BigInteger

參數

value
Double

將 轉換為 的值 BigInteger

傳回

一個包含參數值 value 的物件。

例外狀況

範例

以下範例將數值BigInteger陣列Double中的個別元素轉換為物件,然後顯示每次轉換的結果。 請注意,值的任何 Double 小數部分在轉換過程中都會被截斷。

double[] doubles = { Double.MinValue, -1.430955172e03, 2.410970032e05,
                     Double.MaxValue, Double.PositiveInfinity,
                     Double.NegativeInfinity, Double.NaN };
BigInteger number;

Console.WriteLine("{0,37} {1,37}\n", "Double", "BigInteger");

foreach (double value in doubles)
{
   try {
      number = (BigInteger) value;
      Console.WriteLine("{0,37} {1,37}", value, number);
   }
   catch (OverflowException) {
      Console.WriteLine("{0,37} {1,37}", value, "OverflowException");
   }
}
// The example displays the following output:
//                                Double                            BigInteger
//
//                -1.79769313486232E+308  -1.7976931348623157081452742373E+308
//                          -1430.955172                                 -1430
//                           241097.0032                                241097
//                 1.79769313486232E+308   1.7976931348623157081452742373E+308
//                              Infinity                     OverflowException
//                             -Infinity                     OverflowException
//                                   NaN                     OverflowException
let doubles =
    [| Double.MinValue
       -1.430955172e03
       2.410970032e05
       Double.MaxValue
       Double.PositiveInfinity
       Double.NegativeInfinity
       Double.NaN |]

printfn "%37s %37s\n" "Double" "BigInteger"

for value in doubles do
    try
        let number = BigInteger(value)
        printfn "%37O %37O" value number
    with :? OverflowException ->
        printfn "%37O %37s" value "OverflowException"
// The example displays the following output:
//                                Double                            BigInteger
//
//                -1.79769313486232E+308  -1.7976931348623157081452742373E+308
//                          -1430.955172                                 -1430
//                           241097.0032                                241097
//                 1.79769313486232E+308   1.7976931348623157081452742373E+308
//                              Infinity                     OverflowException
//                             -Infinity                     OverflowException
//                                   NaN                     OverflowException
Dim doubles() As Double = { Double.MinValue, -1.430955172e03, 2.410970032e05, 
                            Double.MaxValue, Double.PositiveInfinity, 
                            Double.NegativeInfinity, Double.NaN }
Dim number As BigInteger

Console.WriteLine("{0,37} {1,37}", "Double", "BigInteger")
Console.WriteLine()
For Each value As Double In doubles
   Try
      number = CType(value, BigInteger)
      Console.WriteLine("{0,37} {1,37}", value, number)
   Catch e As OverflowException
      Console.WriteLine("{0,37} {1,37}", value, "OverflowException")
   End Try      
Next
' The example displays the following output:
'                                Double                            BigInteger
' 
'                -1.79769313486232E+308  -1.7976931348623157081452742373E+308
'                          -1430.955172                                 -1430
'                           241097.0032                                241097
'                 1.79769313486232E+308   1.7976931348623157081452742373E+308
'                              Infinity                     OverflowException
'                             -Infinity                     OverflowException
'                                   NaN                     OverflowException

備註

參數中 value 任意的分數部分在轉換前會被截斷。

方法的 Explicit(Decimal to BigInteger) 過載定義了物件可轉換為的 BigInteger 類型。 由於從 Double 轉換到 BigInteger 可能涉及截斷 中 value任意的分數部分,語言編譯器並不會自動執行此轉換。 相反地,只有在使用鑄造運算子(C# 中)或轉換函數(如 CType Visual Basic 中)時,才會執行轉換。 否則,他們會顯示編譯器錯誤。

對於不支援自訂運算子的語言,另一種方法是 BigInteger.BigInteger(Double)

適用於

Explicit(Decimal to BigInteger)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

定義 Decimal 了物件明確轉換為值 BigInteger 的方式。

public:
 static explicit operator System::Numerics::BigInteger(System::Decimal value);
public static explicit operator System.Numerics.BigInteger(decimal value);
static member op_Explicit : decimal -> System.Numerics.BigInteger
Public Shared Narrowing Operator CType (value As Decimal) As BigInteger

參數

value
Decimal

將 轉換為 的值 BigInteger

傳回

一個包含參數值 value 的物件。

範例

以下範例將數值BigInteger陣列Decimal中的個別元素轉換為物件,然後顯示每次轉換的結果。 請注意,值的任何 Decimal 小數部分在轉換過程中都會被截斷。

decimal[] decimals = { Decimal.MinValue, -15632.991m, 9029321.12m,
                       Decimal.MaxValue };
BigInteger number;

Console.WriteLine("{0,35} {1,35}\n", "Decimal", "BigInteger");

foreach (decimal value in decimals)
{
   number = (BigInteger) value;
   Console.WriteLine("{0,35} {1,35}", value, number);
}
// The example displays the following output:
//
//                          Decimal                          BigInteger
//
//    -79228162514264337593543950335      -79228162514264337593543950335
//                       -15632.991                              -15632
//                       9029321.12                             9029321
//    79228162514264337593543950335       79228162514264337593543950335
let decimals = [| Decimal.MinValue; -15632.991m; 9029321.12m; Decimal.MaxValue |]

printfn "%35s %35s\n" "Decimal" "BigInteger"

for value in decimals do
    let number = BigInteger(value)
    printfn "%35O %35O" value number
// The example displays the following output:
//
//                          Decimal                          BigInteger
//
//    -79228162514264337593543950335      -79228162514264337593543950335
//                       -15632.991                              -15632
//                       9029321.12                             9029321
//    79228162514264337593543950335       79228162514264337593543950335
' Explicit Decimal to BigInteger conversion
Dim decimals() As Decimal = { Decimal.MinValue, -15632.991d, 9029321.12d, 
                              Decimal.MaxValue }
Dim number As BigInteger 

Console.WriteLine("{0,35} {1,35}", "Decimal", "BigInteger")
Console.WriteLine()
For Each value As Decimal In decimals
   number = CType(value, BigInteger)
   Console.WriteLine("{0,35} {1,35}",
                     value, number)
Next
' The example displays the following output:
'
'                          Decimal                          BigInteger
'    
'    -79228162514264337593543950335      -79228162514264337593543950335
'                       -15632.991                              -15632
'                       9029321.12                             9029321
'    79228162514264337593543950335       79228162514264337593543950335

備註

參數中 value 任意的分數部分在轉換前會被截斷。

方法的 Explicit(Decimal to BigInteger) 過載定義了物件可轉換為的 BigInteger 類型。 由於從 Decimal 轉換到 BigInteger 可能涉及截斷 中 value任意的分數部分,語言編譯器並不會自動執行此轉換。 相反地,只有在使用鑄造運算子(C# 中)或轉換函數(如 CType Visual Basic 中)時,才會執行轉換。 否則,他們會顯示編譯器錯誤。

對於不支援自訂運算子的語言,另一種方法是 BigInteger.BigInteger(Decimal)

適用於

Explicit(BigInteger to Int32)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

定義了將物件明確轉換為 BigInteger 32 位元有符號整數值的方法。

public:
 static explicit operator int(System::Numerics::BigInteger value);
public static explicit operator int(System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> int
Public Shared Narrowing Operator CType (value As BigInteger) As Integer

參數

value
BigInteger

這個值轉換成 32 位元有符號整數。

傳回

一個包含參數值 value 的物件。

例外狀況

value 小於 Int32.MinValue 或大於 Int32.MaxValue

範例

以下範例說明將 轉換為 BigIntegerInt32 的過程。 它也會處理OverflowException因為值不在資料型別範圍Int32而拋BigInteger出的 that。

// BigInteger to Int32 conversion.
BigInteger goodInteger = 200000;
BigInteger badInteger = 65000000000;

int integerFromBigInteger;

// Successful conversion using cast operator.
integerFromBigInteger = (int) goodInteger;
Console.WriteLine(integerFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   integerFromBigInteger = (int) badInteger;
   Console.WriteLine(integerFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badInteger, e.Message);
}
Console.WriteLine();
// BigInteger to Int32 conversion.
let goodInteger = bigint 200000
let badInteger = bigint 65000000000L

// Successful conversion using cast operator.
let integerFromBigInteger = int goodInteger
printfn $"{integerFromBigInteger}"

// Handle conversion that should result in overflow.
try
    let integerFromBigInteger = int badInteger
    printfn $"{integerFromBigInteger}"
with :? OverflowException as e ->
    printfn $"Unable to convert {badInteger}:\n   {e.Message}"
' BigInteger to Int32 conversion.
Dim goodInteger As BigInteger = 200000
Dim badInteger As BigInteger = 65000000000

Dim integerFromBigInteger As Integer

' Convert using CType function.
integerFromBigInteger = CType(goodInteger, Integer)
Console.WriteLine(integerFromBigInteger)
' Convert using CInt function.
integerFromBigInteger = CInt(goodInteger)
Console.WriteLIne(integerFromBigInteger)

' Handle conversion that should result in overflow.
Try
   integerFromBigInteger = CType(badInteger, Integer)
   Console.WriteLine(integerFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badInteger, vbCrLf, e.Message)
End Try
Console.WriteLine()

備註

方法的 Explicit(Decimal to BigInteger) 過載定義了物件可轉換為的 BigInteger 類型。 語言編譯器不會自動執行此轉換,因為這可能導致資料遺失。 相反地,只有在使用 casting operator(C# 中)或 conversion 函式(如 CTypeCInt Visual Basic 中)時,才會執行轉換。 否則,他們會顯示編譯器錯誤。

由於此操作定義了縮小轉換,若BigInteger值超出資料型態範圍Int32,執行時可能會拋出 。OverflowException 若轉換成功,所得 Int32 值的精確度不會損失。

適用於