共用方式為


BigInteger 建構函式

定義

初始化 BigInteger 結構的新執行個體。

多載

BigInteger(Byte[])

使用位元組陣列中的值,初始化 BigInteger 結構的新執行個體。

BigInteger(Decimal)

使用 BigInteger 值,初始化 Decimal 結構的新執行個體。

BigInteger(Double)

使用雙精確度浮點值,初始化 BigInteger 結構的新執行個體。

BigInteger(Int32)

使用 32 位元帶正負號的整數值,初始化 BigInteger 結構的新執行個體。

BigInteger(Int64)

使用 64 位元帶正負號的整數值,初始化 BigInteger 結構的新執行個體。

BigInteger(Single)

使用單精確度浮點值,初始化 BigInteger 結構的新執行個體。

BigInteger(UInt32)

使用不帶正負號的 32 位元整數值,初始化 BigInteger 結構的新執行個體。

BigInteger(UInt64)

使用不帶正負號的 64 位元整數值,初始化 BigInteger 結構的新執行個體。

BigInteger(ReadOnlySpan<Byte>, Boolean, Boolean)

使用唯讀位元組範圍中的值並選擇性指出是否帶正負號與無大小順序的位元組順序,來初始化 BigInteger 結構的新執行個體。

BigInteger(Byte[])

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

重要

此 API 不符合 CLS 規範。

使用位元組陣列中的值,初始化 BigInteger 結構的新執行個體。

public:
 BigInteger(cli::array <System::Byte> ^ value);
[System.CLSCompliant(false)]
public BigInteger (byte[] value);
[<System.CLSCompliant(false)>]
new System.Numerics.BigInteger : byte[] -> System.Numerics.BigInteger
Public Sub New (value As Byte())

參數

value
Byte[]

位元組由小到大順序的位元組值陣列。

屬性

例外狀況

valuenull

範例

下列範例會從值為 {5, 4, 3, 2, 1} 的 5 個元素位元組陣列具現化 BigInteger 物件。 然後, BigInteger 它會在控制台中顯示以十進位和十六進位數位表示的值。 輸入陣列與文字輸出的比較可讓您清楚了解類別建構函式的這個多載 BigInteger 為何會建立 BigInteger 值為4328719365 (或0x102030405) 的对象。 位元組陣列的第一個元素,其值為5,定義物件之最低順序位元組 BigInteger 的值,也就是0x05。 位元組陣列的第二個元素,其值為4,定義物件第二個字節 BigInteger 的值,也就是0x04等等。

byte[] bytes = { 5, 4, 3, 2, 1 };
BigInteger number = new BigInteger(bytes);
Console.WriteLine("The value of number is {0} (or 0x{0:x}).", number);
// The example displays the following output:
//    The value of number is 4328719365 (or 0x102030405).
let bytes = [| 5uy; 4uy; 3uy; 2uy; 1uy |]
let number = new BigInteger(bytes)
printfn $"The value of number is {number} (or 0x{number:x})."
// The example displays the following output:
//    The value of number is 4328719365 (or 0x102030405).
Dim bytes() As Byte = { 5, 4, 3, 2, 1 }
Dim number As New BigInteger(bytes)
Console.WriteLine("The value of number is {0} (or 0x{0:x}).", number) 
' The example displays the following output:
'    The value of number is 4328719365 (or 0x102030405).

下列範例會具現化正值和負 BigInteger 值、將它們傳遞至 ToByteArray 方法,然後從產生的位元組陣列還原原始 BigInteger 值。 請注意,這兩個值是以相同的位元組陣組表示。 它們之間的唯一差異在於位元組陣列中最後一個元素的最顯著位。 如果陣列是從負 BigInteger 值建立 0xFF) ,則會在位元組的值 (設定這個位。 如果陣列是從正 BigInteger 值建立,則位不會設定 (位元組的值為零) 。

// Instantiate BigInteger values.
BigInteger positiveValue = BigInteger.Parse("4713143110832790377889");
BigInteger negativeValue = BigInteger.Add(-Int64.MaxValue, -60000);
BigInteger positiveValue2, negativeValue2;

// Create two byte arrays.
byte[] positiveBytes = positiveValue.ToByteArray();
byte[] negativeBytes = negativeValue.ToByteArray();

// Instantiate new BigInteger from negativeBytes array.
Console.Write("Converted {0:N0} to the byte array ", negativeValue);
foreach (byte byteValue in negativeBytes)
   Console.Write("{0:X2} ", byteValue);
Console.WriteLine();
negativeValue2 = new BigInteger(negativeBytes);
Console.WriteLine("Converted the byte array to {0:N0}", negativeValue2);
Console.WriteLine();

// Instantiate new BigInteger from positiveBytes array.
Console.Write("Converted {0:N0} to the byte array ", positiveValue);
foreach (byte byteValue in positiveBytes)
   Console.Write("{0:X2} ", byteValue);
Console.WriteLine();
positiveValue2 = new BigInteger(positiveBytes);
Console.WriteLine("Converted the byte array to {0:N0}", positiveValue2);
Console.WriteLine();
// The example displays the following output:
//    Converted -9,223,372,036,854,835,807 to the byte array A1 15 FF FF FF FF FF 7F FF
//    Converted the byte array to -9,223,372,036,854,835,807
//
//    Converted 4,713,143,110,832,790,377,889 to the byte array A1 15 FF FF FF FF FF 7F FF 00
//    Converted the byte array to 4,713,143,110,832,790,377,889
// Instantiate BigInteger values.
let positiveValue = BigInteger.Parse "4713143110832790377889"
let negativeValue = BigInteger.Add(-Int64.MaxValue, -60000)

// Create two byte arrays.
let positiveBytes = positiveValue.ToByteArray()
let negativeBytes = negativeValue.ToByteArray()

// Instantiate new BigInteger from negativeBytes array.
printf $"Converted {negativeValue:N0} to the byte array "

for byteValue in negativeBytes do
    printf $"0x{byteValue:x2} "

printfn ""
let negativeValue2 = bigint negativeBytes
printfn $"Converted the byte array to {negativeValue2:N0}"
printfn ""

// Instantiate new BigInteger from positiveBytes array.
printf $"Converted {positiveValue:N0} to the byte array "

for byteValue in positiveBytes do
    printf $"0x{byteValue:x2} "

printfn ""
let positiveValue2 = new BigInteger(positiveBytes)
printfn $"Converted the byte array to {positiveValue2:N0}"
printfn ""
// The example displays the following output:
//    Converted -9,223,372,036,854,835,807 to the byte array A1 15 FF FF FF FF FF 7F FF
//    Converted the byte array to -9,223,372,036,854,835,807
//
//    Converted 4,713,143,110,832,790,377,889 to the byte array A1 15 FF FF FF FF FF 7F FF 00
//    Converted the byte array to 4,713,143,110,832,790,377,889
' Instantiate BigInteger values.
Dim positiveValue As BigInteger = BigInteger.Parse("4713143110832790377889")
Dim negativeValue As BigInteger = BigInteger.Add(-Int64.MaxValue, -60000) 
Dim positiveValue2, negativeValue2 As BigInteger

' Create two byte arrays.
Dim positiveBytes() As Byte = positiveValue.ToByteArray()
Dim negativeBytes() As Byte = negativeValue.ToByteArray()

' Instantiate new BigInteger from negativeBytes array.
Console.Write("Converted {0:N0} to the byte array ", negativeValue)
For Each byteValue As Byte In negativeBytes
   Console.Write("{0:X2} ", byteValue)
Next 
Console.WriteLine()
negativeValue2 = New BigInteger(negativeBytes)
Console.WriteLine("Converted the byte array to {0:N0}", negativeValue2)
Console.WriteLine()

' Instantiate new BigInteger from positiveBytes array.
Console.Write("Converted {0:N0} to the byte array ", positiveValue)
For Each byteValue As Byte In positiveBytes
   Console.Write("{0:X2} ", byteValue)
Next 
Console.WriteLine()
positiveValue2 = New BigInteger(positiveBytes)
Console.WriteLine("Converted the byte array to {0:N0}", positiveValue2)
Console.WriteLine()
' The example displays the following output:
'    Converted -9,223,372,036,854,835,807 to the byte array A1 15 FF FF FF FF FF 7F FF
'    Converted the byte array to -9,223,372,036,854,835,807
'    
'    Converted 4,713,143,110,832,790,377,889 to the byte array A1 15 FF FF FF FF FF 7F FF 00
'    Converted the byte array to 4,713,143,110,832,790,377,889

下列範例說明如何藉由將值為零的位元組加入數位結尾,以確保正值不正確地具現化為負值。

ulong originalNumber = UInt64.MaxValue;
byte[] bytes = BitConverter.GetBytes(originalNumber);
if (originalNumber > 0 && (bytes[bytes.Length - 1] & 0x80) > 0)
{
   byte[] temp = new byte[bytes.Length];
   Array.Copy(bytes, temp, bytes.Length);
   bytes = new byte[temp.Length + 1];
   Array.Copy(temp, bytes, temp.Length);
}

BigInteger newNumber = new BigInteger(bytes);
Console.WriteLine("Converted the UInt64 value {0:N0} to {1:N0}.",
                  originalNumber, newNumber);
// The example displays the following output:
//    Converted the UInt64 value 18,446,744,073,709,551,615 to 18,446,744,073,709,551,615.
let originalNumber = UInt64.MaxValue
let mutable bytes = BitConverter.GetBytes originalNumber

if originalNumber > 0uL && (bytes[bytes.Length - 1] &&& 0x80uy) > 0uy then
    let temp = Array.zeroCreate bytes.Length

    Array.Copy(bytes, temp, bytes.Length)
    bytes <- Array.zeroCreate (temp.Length + 1)
    Array.Copy(temp, bytes, temp.Length)

let newNumber = bigint bytes
printfn $"Converted the UInt64 value {originalNumber:N0} to {newNumber:N0}."
// The example displays the following output:
//    Converted the UInt64 value 18,446,744,073,709,551,615 to 18,446,744,073,709,551,615.
Dim originalNumber As ULong = UInt64.MaxValue
' Convert an unsigned integer to a byte array.
Dim bytes() As Byte = BitConverter.GetBytes(originalNumber)
' Determine whether the MSB of the highest-order byte is set.
If originalNumber > 0 And (bytes(bytes.Length - 1) And &h80) > 0 Then
   ' If the MSB is set, add one zero-value byte to the end of the array.
   ReDim Preserve bytes(bytes.Length)
End If

Dim newNumber As New BigInteger(bytes)
Console.WriteLine("Converted the UInt64 value {0:N0} to {1:N0}.", 
                  originalNumber, newNumber) 
' The example displays the following output:
'    Converted the UInt64 value 18,446,744,073,709,551,615 to 18,446,744,073,709,551,615.

備註

陣列中的 value 個別位元組應該以小到尾順序,從最低順序位元組到最高順序位元組。 例如,數值 1,000,000,000,000 代表,如下表所示:

十六進位字串 E8D4A51000
位元組數位列 (最先) 最低索引 00 10 A5 D4 E8 00

大部分將數值轉換成位元組陣列的方法,例如 BigInteger.ToByteArrayBitConverter.GetBytes,會以小到尾順序傳回位元組陣列。

建構函式預期位元組陣列中的正值會使用符號和大小表示法,而負值則使用兩個補碼表示法。 換句話說,如果 中 value 最高順序位元組的最高順序位已設定,則產生的 BigInteger 值會是負數。 根據位元組陣列的來源,這可能會導致正值誤譯為負值。 位元組數組通常會以下列方式產生:

  • 藉由呼叫 BigInteger.ToByteArray 方法。 由於這個方法會傳回位元組陣列,且陣列中最高順序位元組的最高順序位元組位設定為零的正值,因此無法將正值誤譯為負值。 方法所 ToByteArray 建立的未修改位元組數位在傳遞至 BigInteger(Byte[]) 建構函式時,一律會成功往返。

  • 藉由呼叫 BitConverter.GetBytes 方法,並將帶正負號的整數傳遞為參數。 因為帶正負號的整數同時處理正負號和大小表示法和兩個補碼表示法,所以無法將正值誤譯為負數。

  • 藉由呼叫 BitConverter.GetBytes 方法,並將不帶正負號的整數傳遞為參數。 因為不帶正負號的整數只以其大小表示,所以正值可能會誤譯為負值。 若要避免這種錯誤解譯,您可以將零位元組值新增至數位結尾。 下一節中的範例提供圖例。

  • 藉由以動態或靜態方式建立位元組陣列,而不一定呼叫任何先前的方法,或修改現有的位元組陣列。 若要防止正值誤譯為負值,您可以將零位元組值新增至陣列結尾。

如果 value 是空 Byte 陣列,則會將新的 BigInteger 物件初始化為的值 BigInteger.Zero。 如果 為 valuenull,則建構函式會擲回 ArgumentNullException

另請參閱

適用於

BigInteger(Decimal)

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

使用 BigInteger 值,初始化 Decimal 結構的新執行個體。

public:
 BigInteger(System::Decimal value);
public BigInteger (decimal value);
new System.Numerics.BigInteger : decimal -> System.Numerics.BigInteger
Public Sub New (value As Decimal)

參數

value
Decimal

十進位數字。

範例

下列範例說明如何使用 BigInteger(Decimal) 建構函式具現化 BigInteger 物件。 它會定義值的陣列 Decimal ,然後將每個值傳遞至建 BigInteger(Decimal) 構函式。 請注意,此值 Decimal 會在指派給 BigInteger 物件時截斷,而不是捨入。

decimal[] decimalValues = { -1790.533m, -15.1514m, 18903.79m, 9180098.003m };
foreach (decimal decimalValue in decimalValues)
{
   BigInteger number = new BigInteger(decimalValue);
   Console.WriteLine("Instantiated BigInteger value {0} from the Decimal value {1}.",
                     number, decimalValue);
}
// The example displays the following output:
//    Instantiated BigInteger value -1790 from the Decimal value -1790.533.
//    Instantiated BigInteger value -15 from the Decimal value -15.1514.
//    Instantiated BigInteger value 18903 from the Decimal value 18903.79.
//    Instantiated BigInteger value 9180098 from the Decimal value 9180098.003.
let decimalValues = [ -1790.533m; -15.1514m; 18903.79m; 9180098.003m ]

for decimalValue in decimalValues do
    let number = bigint decimalValue
    printfn $"Instantiated BigInteger value {number} from the Decimal value {decimalValue}."
// The example displays the following output:
//    Instantiated BigInteger value -1790 from the Decimal value -1790.533.
//    Instantiated BigInteger value -15 from the Decimal value -15.1514.
//    Instantiated BigInteger value 18903 from the Decimal value 18903.79.
//    Instantiated BigInteger value 9180098 from the Decimal value 9180098.003.
Dim decimalValues() As Decimal = { -1790.533d, -15.1514d, 18903.79d, 9180098.003d }
For Each decimalValue As Decimal In decimalValues
   Dim number As New BigInteger(decimalValue)
   Console.WriteLine("Instantiated BigInteger value {0} from the Decimal value {1}.",
                     number, decimalValue)
Next                 
' The example displays the following output:
'    Instantiated BigInteger value -1790 from the Decimal value -1790.533.
'    Instantiated BigInteger value -15 from the Decimal value -15.1514.
'    Instantiated BigInteger value 18903 from the Decimal value 18903.79.
'    Instantiated BigInteger value 9180098 from the Decimal value 9180098.003.

備註

呼叫這個建構函式的結果與明確將值指派 DecimalBigInteger 變數相同。

呼叫此建構函式可能會導致數據遺失;在具現化 BigInteger 物件時,會截斷 的任何value小數部分。

適用於

BigInteger(Double)

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

使用雙精確度浮點值,初始化 BigInteger 結構的新執行個體。

public:
 BigInteger(double value);
public BigInteger (double value);
new System.Numerics.BigInteger : double -> System.Numerics.BigInteger
Public Sub New (value As Double)

參數

value
Double

雙精確度浮點值。

例外狀況

範例

下列範例說明如何使用 BigInteger(Double) 建構函式具現化 BigInteger 物件。 它也說明當您使用 Double 數據類型時可能發生的精確度遺失。 Double會指派大型值,然後指派給 BigInteger 物件。 如輸出所示,此指派牽涉到遺失精確度。 然後,這兩個值都會遞增一個。 輸出會顯示 BigInteger 物件反映已變更的值,而 Double 物件則不會。

// Create a BigInteger from a large double value.
double doubleValue = -6e20;
BigInteger bigIntValue = new BigInteger(doubleValue);
Console.WriteLine("Original Double value: {0:N0}", doubleValue);
Console.WriteLine("Original BigInteger value: {0:N0}", bigIntValue);
// Increment and then display both values.
doubleValue++;
bigIntValue += BigInteger.One;
Console.WriteLine("Incremented Double value: {0:N0}", doubleValue);
Console.WriteLine("Incremented BigInteger value: {0:N0}", bigIntValue);
// The example displays the following output:
//    Original Double value: -600,000,000,000,000,000,000
//    Original BigInteger value: -600,000,000,000,000,000,000
//    Incremented Double value: -600,000,000,000,000,000,000
//    Incremented BigInteger value: -599,999,999,999,999,999,999
// Create a BigInteger from a large double value.
let doubleValue = -6e20
let bigIntValue = bigint doubleValue
printfn $"Original Double value: {doubleValue:N0}"
printfn $"Original BigInteger value: {bigIntValue:N0}"
// Increment and then display both values.
let doubleValue = doubleValue + 1.
let bigIntValue = bigIntValue + BigInteger.One
printfn $"Incremented Double value: {doubleValue:N0}"
printfn $"Incremented BigInteger value: {bigIntValue:N0}"
// The example displays the following output:
//    Original Double value: -600,000,000,000,000,000,000
//    Original BigInteger value: -600,000,000,000,000,000,000
//    Incremented Double value: -600,000,000,000,000,000,000
//    Incremented BigInteger value: -599,999,999,999,999,999,999
' Create a BigInteger from a large double value.
Dim doubleValue As Double = -6e20
Dim bigIntValue As New BigInteger(doubleValue)
Console.WriteLine("Original Double value: {0:N0}", doubleValue)
Console.WriteLine("Original BigInteger value: {0:N0}", bigIntValue)
' Increment and then display both values.
doubleValue += 1
bigIntValue += BigInteger.One
Console.WriteLine("Incremented Double value: {0:N0}", doubleValue)
Console.WriteLine("Incremented BigInteger value: {0:N0}", bigIntValue)
' The example displays the following output:
'    Original Double value: -600,000,000,000,000,000,000
'    Original BigInteger value: -600,000,000,000,000,000,000
'    Incremented Double value: -600,000,000,000,000,000,000
'    Incremented BigInteger value: -599,999,999,999,999,999,999

備註

在具BigInteger現化 物件時,參數的任何小數部分value會截斷。

由於數據類型的精確度 Double 不足,因此呼叫此建構函式可能會導致數據遺失。

BigInteger呼叫這個建構函式所產生的值與明確指派值給 DoubleBigInteger的值相同。

適用於

BigInteger(Int32)

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

使用 32 位元帶正負號的整數值,初始化 BigInteger 結構的新執行個體。

public:
 BigInteger(int value);
public BigInteger (int value);
new System.Numerics.BigInteger : int -> System.Numerics.BigInteger
Public Sub New (value As Integer)

參數

value
Int32

32 位元帶正負號的整數。

範例

下列範例會呼叫 建構函式, BigInteger(Int32) 以具現化 BigInteger 32 位整數陣列中的值。 它也會使用隱含轉換,將每個32位整數值指派給 BigInteger 變數。 然後,它會比較兩個值,以建立產生的 BigInteger 值相同。

int[] integers = { Int32.MinValue, -10534, -189, 0, 17, 113439,
                   Int32.MaxValue };
BigInteger constructed, assigned;

foreach (int number in integers)
{
   constructed = new BigInteger(number);
   assigned = number;
   Console.WriteLine("{0} = {1}: {2}", constructed, assigned,
                     constructed.Equals(assigned));
}
// The example displays the following output:
//       -2147483648 = -2147483648: True
//       -10534 = -10534: True
//       -189 = -189: True
//       0 = 0: True
//       17 = 17: True
//       113439 = 113439: True
//       2147483647 = 2147483647: True
let integers = [ Int32.MinValue; -10534; -189; 0; 17; 113439; Int32.MaxValue ]

for number in integers do
    let constructed = bigint number
    let assigned = number
    printfn $"{constructed} = {assigned}: {constructed.Equals assigned}"
// The example displays the following output:
//       -2147483648 = -2147483648: True
//       -10534 = -10534: True
//       -189 = -189: True
//       0 = 0: True
//       17 = 17: True
//       113439 = 113439: True
//       2147483647 = 2147483647: True
Dim integers() As Integer = { Int32.MinValue, -10534, -189, 0, 17, 113439,
                              Int32.MaxValue }
Dim constructed, assigned As BigInteger

For Each number As Integer In integers
   constructed = New BigInteger(number)
   assigned = number
   Console.WriteLine("{0} = {1}: {2}", constructed, assigned, 
                     constructed.Equals(assigned)) 
Next
' The example displays the following output:
'       -2147483648 = -2147483648: True
'       -10534 = -10534: True
'       -189 = -189: True
'       0 = 0: True
'       17 = 17: True
'       113439 = 113439: True
'       2147483647 = 2147483647: True

備註

使用這個建構函式具現化 BigInteger 物件時,不會遺失精確度。

呼叫 BigInteger 這個建構函式所產生的值與將值指派 Int32BigInteger所產生的值相同。

結構BigInteger不包含類型為、 Int16SByteUInt16的參數建Byte構函式。 不過,此 Int32 類型支援將8位和16位帶正負號的整數隱含轉換成帶正負號的32位整數。 因此,如果 value 是這四個整數型別的其中一個,就會呼叫這個建構函式。

適用於

BigInteger(Int64)

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

使用 64 位元帶正負號的整數值,初始化 BigInteger 結構的新執行個體。

public:
 BigInteger(long value);
public BigInteger (long value);
new System.Numerics.BigInteger : int64 -> System.Numerics.BigInteger
Public Sub New (value As Long)

參數

value
Int64

64 位元帶正負號的整數。

範例

下列範例會呼叫 建構函式, BigInteger(Int64) 以具現化 BigInteger 64 位整數陣列中的值。 它也會使用隱含轉換,將每個64位整數值指派給 BigInteger 變數。 然後,它會比較兩個值,以建立產生的 BigInteger 值相同。

long[] longs = { Int64.MinValue, -10534, -189, 0, 17, 113439,
                 Int64.MaxValue };
BigInteger constructed, assigned;

foreach (long number in longs)
{
   constructed = new BigInteger(number);
   assigned = number;
   Console.WriteLine("{0} = {1}: {2}", constructed, assigned,
                     constructed.Equals(assigned));
}
// The example displays the following output:
//       -2147483648 = -2147483648: True
//       -10534 = -10534: True
//       -189 = -189: True
//       0 = 0: True
//       17 = 17: True
//       113439 = 113439: True
//       2147483647 = 2147483647: True
let longs = [ Int64.MinValue; -10534; -189; 0; 17; 113439; Int64.MaxValue ]

for number in longs do
    let constructed = bigint number
    let assigned = number
    printfn $"{constructed} = {assigned}: {constructed.Equals assigned}"
// The example displays the following output:
//       -2147483648 = -2147483648: True
//       -10534 = -10534: True
//       -189 = -189: True
//       0 = 0: True
//       17 = 17: True
//       113439 = 113439: True
//       2147483647 = 2147483647: True
Dim longs() As Long = { Int64.MinValue, -10534, -189, 0, 17, 113439,
                              Int64.MaxValue }
Dim constructed, assigned As BigInteger

For Each number As Long In longs
   constructed = New BigInteger(number)
   assigned = number
   Console.WriteLine("{0} = {1}: {2}", constructed, assigned, 
                     constructed.Equals(assigned)) 
Next
' The example displays the following output:
'       -2147483648 = -2147483648: True
'       -10534 = -10534: True
'       -189 = -189: True
'       0 = 0: True
'       17 = 17: True
'       113439 = 113439: True
'       2147483647 = 2147483647: True

備註

使用這個建構函式具現化 BigInteger 物件時,不會遺失精確度。

呼叫 BigInteger 這個建構函式所產生的值與將值指派 Int64BigInteger所產生的值相同。

適用於

BigInteger(Single)

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

使用單精確度浮點值,初始化 BigInteger 結構的新執行個體。

public:
 BigInteger(float value);
public BigInteger (float value);
new System.Numerics.BigInteger : single -> System.Numerics.BigInteger
Public Sub New (value As Single)

參數

value
Single

單精確度浮點值。

例外狀況

範例

下列範例說明如何使用 BigInteger(Single) 建構函式具現化 BigInteger 物件。 它也說明當您使用 Single 數據類型時可能發生的精確度遺失。 Single會指派大型負值,然後指派給 BigInteger 物件。 如輸出所示,此指派牽涉到遺失精確度。 然後,這兩個值都會遞增一。 輸出顯示 BigInteger 物件反映已變更的值,而 Single 物件則不會。

// Create a BigInteger from a large negative Single value
float negativeSingle = Single.MinValue;
BigInteger negativeNumber = new BigInteger(negativeSingle);

Console.WriteLine(negativeSingle.ToString("N0"));
Console.WriteLine(negativeNumber.ToString("N0"));

negativeSingle++;
negativeNumber++;

Console.WriteLine(negativeSingle.ToString("N0"));
Console.WriteLine(negativeNumber.ToString("N0"));
// The example displays the following output:
//       -340,282,300,000,000,000,000,000,000,000,000,000,000
//       -340,282,346,638,528,859,811,704,183,484,516,925,440
//       -340,282,300,000,000,000,000,000,000,000,000,000,000
//       -340,282,346,638,528,859,811,704,183,484,516,925,439
// Create a BigInteger from a large negative Single value
let negativeSingle = Single.MinValue
let negativeNumber = bigint negativeSingle

printfn $"""{negativeSingle.ToString "N0"}"""
printfn $"""{negativeNumber.ToString "N0"}"""

let negativeSingle = negativeSingle + 1f
let negativeNumber = negativeNumber + 1I

printfn $"""{negativeSingle.ToString "N0"}"""
printfn $"""{negativeNumber.ToString "N0"}"""
// The example displays the following output:
//       -340,282,300,000,000,000,000,000,000,000,000,000,000
//       -340,282,346,638,528,859,811,704,183,484,516,925,440
//       -340,282,300,000,000,000,000,000,000,000,000,000,000
//       -340,282,346,638,528,859,811,704,183,484,516,925,439
' Create a BigInteger from a large negative Single value
Dim negativeSingle As Single = Single.MinValue
Dim negativeNumber As New BigInteger(negativeSingle)

Console.WriteLine(negativeSingle.ToString("N0"))
Console.WriteLine(negativeNumber.ToString("N0"))

negativeSingle += 1
negativeNumber += 1
Console.WriteLine(negativeSingle.ToString("N0"))
Console.WriteLine(negativeNumber.ToString("N0"))
' The example displays the following output:
'       -340,282,300,000,000,000,000,000,000,000,000,000,000
'       -340,282,346,638,528,859,811,704,183,484,516,925,440
'       -340,282,300,000,000,000,000,000,000,000,000,000,000
'       -340,282,346,638,528,859,811,704,183,484,516,925,439

備註

具現化 物件時BigInteger,會截斷參數的任何小數部分value

由於缺少數據類型的有效 Single 位數,因此呼叫這個建構函式可能會導致數據遺失。

BigInteger呼叫這個建構函式所產生的值與明確指派Single值給BigInteger的值相同。

適用於

BigInteger(UInt32)

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

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Numerics.BigInteger.BigInteger(Int64)

使用不帶正負號的 32 位元整數值,初始化 BigInteger 結構的新執行個體。

public:
 BigInteger(System::UInt32 value);
[System.CLSCompliant(false)]
public BigInteger (uint value);
[<System.CLSCompliant(false)>]
new System.Numerics.BigInteger : uint32 -> System.Numerics.BigInteger
Public Sub New (value As UInteger)

參數

value
UInt32

32 位元不帶正負號的整數值。

屬性

範例

下列範例會使用建 BigInteger(UInt32) 構函式和指派語句,從不帶正負號的 32 位整數陣列初始化 BigInteger 值。 然後,它會比較這兩個值,以示範初始化值的兩種方法 BigInteger 會產生相同的結果。

uint[] unsignedValues = { 0, 16704, 199365, UInt32.MaxValue };
foreach (uint unsignedValue in unsignedValues)
{
   BigInteger constructedNumber = new BigInteger(unsignedValue);
   BigInteger assignedNumber = unsignedValue;
   if (constructedNumber.Equals(assignedNumber))
      Console.WriteLine("Both methods create a BigInteger whose value is {0:N0}.",
                        constructedNumber);
   else
      Console.WriteLine("{0:N0} ≠ {1:N0}", constructedNumber, assignedNumber);
}
// The example displays the following output:
//    Both methods create a BigInteger whose value is 0.
//    Both methods create a BigInteger whose value is 16,704.
//    Both methods create a BigInteger whose value is 199,365.
//    Both methods create a BigInteger whose value is 4,294,967,295.
let unsignedValues = [ 0u; 16704u; 199365u; UInt32.MaxValue ]

for unsignedValue in unsignedValues do
    let constructedNumber = bigint unsignedValue
    let assignedNumber = unsignedValue

    if constructedNumber.Equals assignedNumber then
        printfn $"Both methods create a BigInteger whose value is {constructedNumber:N0}."
    else
        printfn $"{constructedNumber:N0} ≠ {assignedNumber:N0}"
// The example displays the following output:
//    Both methods create a BigInteger whose value is 0.
//    Both methods create a BigInteger whose value is 16,704.
//    Both methods create a BigInteger whose value is 199,365.
//    Both methods create a BigInteger whose value is 4,294,967,295.
Dim unsignedValues() As UInteger = { 0, 16704, 199365, UInt32.MaxValue }
For Each unsignedValue As UInteger In unsignedValues
   Dim constructedNumber As New BigInteger(unsignedValue)
   Dim assignedNumber As BigInteger = unsignedValue
   If constructedNumber.Equals(assignedNumber) Then
      Console.WriteLine("Both methods create a BigInteger whose value is {0:N0}.",
                        constructedNumber)
   Else
      Console.WriteLine("{0:N0} ≠ {1:N0}", constructedNumber, assignedNumber)
   End If                         
Next
' The example displays the following output:
'    Both methods create a BigInteger whose value is 0.
'    Both methods create a BigInteger whose value is 16,704.
'    Both methods create a BigInteger whose value is 199,365.
'    Both methods create a BigInteger whose value is 4,294,967,295.

備註

使用這個建構函式具現化 BigInteger 時,不會遺失精確度。

BigInteger呼叫這個建構函式所產生的值與將值指派UInt32BigInteger所產生的值相同。

適用於

BigInteger(UInt64)

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

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Numerics.BigInteger.BigInteger(Double)

使用不帶正負號的 64 位元整數值,初始化 BigInteger 結構的新執行個體。

public:
 BigInteger(System::UInt64 value);
[System.CLSCompliant(false)]
public BigInteger (ulong value);
[<System.CLSCompliant(false)>]
new System.Numerics.BigInteger : uint64 -> System.Numerics.BigInteger
Public Sub New (value As ULong)

參數

value
UInt64

不帶正負號的 64 位元整數。

屬性

範例

下列範例會使用 建 BigInteger(UInt64) 構函式來具現化 BigInteger 其值等於 MaxValue的物件。

ulong unsignedValue = UInt64.MaxValue;
BigInteger number = new BigInteger(unsignedValue);
Console.WriteLine(number.ToString("N0"));
// The example displays the following output:
//       18,446,744,073,709,551,615
let unsignedValue = UInt64.MaxValue
let number = bigint unsignedValue
printfn $"{number:N0}"
// The example displays the following output:
//       18,446,744,073,709,551,615
Dim unsignedValue As ULong = UInt64.MaxValue
Dim number As New BigInteger(unsignedValue)
Console.WriteLine(number.ToString("N0"))       
' The example displays the following output:
'       18,446,744,073,709,551,615

備註

使用這個建構函式具現化 BigInteger 時,不會遺失精確度。

BigInteger呼叫這個建構函式所產生的值與將值指派UInt64BigInteger所產生的值相同。

適用於

BigInteger(ReadOnlySpan<Byte>, Boolean, Boolean)

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

使用唯讀位元組範圍中的值並選擇性指出是否帶正負號與無大小順序的位元組順序,來初始化 BigInteger 結構的新執行個體。

public BigInteger (ReadOnlySpan<byte> value, bool isUnsigned = false, bool isBigEndian = false);
new System.Numerics.BigInteger : ReadOnlySpan<byte> * bool * bool -> System.Numerics.BigInteger
Public Sub New (value As ReadOnlySpan(Of Byte), Optional isUnsigned As Boolean = false, Optional isBigEndian As Boolean = false)

參數

value
ReadOnlySpan<Byte>

代表大整數的唯讀位元組範圍。

isUnsigned
Boolean

true 以指出 value 使用不帶正負號的編碼;否則為 false (預設值)。

isBigEndian
Boolean

true value表示是以大到小位元組的順序,否則 (false 預設值) 。

適用於