BigInteger.Parse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將數字的字串表示轉換為其 BigInteger 等價物。
多載
| 名稱 | Description |
|---|---|
| Parse(String, NumberStyles, IFormatProvider) |
將特定風格及文化特定格式中的數字字串表示轉換為其 BigInteger 等價值。 |
| Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
將指定唯讀字元區間內的數字表示,以特定風格轉換為其 BigInteger 等價值。 |
| Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) | |
| Parse(String, IFormatProvider) |
將特定文化格式中的數字字串表示轉換為其 BigInteger 等價值。 |
| Parse(ReadOnlySpan<Byte>, IFormatProvider) | |
| Parse(ReadOnlySpan<Char>, IFormatProvider) |
將一組字元解析成一個數值。 |
| Parse(String, NumberStyles) |
將特定風格中的數字字串表示轉換為其 BigInteger 等價形式。 |
| Parse(String) |
將數字的字串表示轉換為其 BigInteger 等價物。 |
Parse(String, NumberStyles, IFormatProvider)
將特定風格及文化特定格式中的數字字串表示轉換為其 BigInteger 等價值。
public:
static System::Numerics::BigInteger Parse(System::String ^ value, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
static System::Numerics::BigInteger Parse(System::String ^ value, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<System::Numerics::BigInteger>::Parse;
public static System.Numerics.BigInteger Parse(string value, System.Globalization.NumberStyles style, IFormatProvider provider);
public static System.Numerics.BigInteger Parse(string value, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> System.Numerics.BigInteger
Public Shared Function Parse (value As String, style As NumberStyles, provider As IFormatProvider) As BigInteger
參數
- value
- String
一個包含一個需要轉換的數字的字串。
- style
- NumberStyles
一個位元組合的列舉值,指定允許的格式 value。
- provider
- IFormatProvider
一個提供關於 value的文化特定格式資訊的物件。
傳回
一個等同於參數中 value 指定數字的值。
實作
例外狀況
value 為 null。
value 不符合由 style所指定的輸入模式。
範例
以下範例多次呼叫該Parse(String, NumberStyles, IFormatProvider)方法,使用與參數的各種值styleprovider組合。
// Call parse with default values of style and provider
Console.WriteLine(BigInteger.Parse(" -300 ",
NumberStyles.Integer, CultureInfo.CurrentCulture));
// Call parse with default values of style and provider supporting tilde as negative sign
Console.WriteLine(BigInteger.Parse(" ~300 ",
NumberStyles.Integer, new BigIntegerFormatProvider()));
// Call parse with only AllowLeadingWhite and AllowTrailingWhite
// Exception thrown because of presence of negative sign
try
{
Console.WriteLine(BigInteger.Parse(" ~300 ",
NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite,
new BigIntegerFormatProvider()));
}
catch (FormatException e)
{
Console.WriteLine("{0}: \n {1}", e.GetType().Name, e.Message);
}
// Call parse with only AllowHexSpecifier
// Exception thrown because of presence of negative sign
try
{
Console.WriteLine(BigInteger.Parse("-3af", NumberStyles.AllowHexSpecifier,
new BigIntegerFormatProvider()));
}
catch (FormatException e)
{
Console.WriteLine("{0}: \n {1}", e.GetType().Name, e.Message);
}
// Call parse with only NumberStyles.None
// Exception thrown because of presence of white space and sign
try
{
Console.WriteLine(BigInteger.Parse(" -300 ", NumberStyles.None,
new BigIntegerFormatProvider()));
}
catch (FormatException e)
{
Console.WriteLine("{0}: \n {1}", e.GetType().Name, e.Message);
}
// The example displays the followingoutput:
// -300
// -300
// FormatException:
// The value could not be parsed.
// FormatException:
// The value could not be parsed.
// FormatException:
// The value could not be parsed.
// Call parse with default values of style and provider
printfn $"""{BigInteger.Parse(" -300 ", NumberStyles.Integer, CultureInfo.CurrentCulture)}"""
// Call parse with default values of style and provider supporting tilde as negative sign
printfn $"""{BigInteger.Parse(" ~300 ", NumberStyles.Integer, new BigIntegerFormatProvider())}"""
// Call parse with only AllowLeadingWhite and AllowTrailingWhite
// Exception thrown because of presence of negative sign
try
printfn
$"""{BigInteger.Parse(
" ~300 ",
NumberStyles.AllowLeadingWhite ||| NumberStyles.AllowTrailingWhite,
new BigIntegerFormatProvider()
)}"""
with :? FormatException as e ->
printfn $"{e.GetType().Name}: \n {e.Message}"
// Call parse with only AllowHexSpecifier
// Exception thrown because of presence of negative sign
try
printfn $"""{BigInteger.Parse("-3af", NumberStyles.AllowHexSpecifier, new BigIntegerFormatProvider())}"""
with :? FormatException as e ->
printfn $"{e.GetType().Name}: \n {e.Message}"
// Call parse with only NumberStyles.None
// Exception thrown because of presence of white space and sign
try
printfn $"""{BigInteger.Parse(" -300 ", NumberStyles.None, new BigIntegerFormatProvider())}"""
with :? FormatException as e ->
printfn $"{e.GetType().Name}: \n {e.Message}"
// The example displays the followingoutput:
// -300
// -300
// FormatException:
// The value could not be parsed.
// FormatException:
// The value could not be parsed.
// FormatException:
// The value could not be parsed.
' Call parse with default values of style and provider
Console.WriteLine(BigInteger.Parse(" -300 ", _
NumberStyles.Integer, CultureInfo.CurrentCulture))
' Call parse with default values of style and provider supporting tilde as negative sign
Console.WriteLine(BigInteger.Parse(" ~300 ", _
NumberStyles.Integer, New BigIntegerFormatProvider()))
' Call parse with only AllowLeadingWhite and AllowTrailingWhite
' Exception thrown because of presence of negative sign
Try
Console.WriteLIne(BigInteger.Parse(" ~300 ", _
NumberStyles.AllowLeadingWhite Or NumberStyles.AllowTrailingWhite, _
New BigIntegerFormatProvider()))
Catch e As FormatException
Console.WriteLine("{0}: {1} {2}", e.GetType().Name, vbCrLf, e.Message)
End Try
' Call parse with only AllowHexSpecifier
' Exception thrown because of presence of negative sign
Try
Console.WriteLIne(BigInteger.Parse("-3af", NumberStyles.AllowHexSpecifier, _
New BigIntegerFormatProvider()))
Catch e As FormatException
Console.WriteLine("{0}: {1} {2}", e.GetType().Name, vbCrLf, e.Message)
End Try
' Call parse with only NumberStyles.None
' Exception thrown because of presence of white space and sign
Try
Console.WriteLIne(BigInteger.Parse(" -300 ", NumberStyles.None, _
New BigIntegerFormatProvider()))
Catch e As FormatException
Console.WriteLine("{0}: {1} {2}", e.GetType().Name, vbCrLf, e.Message)
End Try
' The example displays the following output:
' -300
' -300
' FormatException:
' The value could not be parsed.
' FormatException:
' The value could not be parsed.
' FormatException:
' The value could not be parsed.
對該 Parse(String, NumberStyles, IFormatProvider) 方法的多個個別呼叫會經過下一個 BigIntegerFormatProvider 類別的實例,該類別定義了一個波浪號(~)作為負號。
public class BigIntegerFormatProvider : IFormatProvider
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(NumberFormatInfo))
{
NumberFormatInfo numberFormat = new NumberFormatInfo();
numberFormat.NegativeSign = "~";
return numberFormat;
}
else
{
return null;
}
}
}
type BigIntegerFormatProvider() =
interface IFormatProvider with
member _.GetFormat(formatType: Type) =
if formatType = typeof<NumberFormatInfo> then
let numberFormat = NumberFormatInfo()
numberFormat.NegativeSign <- "~"
numberFormat
else
null
Public Class BigIntegerFormatProvider : Implements IFormatProvider
Public Function GetFormat(formatType As Type) As Object _
Implements IFormatProvider.GetFormat
If formatType Is GetType(NumberFormatInfo) Then
Dim numberFormat As New NumberFormatInfo
numberFormat.NegativeSign = "~"
Return numberFormat
Else
Return Nothing
End If
End Function
End Class
備註
參數 style 定義了允許在解析操作成功時所允許 value 的樣式元素(例如空白、正負符號、群組分隔符號或小數點符號)。
styles 必須是列舉中多個位元旗 NumberStyles 標的組合。
style當 value 包含十六進位值的字串表示、所表示value的數字系統(十進位或十六進位)僅在執行時已知,或你想在 中排除空白或符號value符號時,這個參數使此方法過載變得有用。
根據 的 style值, value 參數可能包含以下元素:
[ws][$][符號][數字,]數字[.fractional_digits][E[符號]exponential_digits][ws]
若 style 包含 NumberStyles.AllowHexSpecifier,該 value 參數可能包含以下元素:
[ws]六角數數字[ws]
方括號內的元素([ 和 ])為可選。 下表說明每個元素。
| 元素 | Description |
|---|---|
| WS | 可選留白。 如果包含旗幟,空白可以出現在 的value開頭,如果style包含 旗NumberStyles.AllowTrailingWhite幟,則可以出現在結尾value。NumberStyles.AllowLeadingWhitestyle |
| $ | 一種文化特定的貨幣符號。 它在字串中的位置由NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern參數所指示provider的文化性質所定義。 當前style文化的貨幣符號可以出現value在包含NumberStyles.AllowCurrencySymbol國旗的情況下。 |
| 簽署 | 一個可選的標誌。 如果包含旗幟,標誌可以出現在 的value開頭,如果包含NumberStyles.AllowTrailingSign該旗幟,則可以出現在 的結尾stylevalue。NumberStyles.AllowLeadingSignstyle 若style包含旗標,NumberStyles.AllowParentheses則可使用value括號表示負值。 |
|
數字 fractional_digits exponential_digits |
一串從0到9的數字序列。 對於 fractional_digits,只有數字0有效。 |
| , | 一個文化特定的群體分隔符號。
provider若style包含旗NumberStyles.AllowThousands幟,則可出現在 中value。 |
| . | 一種文化特定的小數點符號。 若style包含NumberStyles.AllowDecimalPoint旗幟,則可顯示value該文化provider的小數點符號。 只有數字 0 可以以小數數字出現,才能成功進行解析操作;若 fractional_digits 包含其他數字,則擲出 A FormatException 。 |
| E | 「e」或「E」字元表示該數值以指數(科學)符號表示。 若style包含NumberStyles.AllowExponent旗標,參數value可用指數符號表示數字。 |
| 六角位數 | 一串從0到f,或0到F的十六進位數字序列。 |
備註
任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。
只有數字的字串(對應 NumberStyles.None 樣式)總是能成功解析。 大多數剩餘 NumberStyles 成員控制輸入字串中可能存在但不必須存在的元素。 下表顯示個別 NumberStyles 成員如何影響可能存在於 value中的元素。
| NumberStyles 的值 | 除了數字外,允許加入數值的元素 |
|---|---|
| None | 只有 數字 元素。 |
| AllowDecimalPoint | 小數點(.)和 小數數字 元素。 |
| AllowExponent | 「e」或「E」字元,表示指數符號。 以及 exponential_digits。 |
| AllowLeadingWhite | 該元素位於 的開頭value為 ws 元素。 |
| AllowTrailingWhite | 末尾value的 ws 元素。 |
| AllowLeadingSign | 符號 元素位於 的開頭 value。 |
| AllowTrailingSign | 符號 元素位於 的結尾 value。 |
| AllowParentheses | 符號 元素以 括號形式包含數值。 |
| AllowThousands | 群分離子(,)元素。 |
| AllowCurrencySymbol | 貨幣($)元素。 |
| Currency | 所有元素。 然而,無法 value 表示十六進位數或指數符號中的數字。 |
| Float | ws 元素位於 的 value起始或結 尾,符號 在 的開頭 value,以及小數點(.)符號。 參數 value 也可以使用指數符號。 |
| Number | 、wssign、群分隔符(,)和小數點(.)元素。 |
| Any | 所有元素。 然而,無法 value 代表十六進位數。 |
這很重要
如果你用這個Parse方法來循環輸出的字ToString串表示BigInteger,你應該用BigInteger.ToString(String)帶有「R」格式規範子的方法來產生該BigInteger值的字串表示。 否則,字 BigInteger 串表示只會保留原始值的最高 50 位位,使用 Parse 還原 BigInteger 方法時可能會遺失資料。
與其他NumberStyles允許但不要求特定樣式元素存在valueNumberStyles.AllowHexSpecifier的值不同,樣式值意味著 中的value個別數字字元總是被解釋為十六進位字元。 有效的十六進位字元為 0-9、A-F 和 a-f。 唯一可以與參數 style 結合的其他旗標是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite。 (列 NumberStyles 舉包含一個複合數字樣式, HexNumber包含兩個空白標記。)
備註
若 value 是十六進位數的字串表示,則其前不能加上任何裝飾(如 0x 或 &h)來區分其為十六進位數。 這會導致轉換失敗。
若 value 是十六進位字串,Parse(String, NumberStyles)若前兩個十六進位數字大於或等0x80於 ,則該方法將其value解釋為以二補數表示儲存的負數。 換句話說,該方法將第一個 value 位元組的最高階位元解釋為符號位元。 為了確保十六進位字串被正確解讀為正數,該數字的 value 第一個數字必須為零。 例如,該方法將 0x80 or 解讀為負值,但同時將 0x080 或 0x0080 解讀為正值。 以下範例說明了代表負值與正值的十六進位字串之間的差異。
using System;
using System.Globalization;
using System.Numerics;
public class Example
{
public static void Main()
{
string[] hexStrings = { "80", "E293", "F9A2FF", "FFFFFFFF",
"080", "0E293", "0F9A2FF", "0FFFFFFFF",
"0080", "00E293", "00F9A2FF", "00FFFFFFFF" };
foreach (string hexString in hexStrings)
{
BigInteger number = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier);
Console.WriteLine("Converted 0x{0} to {1}.", hexString, number);
}
}
}
// The example displays the following output:
// Converted 0x80 to -128.
// Converted 0xE293 to -7533.
// Converted 0xF9A2FF to -417025.
// Converted 0xFFFFFFFF to -1.
// Converted 0x080 to 128.
// Converted 0x0E293 to 58003.
// Converted 0x0F9A2FF to 16360191.
// Converted 0x0FFFFFFFF to 4294967295.
// Converted 0x0080 to 128.
// Converted 0x00E293 to 58003.
// Converted 0x00F9A2FF to 16360191.
// Converted 0x00FFFFFFFF to 4294967295.
open System.Globalization
open System.Numerics
let hexStrings =
[| "80"
"E293"
"F9A2FF"
"FFFFFFFF"
"080"
"0E293"
"0F9A2FF"
"0FFFFFFFF"
"0080"
"00E293"
"00F9A2FF"
"00FFFFFFFF" |]
for hexString in hexStrings do
let number = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier)
printfn $"Converted 0x{hexString} to {number:N0}."
// The example displays the following output:
// Converted 0x80 to -128.
// Converted 0xE293 to -7533.
// Converted 0xF9A2FF to -417025.
// Converted 0xFFFFFFFF to -1.
// Converted 0x080 to 128.
// Converted 0x0E293 to 58003.
// Converted 0x0F9A2FF to 16360191.
// Converted 0x0FFFFFFFF to 4294967295.
// Converted 0x0080 to 128.
// Converted 0x00E293 to 58003.
// Converted 0x00F9A2FF to 16360191.
// Converted 0x00FFFFFFFF to 4294967295.
Imports System.Globalization
Imports System.Numerics
Module Example
Public Sub Main()
Dim hexStrings() As String = { "80", "E293", "F9A2FF", "FFFFFFFF",
"080", "0E293", "0F9A2FF", "0FFFFFFFF",
"0080", "00E293", "00F9A2FF", "00FFFFFFFF" }
For Each hexString As String In hexStrings
Dim number As BigInteger = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier)
Console.WriteLine("Converted 0x{0} to {1}.", hexString, number)
Next
End Sub
End Module
' The example displays the following output:
' Converted 0x80 to -128.
' Converted 0xE293 to -7533.
' Converted 0xF9A2FF to -417025.
' Converted 0xFFFFFFFF to -1.
' Converted 0x080 to 128.
' Converted 0x0E293 to 58003.
' Converted 0x0F9A2FF to 16360191.
' Converted 0x0FFFFFFFF to 4294967295.
' Converted 0x0080 to 128.
' Converted 0x00E293 to 58003.
' Converted 0x00F9A2FF to 16360191.
' Converted 0x00FFFFFFFF to 4294967295.
參數 provider 是一個 IFormatProvider 實作。 其 GetFormat 方法回傳一個 NumberFormatInfo 物件,提供關於 格式 value的文化特定資訊。 通常, provider 可以是以下任何一種:
一個 CultureInfo 代表文化的物件,提供數字格式資訊。 其 GetFormat 方法回傳 NumberFormatInfo 提供數值格式資訊的物件。
一個 NumberFormatInfo 提供格式資訊的物件。 (它的實作 GetFormat 只是回傳自己。)
一個自訂物件,實作 IFormatProvider 並使用該 GetFormat 方法實例化並提供格式資訊的 NumberFormatInfo 物件。
若provider為 ,nullNumberFormatInfo則使用 當前文化的物件。
另請參閱
適用於
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
將指定唯讀字元區間內的數字表示,以特定風格轉換為其 BigInteger 等價值。
public static System.Numerics.BigInteger Parse(ReadOnlySpan<char> value, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
public static System.Numerics.BigInteger Parse(ReadOnlySpan<char> value, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> System.Numerics.BigInteger
Public Shared Function Parse (value As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As BigInteger
參數
- value
- ReadOnlySpan<Char>
一個只讀的字元區段,包含要轉換的數字。
- style
- NumberStyles
一個位元組合的列舉值,指定允許的格式 value。
- provider
- IFormatProvider
一個提供關於 value的文化特定格式資訊的物件。
傳回
一個等同於參數中 value 指定數字的值。
實作
例外狀況
value 為 null。
value 不符合由 style所指定的輸入模式。
備註
參數 style 定義了允許在解析操作成功時所允許 value 的樣式元素(例如空白、正負符號、群組分隔符號或小數點符號)。
styles 必須是列舉中多個位元旗 NumberStyles 標的組合。
style當 value 包含十六進位值表示、所表示value的數字系統(十進位或十六進位)僅在執行時已知,或你想在 中排除空白或符號value時,該參數使此方法過載變得有用。
根據 的 style值, value 參數可能包含以下元素:
[ws][$][符號][數字,]數字[.fractional_digits][E[符號]exponential_digits][ws]
若 style 包含 NumberStyles.AllowHexSpecifier,該 value 參數可能包含以下元素:
[ws]六角數數字[ws]
方括號內的元素([ 和 ])為可選。 下表說明每個元素。
| 元素 | Description |
|---|---|
| WS | 可選留白。 如果包含旗幟,空白可以出現在 的value開頭,如果style包含 旗NumberStyles.AllowTrailingWhite幟,則可以出現在結尾value。NumberStyles.AllowLeadingWhitestyle |
| $ | 一種文化特定的貨幣符號。 其在中 value 的位置由 NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern 與 的文化性質所定義,該參數所示 provider 。 當前style文化的貨幣符號可以出現value在包含NumberStyles.AllowCurrencySymbol國旗的情況下。 |
| 簽署 | 一個可選的標誌。 如果包含旗幟,標誌可以出現在 的value開頭,如果包含NumberStyles.AllowTrailingSign該旗幟,則可以出現在 的結尾stylevalue。NumberStyles.AllowLeadingSignstyle 若style包含旗標,NumberStyles.AllowParentheses則可使用value括號表示負值。 |
|
數字 fractional_digits exponential_digits |
一串從0到9的數字序列。 對於 fractional_digits,只有數字0有效。 |
| , | 一個文化特定的群體分隔符號。
provider若style包含旗NumberStyles.AllowThousands幟,則可出現在 中value。 |
| . | 一種文化特定的小數點符號。 若style包含NumberStyles.AllowDecimalPoint旗幟,則可顯示value該文化provider的小數點符號。 只有數字 0 可以以小數數字出現,才能成功進行解析操作;若 fractional_digits 包含其他數字,則擲出 A FormatException 。 |
| E | 「e」或「E」字元表示該數值以指數(科學)符號表示。 若style包含NumberStyles.AllowExponent旗標,參數value可用指數符號表示數字。 |
| 六角位數 | 一串從0到f,或0到F的十六進位數字序列。 |
備註
任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。
只有數字(對應NumberStyles.None樣式)的 A value 總是能成功解析。 其餘NumberStyles大多數成員控制著可能存在但不必須存在的元素。value 下表顯示個別 NumberStyles 成員如何影響可能存在於 value中的元素。
| NumberStyles 的值 | 除了數字外,允許加入數值的元素 |
|---|---|
| None | 只有 數字 元素。 |
| AllowDecimalPoint | 小數點(.)和 小數數字 元素。 |
| AllowExponent | 「e」或「E」字元,表示指數符號。 以及 exponential_digits。 |
| AllowLeadingWhite | 該元素位於 的開頭value為 ws 元素。 |
| AllowTrailingWhite | 末尾value的 ws 元素。 |
| AllowLeadingSign | 符號 元素位於 的開頭 value。 |
| AllowTrailingSign | 符號 元素位於 的結尾 value。 |
| AllowParentheses | 符號 元素以 括號形式包含數值。 |
| AllowThousands | 群分離子(,)元素。 |
| AllowCurrencySymbol | 貨幣($)元素。 |
| Currency | 所有元素。 然而,無法 value 表示十六進位數或指數符號中的數字。 |
| Float | ws 元素位於 的 value起始或結 尾,符號 在 的開頭 value,以及小數點(.)符號。 參數 value 也可以使用指數符號。 |
| Number | 、wssign、群分隔符(,)和小數點(.)元素。 |
| Any | 所有元素。 然而,無法 value 代表十六進位數。 |
與其他NumberStyles允許但不要求特定樣式元素存在valueNumberStyles.AllowHexSpecifier的值不同,樣式值意味著 中的value個別數字字元總是被解釋為十六進位字元。 有效的十六進位字元為 0-9、A-F 和 a-f。 唯一可以與參數 style 結合的其他旗標是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite。 (列 NumberStyles 舉包含一個複合數字樣式, HexNumber包含兩個空白標記。)
參數 provider 是一個 IFormatProvider 實作。 其 GetFormat 方法回傳一個 NumberFormatInfo 物件,提供關於 格式 value的文化特定資訊。 通常, provider 可以是以下任何一種:
一個 CultureInfo 代表文化的物件,提供數字格式資訊。 其 GetFormat 方法回傳 NumberFormatInfo 提供數值格式資訊的物件。
一個 NumberFormatInfo 提供格式資訊的物件。 (它的實作 GetFormat 只是回傳自己。)
一個自訂物件,實作 IFormatProvider 並使用該 GetFormat 方法實例化並提供格式資訊的 NumberFormatInfo 物件。
若provider為 ,nullNumberFormatInfo則使用 當前文化的物件。
另請參閱
適用於
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
public static System.Numerics.BigInteger Parse(ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> System.Numerics.BigInteger
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As BigInteger
參數
- utf8Text
- ReadOnlySpan<Byte>
- style
- NumberStyles
- provider
- IFormatProvider
傳回
實作
適用於
Parse(String, IFormatProvider)
將特定文化格式中的數字字串表示轉換為其 BigInteger 等價值。
public:
static System::Numerics::BigInteger Parse(System::String ^ value, IFormatProvider ^ provider);
public:
static System::Numerics::BigInteger Parse(System::String ^ value, IFormatProvider ^ provider) = IParsable<System::Numerics::BigInteger>::Parse;
public static System.Numerics.BigInteger Parse(string value, IFormatProvider provider);
public static System.Numerics.BigInteger Parse(string value, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> System.Numerics.BigInteger
Public Shared Function Parse (value As String, provider As IFormatProvider) As BigInteger
參數
- value
- String
一個包含一個需要轉換的數字的字串。
- provider
- IFormatProvider
一個提供關於 value的文化特定格式資訊的物件。
傳回
一個等同於參數中 value 指定數字的值。
實作
例外狀況
value 為 null。
value 格式不正確。
範例
以下範例展示了兩種定義波浪號(~)作為格式 BigInteger 化值負號的方法。 請注意,若要以原始字串相同的格式顯示 BigInteger 數值,程式碼必須呼叫該 BigInteger.ToString(IFormatProvider) 方法並傳遞 NumberFormatInfo 提供格式資訊的物件。
第一個範例定義了一個類別,該類別實作 IFormatProvider 並使用該 GetFormat 方法回傳 NumberFormatInfo 提供格式資訊的物件。
public class BigIntegerFormatProvider : IFormatProvider
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(NumberFormatInfo))
{
NumberFormatInfo numberFormat = new NumberFormatInfo();
numberFormat.NegativeSign = "~";
return numberFormat;
}
else
{
return null;
}
}
}
type BigIntegerFormatProvider() =
interface IFormatProvider with
member _.GetFormat(formatType: Type) =
if formatType = typeof<NumberFormatInfo> then
let numberFormat = NumberFormatInfo()
numberFormat.NegativeSign <- "~"
numberFormat
else
null
Public Class BigIntegerFormatProvider : Implements IFormatProvider
Public Function GetFormat(formatType As Type) As Object _
Implements IFormatProvider.GetFormat
If formatType Is GetType(NumberFormatInfo) Then
Dim numberFormat As New NumberFormatInfo
numberFormat.NegativeSign = "~"
Return numberFormat
Else
Return Nothing
End If
End Function
End Class
BigInteger物件接著可以用以下程式碼實例化:
BigInteger number = BigInteger.Parse("~6354129876", new BigIntegerFormatProvider());
// Display value using same formatting information
Console.WriteLine(number.ToString(new BigIntegerFormatProvider()));
// Display value using formatting of current culture
Console.WriteLine(number);
let number = BigInteger.Parse("~6354129876", BigIntegerFormatProvider())
printfn $"{number.ToString(BigIntegerFormatProvider())}"
printfn $"{number}"
Dim number As BigInteger = BigInteger.Parse("~6354129876", New BigIntegerFormatProvider)
' Display value using same formatting information
Console.WriteLine(number.ToString(New BigIntegerFormatProvider))
' Display value using formatting of current culture
Console.WriteLine(number)
第二個例子比較直接。 它會將提供格式資訊的物件傳遞NumberFormatInfo給參數。provider
NumberFormatInfo fmt = new NumberFormatInfo();
fmt.NegativeSign = "~";
BigInteger number = BigInteger.Parse("~6354129876", fmt);
// Display value using same formatting information
Console.WriteLine(number.ToString(fmt));
// Display value using formatting of current culture
Console.WriteLine(number);
let fmt = NumberFormatInfo()
fmt.NegativeSign <- "~"
let number = BigInteger.Parse("~6354129876", fmt)
printfn $"{number.ToString fmt}"
printfn $"{number}"
Dim fmt As New NumberFormatInfo()
fmt.NegativeSign = "~"
Dim number As BigInteger = BigInteger.Parse("~6354129876", fmt)
' Display value using same formatting information
Console.WriteLine(number.ToString(fmt))
' Display value using formatting of current culture
Console.WriteLine(number)
備註
value參數應為以下形式的數字的字串表示:
[ws][標誌]數字[WS]
方括號內的元素([ 和 ])為可選。 下表說明每個元素。
| 元素 | Description |
|---|---|
| WS | 可選留白。 |
| 簽署 | 一個可選的標誌。 有效的符號字元由NumberFormatInfo.NegativeSign物件的方法回傳providerGetFormat的 和 NumberFormatInfo.PositiveSign 屬性NumberFormatInfo決定。 |
| 數字 | 一串從0到9的數字序列。 任何前置零都被忽略。 |
備註
參數 value 指定的字串會使用 style NumberStyles.Integer 來解讀。 它不能包含任何群分隔符或十進位分隔符,也不能有小數部分。
這很重要
如果你用這個Parse方法來循環輸出的字ToString串表示BigInteger,你應該用BigInteger.ToString(String)帶有「R」格式規範子的方法來產生該BigInteger值的字串表示。 否則,字 BigInteger 串表示只會保留原始值的最高 50 位位,使用 Parse 還原 BigInteger 方法時可能會遺失資料。
參數 provider 是一個 IFormatProvider 實作,其 GetFormat 方法回傳 NumberFormatInfo 一個物件,提供特定文化的格式資訊。 當 Parse(String, IFormatProvider) 方法被呼叫時,它會呼叫 provider 參數的方法 GetFormat ,並傳入 Type 一個代表該型別的 NumberFormatInfo 物件。 接著,該 GetFormat 方法會回傳 NumberFormatInfo 提供參數格式 value 資訊的物件。 有三種方式可以利用該 provider 參數為解析操作提供自訂格式資訊:
你可以傳遞 CultureInfo 一個代表文化的物件,提供格式資訊。 其 GetFormat 方法回傳 NumberFormatInfo 提供該文化數值格式資訊的物件。
你可以傳遞提供數字格式資訊的實體 NumberFormatInfo 物件。 (它的實作 GetFormat 只是回傳自己。)
你可以傳遞一個自訂物件來實作 IFormatProvider。 其 GetFormat 方法實例化並提供格式資訊的 NumberFormatInfo 物件。
若 provider ,則 的value格式是根據NumberFormatInfo當前文化的null物件來解釋。
另請參閱
適用於
Parse(ReadOnlySpan<Byte>, IFormatProvider)
public:
static System::Numerics::BigInteger Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<System::Numerics::BigInteger>::Parse;
public static System.Numerics.BigInteger Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> System.Numerics.BigInteger
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As BigInteger
參數
- utf8Text
- ReadOnlySpan<Byte>
- provider
- IFormatProvider
傳回
實作
適用於
Parse(ReadOnlySpan<Char>, IFormatProvider)
將一組字元解析成一個數值。
public:
static System::Numerics::BigInteger Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<System::Numerics::BigInteger>::Parse;
public static System.Numerics.BigInteger Parse(ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> System.Numerics.BigInteger
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As BigInteger
參數
- s
- ReadOnlySpan<Char>
要解析的字元範圍。
- provider
- IFormatProvider
一個提供關於 s的文化特定格式資訊的物件。
傳回
解析 的結果 s。
實作
適用於
Parse(String, NumberStyles)
將特定風格中的數字字串表示轉換為其 BigInteger 等價形式。
public:
static System::Numerics::BigInteger Parse(System::String ^ value, System::Globalization::NumberStyles style);
public static System.Numerics.BigInteger Parse(string value, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> System.Numerics.BigInteger
Public Shared Function Parse (value As String, style As NumberStyles) As BigInteger
參數
- value
- String
一個包含一個需要轉換的數字的字串。
- style
- NumberStyles
一個位元組合的列舉值,指定允許的格式 value。
傳回
一個等同於參數中 value 指定數字的值。
例外狀況
value 為 null。
value 不符合由 NumberStyles所指定的輸入模式。
範例
以下範例說明了對該 Parse(String, NumberStyles) 方法的呼叫,並可能包含 style 參數的數值。 它說明如何將字串解釋為十六進位值,以及如何禁止空格和符號符號。
BigInteger number;
// Method should succeed (white space and sign allowed)
number = BigInteger.Parse(" -68054 ", NumberStyles.Integer);
Console.WriteLine(number);
// Method should succeed (string interpreted as hexadecimal)
number = BigInteger.Parse("68054", NumberStyles.AllowHexSpecifier);
Console.WriteLine(number);
// Method call should fail: sign not allowed
try
{
number = BigInteger.Parse(" -68054 ", NumberStyles.AllowLeadingWhite
| NumberStyles.AllowTrailingWhite);
Console.WriteLine(number);
}
catch (FormatException e)
{
Console.WriteLine(e.Message);
}
// Method call should fail: white space not allowed
try
{
number = BigInteger.Parse(" 68054 ", NumberStyles.AllowLeadingSign);
Console.WriteLine(number);
}
catch (FormatException e)
{
Console.WriteLine(e.Message);
}
//
// The method produces the following output:
//
// -68054
// 426068
// Input string was not in a correct format.
// Input string was not in a correct format.
let number = BigInteger.Parse(" -68054 ", NumberStyles.Integer)
printfn $"{number}"
let number = BigInteger.Parse("68054", NumberStyles.AllowHexSpecifier)
printfn $"{number}"
try
let number =
BigInteger.Parse(" -68054 ", NumberStyles.AllowLeadingWhite ||| NumberStyles.AllowTrailingWhite)
printfn $"{number}"
with :? FormatException as e ->
printfn $"{e.Message}"
try
let number = BigInteger.Parse(" 68054 ", NumberStyles.AllowLeadingSign)
printfn $"{number}"
with :? FormatException as e ->
printfn $"{e.Message}"
// The method produces the following output:
// -68054
// 426068
// Input string was not in a correct format.
// Input string was not in a correct format.
Dim number As BigInteger
' Method should succeed (white space and sign allowed)
number = BigInteger.Parse(" -68054 ", NumberStyles.Integer)
Console.WriteLine(number)
' Method should succeed (string interpreted as hexadecimal)
number = BigInteger.Parse("68054", NumberStyles.AllowHexSpecifier)
Console.WriteLine(number)
' Method call should fail: sign not allowed
Try
number = BigInteger.Parse(" -68054 ", NumberStyles.AllowLeadingWhite _
Or NumberStyles.AllowTrailingWhite)
Console.WriteLine(number)
Catch e As FormatException
Console.WriteLine(e.Message)
End Try
' Method call should fail: white space not allowed
Try
number = BigInteger.Parse(" 68054 ", NumberStyles.AllowLeadingSign)
Console.WriteLine(number)
Catch e As FormatException
Console.WriteLine(e.Message)
End Try
'
' The method produces the following output:
'
' -68054
' 426068
' Input string was not in a correct format.
' Input string was not in a correct format.
備註
參數 style 定義了允許在解析操作成功時所允許 value 的樣式元素(例如空白、正負符號、群組分隔符號或小數點符號)。
styles 必須是列舉中多個位元旗 NumberStyles 標的組合。
style當 value 包含十六進位值的字串表示、所表示value的數字系統(十進位或十六進位)僅在執行時已知,或你想在 中排除空白或符號value符號時,這個參數使此方法過載變得有用。
根據 的 style值, value 參數可能包含以下元素:
[ws][$][符號][數字,]數字[。fractional_digits][E[sign]exponential_digits][ws]
若 style 包含 NumberStyles.AllowHexSpecifier,該 value 參數可能包含以下元素:
[ws]六角數數字[ws]
方括號內的元素([ 和 ])為可選。 下表說明每個元素。
| 元素 | Description |
|---|---|
| WS | 可選留白。 如果包含旗幟,空白可以出現在 的value開頭,如果style包含 旗NumberStyles.AllowTrailingWhite幟,則可以出現在結尾value。NumberStyles.AllowLeadingWhitestyle |
| $ | 一種文化特定的貨幣符號。 它在字串中的位置由NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern當前文化的性質所定義。 當前style文化的貨幣符號可以出現value在包含NumberStyles.AllowCurrencySymbol國旗的情況下。 |
| 簽署 | 一個可選的標誌。 如果包含旗幟,標誌可以出現在 的value開頭,如果包含NumberStyles.AllowTrailingSign該旗幟,則可以出現在 的結尾stylevalue。NumberStyles.AllowLeadingSignstyle 若style包含旗標,NumberStyles.AllowParentheses則可使用value括號表示負值。 |
|
數字 fractional_digits exponential_digits |
一串從0到9的數字序列。 對於 fractional_digits,只有數字0有效。 |
| , | 一個文化特定的群體分隔符號。 當前文化的群組分隔符可以出現value在包含styleNumberStyles.AllowThousands旗幟時。 |
| . | 一種文化特定的小數點符號。 當前文化的小數點符號可以出現valuestyle在包含NumberStyles.AllowDecimalPoint國旗的情況下。 只有數字 0 可以以小數數字出現,才能成功進行解析操作;若 fractional_digits 包含其他數字,則擲出 A FormatException 。 |
| E | 「e」或「E」字元表示該數值以指數(科學)符號表示。 若style包含NumberStyles.AllowExponent旗標,參數value可用指數符號表示數字。 |
| 六角位數 | 一串從0到f,或0到F的十六進位數字序列。 |
備註
任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。
只有數字的字串(對應 NumberStyles.None 樣式)總是能成功解析。 大多數剩餘 NumberStyles 成員控制輸入字串中可能存在但不必須存在的元素。 下表顯示個別 NumberStyles 成員如何影響可能存在於 value中的元素。
NumberStyles 值 |
除了數字外,允許加入 value 的元素 |
|---|---|
| None | 只有 數字 元素。 |
| AllowDecimalPoint | 小數點(.)和 小數數字 元素。 |
| AllowExponent | 「e」或「E」字元表示指數符號,並附有 exponential_digits。 |
| AllowLeadingWhite | 該元素位於 的開頭value為 ws 元素。 |
| AllowTrailingWhite | 末尾value的 ws 元素。 |
| AllowLeadingSign | 符號 元素位於 的開頭 value。 |
| AllowTrailingSign | 符號 元素位於 的結尾 value。 |
| AllowParentheses | 符號 元素以 括號形式包含數值。 |
| AllowThousands | 群分離子(,)元素。 |
| AllowCurrencySymbol | 貨幣($)元素。 |
| Currency | 所有元素。 然而,無法 value 表示十六進位數或指數符號中的數字。 |
| Float | 起始或結尾value的 w元素,符號開頭value,以及小數點(.)符號。 參數 value 也可以使用指數符號。 |
| Number | 、 wssign、 群組分隔符 (,) 以及小數點 (.) 元素。 |
| Any | 所有元素。 然而,無法 value 代表十六進位數。 |
這很重要
如果你用這個Parse方法來循環輸出的字ToString串表示BigInteger,你應該用BigInteger.ToString(String)帶有「R」格式規範子的方法來產生該BigInteger值的字串表示。 否則,字 BigInteger 串表示只會保留原始值的最高 50 位位,使用 Parse 還原 BigInteger 方法時可能會遺失資料。
與其他NumberStyles允許但不要求存在特定樣式元素valueNumberStyles.AllowHexSpecifier的值不同,樣式值意味著 中的value個別數字字元總是被解釋為十六進位字元。 有效的十六進位字元為 0-9、A-F 和 a-f。 唯一可以與參數 style 結合的其他旗標是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite。 (列 NumberStyles 舉包含一個複合數字樣式, HexNumber包含兩個空白標記。)
備註
若 value 是十六進位數的字串表示,則其前不能加上任何裝飾(如 0x 或 &h)來區分其為十六進位數。 這會導致轉換失敗。
若 value 是十六進位字串,Parse(String, NumberStyles)若前兩個十六進位數字大於或等0x80於 ,則該方法將其value解釋為以二補數表示儲存的負數。 換句話說,該方法將第一個 value 位元組的最高階位元解釋為符號位元。 為了確保十六進位字串被正確解讀為正數,該數字的 value 第一個數字必須為零。 例如,該方法將 0x80 or 解讀為負值,但同時將 0x080 或 0x0080 解讀為正值。 以下範例說明了代表負值與正值的十六進位字串之間的差異。
using System;
using System.Globalization;
using System.Numerics;
public class Example
{
public static void Main()
{
string[] hexStrings = { "80", "E293", "F9A2FF", "FFFFFFFF",
"080", "0E293", "0F9A2FF", "0FFFFFFFF",
"0080", "00E293", "00F9A2FF", "00FFFFFFFF" };
foreach (string hexString in hexStrings)
{
BigInteger number = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier);
Console.WriteLine("Converted 0x{0} to {1}.", hexString, number);
}
}
}
// The example displays the following output:
// Converted 0x80 to -128.
// Converted 0xE293 to -7533.
// Converted 0xF9A2FF to -417025.
// Converted 0xFFFFFFFF to -1.
// Converted 0x080 to 128.
// Converted 0x0E293 to 58003.
// Converted 0x0F9A2FF to 16360191.
// Converted 0x0FFFFFFFF to 4294967295.
// Converted 0x0080 to 128.
// Converted 0x00E293 to 58003.
// Converted 0x00F9A2FF to 16360191.
// Converted 0x00FFFFFFFF to 4294967295.
open System.Globalization
open System.Numerics
let hexStrings =
[| "80"
"E293"
"F9A2FF"
"FFFFFFFF"
"080"
"0E293"
"0F9A2FF"
"0FFFFFFFF"
"0080"
"00E293"
"00F9A2FF"
"00FFFFFFFF" |]
for hexString in hexStrings do
let number = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier)
printfn $"Converted 0x{hexString} to {number:N0}."
// The example displays the following output:
// Converted 0x80 to -128.
// Converted 0xE293 to -7533.
// Converted 0xF9A2FF to -417025.
// Converted 0xFFFFFFFF to -1.
// Converted 0x080 to 128.
// Converted 0x0E293 to 58003.
// Converted 0x0F9A2FF to 16360191.
// Converted 0x0FFFFFFFF to 4294967295.
// Converted 0x0080 to 128.
// Converted 0x00E293 to 58003.
// Converted 0x00F9A2FF to 16360191.
// Converted 0x00FFFFFFFF to 4294967295.
Imports System.Globalization
Imports System.Numerics
Module Example
Public Sub Main()
Dim hexStrings() As String = { "80", "E293", "F9A2FF", "FFFFFFFF",
"080", "0E293", "0F9A2FF", "0FFFFFFFF",
"0080", "00E293", "00F9A2FF", "00FFFFFFFF" }
For Each hexString As String In hexStrings
Dim number As BigInteger = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier)
Console.WriteLine("Converted 0x{0} to {1}.", hexString, number)
Next
End Sub
End Module
' The example displays the following output:
' Converted 0x80 to -128.
' Converted 0xE293 to -7533.
' Converted 0xF9A2FF to -417025.
' Converted 0xFFFFFFFF to -1.
' Converted 0x080 to 128.
' Converted 0x0E293 to 58003.
' Converted 0x0F9A2FF to 16360191.
' Converted 0x0FFFFFFFF to 4294967295.
' Converted 0x0080 to 128.
' Converted 0x00E293 to 58003.
' Converted 0x00F9A2FF to 16360191.
' Converted 0x00FFFFFFFF to 4294967295.
value參數是利用物件格式資訊解析,該物件已NumberFormatInfo初始化為當前系統文化。 若要指定用於解析操作的格式資訊文化,請呼叫 overload Parse(String, NumberStyles, IFormatProvider) 。
另請參閱
適用於
Parse(String)
將數字的字串表示轉換為其 BigInteger 等價物。
public:
static System::Numerics::BigInteger Parse(System::String ^ value);
public static System.Numerics.BigInteger Parse(string value);
static member Parse : string -> System.Numerics.BigInteger
Public Shared Function Parse (value As String) As BigInteger
參數
- value
- String
一個包含要轉換數字的字串。
傳回
一個等同於參數中 value 指定數字的值。
例外狀況
value 為 null。
value 格式不正確。
範例
以下範例使用此 Parse(String) 方法來實例化兩個 BigInteger 物件。 它會將每個物件乘以另一個數字,然後呼叫該 Compare 方法來判斷兩個值之間的關係。
string stringToParse = String.Empty;
try
{
// Parse two strings.
string string1, string2;
string1 = "12347534159895123";
string2 = "987654321357159852";
stringToParse = string1;
BigInteger number1 = BigInteger.Parse(stringToParse);
Console.WriteLine("Converted '{0}' to {1:N0}.", stringToParse, number1);
stringToParse = string2;
BigInteger number2 = BigInteger.Parse(stringToParse);
Console.WriteLine("Converted '{0}' to {1:N0}.", stringToParse, number2);
// Perform arithmetic operations on the two numbers.
number1 *= 3;
number2 *= 2;
// Compare the numbers.
int result = BigInteger.Compare(number1, number2);
switch (result)
{
case -1:
Console.WriteLine("{0} is greater than {1}.", number2, number1);
break;
case 0:
Console.WriteLine("{0} is equal to {1}.", number1, number2);
break;
case 1:
Console.WriteLine("{0} is greater than {1}.", number1, number2);
break;
}
}
catch (FormatException)
{
Console.WriteLine("Unable to parse {0}.", stringToParse);
}
// The example displays the following output:
// Converted '12347534159895123' to 12,347,534,159,895,123.
// Converted '987654321357159852' to 987,654,321,357,159,852.
// 1975308642714319704 is greater than 37042602479685369.
let mutable stringToParse = ""
try
// Parse two strings.
let string1 = "12347534159895123"
let string2 = "987654321357159852"
stringToParse <- string1
let number1 = BigInteger.Parse stringToParse
printfn $"Converted '{stringToParse}' to {number1:N0}."
stringToParse <- string2
let number2 = BigInteger.Parse stringToParse
printfn $"Converted '{stringToParse}' to {number2:N0}."
// Perform arithmetic operations on the two numbers.
let number1 = number1 * bigint 3
let number2 = number2 * bigint 2
// Compare the numbers.
let result = BigInteger.Compare(number1, number2)
match result with
| -1 -> printfn $"{number2:N0} is greater than {number1:N0}."
| 0 -> printfn $"{number1:N0} is equal to {number2:N0}."
| 1
| _ -> printfn $"{number1:N0} is greater than {number2:N0}."
with :? FormatException ->
printfn $"Unable to parse {stringToParse}."
// The example displays the following output:
// Converted '12347534159895123' to 12,347,534,159,895,123.
// Converted '987654321357159852' to 987,654,321,357,159,852.
// 1975308642714319704 is greater than 37042602479685369.
Dim stringToParse As String = String.Empty
Try
' Parse two strings.
Dim string1, string2 As String
string1 = "12347534159895123"
string2 = "987654321357159852"
stringToParse = string1
Dim number1 As BigInteger = BigInteger.Parse(stringToParse)
Console.WriteLine("Converted '{0}' to {1:N0}.", stringToParse, number1)
stringToParse = string2
Dim number2 As BigInteger = BigInteger.Parse(stringToParse)
Console.WriteLine("Converted '{0}' to {1:N0}.", stringToParse, number2)
' Perform arithmetic operations on the two numbers.
number1 *= 3
number2 *= 2
' Compare the numbers.
Select Case BigInteger.Compare(number1, number2)
Case -1
Console.WriteLine("{0} is greater than {1}.", number2, number1)
Case 0
Console.WriteLine("{0} is equal to {1}.", number1, number2)
Case 1
Console.WriteLine("{0} is greater than {1}.", number1, number2)
End Select
Catch e As FormatException
Console.WriteLine("Unable to parse {0}.", stringToParse)
End Try
' The example displays the following output:
' Converted '12347534159895123' to 12,347,534,159,895,123.
' Converted '987654321357159852' to 987,654,321,357,159,852.
' 1975308642714319704 is greater than 37042602479685369.
備註
value參數應為以下形式的數字的字串表示。
[ws][標誌]數字[WS]
方括號內的元素([ 和 ])為可選。 下表說明每個元素。
| 元素 | Description |
|---|---|
| WS | 可選留白。 |
| 簽署 | 一個可選的標誌。 有效的符號特徵是由 NumberFormatInfo.NegativeSign 當前文化的屬性 NumberFormatInfo.PositiveSign 決定的。 |
| 數字 | 一串從0到9的數字序列。 任何前置零都被忽略。 |
備註
參數指定的 value 字串會透過 NumberStyles.Integer 樣式來解釋。 它不能包含任何群分隔符或十進位分隔符,也不能有小數部分。
value參數是利用物件格式資訊解析,該物件已System.Globalization.NumberFormatInfo初始化為當前系統文化。 如需詳細資訊,請參閱NumberFormatInfo.CurrentInfo。 要利用特定文化的格式資訊解析字串,請使用該 Parse(String, IFormatProvider) 方法。
這很重要
如果你用這個Parse方法來循環輸出的字ToString串表示BigInteger,你應該用BigInteger.ToString(String)帶有「R」格式規範子的方法來產生該BigInteger值的字串表示。 否則,字 BigInteger 串表示只會保留原始值的最高 50 位位,使用 Parse 還原 BigInteger 方法時可能會遺失資料。