共用方式為


BigInteger.TryParse 方法

定義

嘗試將數字的字串表示轉換為其 BigInteger 等價值,並回傳一個表示轉換是否成功的值。

多載

名稱 Description
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, BigInteger)

嘗試將數字的字串表示轉換為其 BigInteger 等價值,並回傳一個表示轉換是否成功的值。

TryParse(ReadOnlySpan<Byte>, BigInteger)
TryParse(ReadOnlySpan<Char>, BigInteger)

嘗試將指定唯讀字元區間內的數字表示轉換為其 BigInteger 等價值,並回傳一個表示轉換是否成功的值。

TryParse(String, BigInteger)

嘗試將數字的字串表示轉換為其 BigInteger 等價值,並回傳一個表示轉換是否成功的值。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, BigInteger)
TryParse(String, IFormatProvider, BigInteger)

嘗試將字串解析成一個值。

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, BigInteger)
TryParse(String, NumberStyles, IFormatProvider, BigInteger)

嘗試將特定風格及文化特定格式的數字字串表示轉換為其 BigInteger 對應格式,並回傳一個表示轉換是否成功的值。

TryParse(ReadOnlySpan<Char>, IFormatProvider, BigInteger)

嘗試將一串字元解析成一個數值。

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, BigInteger)

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

嘗試將數字的字串表示轉換為其 BigInteger 等價值,並回傳一個表示轉換是否成功的值。

public:
 static bool TryParse(ReadOnlySpan<char> value, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result) = System::Numerics::INumberBase<System::Numerics::BigInteger>::TryParse;
public:
 static bool TryParse(ReadOnlySpan<char> value, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result);
public static bool TryParse(ReadOnlySpan<char> value, System.Globalization.NumberStyles style, IFormatProvider? provider, out System.Numerics.BigInteger result);
public static bool TryParse(ReadOnlySpan<char> value, System.Globalization.NumberStyles style, IFormatProvider provider, out System.Numerics.BigInteger result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * BigInteger -> bool
Public Shared Function TryParse (value As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As BigInteger) As Boolean

參數

value
ReadOnlySpan<Char>

將數字表示為唯讀字元範圍。

style
NumberStyles

一個位元組合的列舉值,表示可能存在 value於 中的樣式元素。 典型的指定值為 Integer

provider
IFormatProvider

一個提供關於 value的文化特定格式資訊的物件。

result
BigInteger

當此方法回傳時,包含 BigInteger 的數值等同於所 value包含的數值,或 Zero 若轉換失敗。 若 value 參數為空字元區段或格式不符合 style,轉換即失敗。 此參數以未初始化的方式傳遞。

傳回

truevalue成功轉換;否則,。 false

例外狀況

style 不是一個 NumberStyles 數值。

-或-

style 包含 AllowHexSpecifier OR HexNumber 旗標及另一個值。

備註

此過載類似於方法, Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) 但轉換失敗時不會拋出例外。 此方法省去了使用例外處理來測試 FormatExceptionvalue 是否無效且無法成功解析的必要。

參數 style 定義了允許 value 在解析操作中成功時的樣式元素(如空白或正負符號)。 它必須是列舉中多個位元旗 NumberStyles 標的組合。 根據 的 style值, value 參數可能包含以下元素:

[ws][$][符號][數字,]數字[。fractional_digits][E[sign]exponential_digits][ws]

若參數 style 包含 AllowHexSpecifier,則 value 參數可能包含以下元素:

[ws]六角數數字[ws]

方括號內的元素([ 和 ])為可選。 下表說明每個元素。

元素 Description
WS 可選留白。 如果包含旗幟,空白可以出現在 的value開頭,或在 NumberStyles.AllowTrailingWhitestyle 包含旗幟時出現在結尾valueNumberStyles.AllowLeadingWhitestyle
$ 一種文化特定的貨幣符號。 其在 中的value位置由CurrencyPositivePattern參數方法provider回傳的GetFormat物件性質NumberFormatInfo所定義。 貨幣style符號可出現value在包含NumberStyles.AllowCurrencySymbol旗幟時。
簽署 一個可選的標誌。 如果包含旗幟,標誌可以出現在 的value開頭,如果包含NumberStyles.AllowTrailingSign該旗幟,則可以出現在 的結尾stylevalueNumberStyles.AllowLeadingSignstylestyle包含旗標,NumberStyles.AllowParentheses則可使用value括號表示負值。
數字 一串從0到9的數字序列。
, 一個文化特定的群體分離器。 providerstyle包含 旗NumberStyles.AllowThousands幟,則可出現在 中value
. 一種文化特定的小數點符號。 provider若包含NumberStyles.AllowDecimalPoint旗幟,則可出現在 中valuestyle
fractional_digits 數字 0 出現一次或多次。 只有當 style 包含該旗幟時NumberStyles.AllowDecimalPoint,小數數字才能出現value在 中。
E 「e」或「E」字元表示該數值以指數(科學)符號表示。 若style包含NumberStyles.AllowExponent旗標,參數value可用指數符號表示數字。
exponential_digits 一串從0到9的數字序列。 若style包含NumberStyles.AllowExponent旗標,參數value可用指數符號表示數字。
六角位數 一串從0到f,或0到F的十六進位數字序列。

備註

任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。

非綜合 NumberStyles 除了數字外,允許加入數值的元素
None 只用小數數字。
AllowDecimalPoint 小數點(.)和 fractional_digits 元素。 然而, fractional_digits 必須只包含一個或多個 0 位數,否則方法會返回 false
AllowExponent 「e」或「E」字元表示指數符號,並附有 exponential_digits。 若 value 以指數符號表示一個數字,則不可能有非零的分數分量。
AllowLeadingWhite 該元素位於 的開頭valuews 元素。
AllowTrailingWhite 末尾valuews 元素。
AllowLeadingSign 數字之前是符號元素。
AllowTrailingSign 數字後面符號元素。
AllowParentheses 符號 元素以 括號形式包含數值。
AllowThousands 群分離子()元素。
AllowCurrencySymbol 貨幣($)元素。
Currency 所有元素。 然而,無法 value 表示十六進位數或指數符號中的數字。
Float 起始或結尾valuew元素,符號開頭value,以及小數點(.)符號。 參數 value 也可以使用指數符號。
Number 包括 ws符號、群分隔符(,,)和小數點(.)元素。
Any 所有元素。 然而,無法 value 代表十六進位數。

NumberStyles.AllowHexSpecifier 使用旗標, value 則必須為十六進位值。 唯一可能存在於 中的 style 其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (該 NumberStyles 枚舉具有複合風格, HexNumber包含兩個空白標記。)

參數 provider 是一個 IFormatProvider 實作。 其 GetFormat 方法回傳一個 NumberFormatInfo 物件,提供關於 格式 value的文化特定資訊。 參數 provider 可以是以下任一:

provider為 ,nullNumberFormatInfo則使用 當前文化的物件。

另請參閱

適用於

TryParse(ReadOnlySpan<Byte>, BigInteger)

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result);
public static bool TryParse(ReadOnlySpan<byte> utf8Text, out System.Numerics.BigInteger result);
static member TryParse : ReadOnlySpan<byte> * BigInteger -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As BigInteger) As Boolean

參數

utf8Text
ReadOnlySpan<Byte>
result
BigInteger

傳回

適用於

TryParse(ReadOnlySpan<Char>, BigInteger)

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

嘗試將指定唯讀字元區間內的數字表示轉換為其 BigInteger 等價值,並回傳一個表示轉換是否成功的值。

public:
 static bool TryParse(ReadOnlySpan<char> value, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result);
public static bool TryParse(ReadOnlySpan<char> value, out System.Numerics.BigInteger result);
static member TryParse : ReadOnlySpan<char> * BigInteger -> bool
Public Shared Function TryParse (value As ReadOnlySpan(Of Char), ByRef result As BigInteger) As Boolean

參數

value
ReadOnlySpan<Char>

將數字表示為唯讀字元範圍。

result
BigInteger

當此方法回傳時,包含 BigInteger 的數值等同於 中 value所包含的數值,若轉換失敗則為零(0)。 若 value 參數字元區間為空或格式不正確,轉換會失敗。 此參數以未初始化的方式傳遞。

傳回

truevalue成功轉換;否則,。 false

另請參閱

適用於

TryParse(String, BigInteger)

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

嘗試將數字的字串表示轉換為其 BigInteger 等價值,並回傳一個表示轉換是否成功的值。

public:
 static bool TryParse(System::String ^ value, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result);
public static bool TryParse(string value, out System.Numerics.BigInteger result);
public static bool TryParse(string? value, out System.Numerics.BigInteger result);
static member TryParse : string * BigInteger -> bool
Public Shared Function TryParse (value As String, ByRef result As BigInteger) As Boolean

參數

value
String

數字的字串表示。

result
BigInteger

當此方法回傳時,包含 BigInteger 的數值等同於 中 value所包含的數值,若轉換失敗則為零(0)。 若 value 參數 null 格式正確或不正確,轉換即告失敗。 此參數以未初始化的方式傳遞。

傳回

truevalue成功轉換;否則,。 false

例外狀況

valuenull

範例

以下範例使用此 TryParse(String, BigInteger) 方法來實例化兩個 BigInteger 物件。 如果轉換成功,會將每個物件乘以另一個數字,然後呼叫該 Compare 方法來判斷兩個物件之間的關係。

BigInteger number1, number2;
bool succeeded1 = BigInteger.TryParse("-12347534159895123", out number1);
bool succeeded2 = BigInteger.TryParse("987654321357159852", out number2);
if (succeeded1 && succeeded2)
{
   number1 *= 3;
   number2 *= 2;
   switch (BigInteger.Compare(number1, number2))
   {
      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;
   }
}
else
{
   if (!succeeded1)
      Console.WriteLine("Unable to initialize the first BigInteger value.");

   if (!succeeded2)
      Console.WriteLine("Unable to initialize the second BigInteger value.");
}
// The example displays the following output:
//      1975308642714319704 is greater than -37042602479685369.
    let mutable number1 = BigInteger.Zero
    let mutable number2 = BigInteger.Zero

    let succeeded1 = BigInteger.TryParse("-12347534159895123", &number1)
    let succeeded2 = BigInteger.TryParse("987654321357159852", &number2)

    if succeeded1 && succeeded2 then
        number1 <- number1 * 3I
        number2 <- number2 * 2I

        match BigInteger.Compare(number1, number2) with
        | -1 -> printfn $"{number2} is greater than {number2}."
        | 0 -> printfn $"{number1} is equal to {number2}."
        | 1
        | _ -> printfn $"{number1} is greater than {number2}."
    else
        if not succeeded1 then
            printfn "Unable to initialize the first BigInteger value."

        if not succeeded2 then
            printfn "Unable to initialize the second BigInteger value."

// The example displays the following output:
//      1975308642714319704 is greater than -37042602479685369.
Dim number1 As BigInteger = BigInteger.Zero
Dim number2 As BigInteger = BigInteger.Zero
Dim succeeded1 As Boolean = BigInteger.TryParse("-12347534159895123", number1)
Dim succeeded2 As Boolean = BigInteger.TryParse("987654321357159852", number2)
If succeeded1 AndAlso succeeded2
   number1 *= 3
   number2 *= 2
   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      
Else
   If Not succeeded1 Then 
      Console.WriteLine("Unable to initialize the first BigInteger value.")
   End If
   If Not succeeded2 Then
      Console.WriteLine("Unable to initialize the second BigInteger value.")
   
   End If
End If
' The example displays the following output:
'      1975308642714319704 is greater than -37042602479685369.

備註

TryParse(String, BigInteger) 方法與方法 Parse(String) 相同,但若轉換失敗不會拋出例外。 此方法省去了使用例外處理來測試 FormatExceptionvalue 是否無效且無法成功解析的必要。

value參數應為以下十進位數的字串表示:

[ws][標誌]數字[WS]

方括號內的元素([ 和 ])為可選。 下表說明每個元素。

元素 Description
WS 可選留白。
簽署 一個可選的標誌。 有效的符號特徵是由 NumberFormatInfo.NegativeSign 當前文化的屬性 NumberFormatInfo.PositiveSign 決定的。
數字 一串從0到9的十進位數字序列。

備註

參數 value 指定的字串不能包含任何群分隔符或十進位分隔符,且不可有小數部分。

value參數是透過 樣NumberStyles.Integer式來解釋的。 除了十進位數字外,僅允許有前置和後置的空格帶有前導符號。 若要明確定義風格元素及文化 value特定的格式資訊,請呼叫該 TryParse(String, NumberStyles, IFormatProvider, BigInteger) 方法。

value參數是透過物件中的格式資訊NumberFormatInfo解析當前文化的。 如需詳細資訊,請參閱NumberFormatInfo.CurrentInfo

此過載將參數中 value 的所有數字解讀為十進位數字。 要解析十六進位數的字串表示,請呼叫 overload TryParse(String, NumberStyles, IFormatProvider, BigInteger)

另請參閱

適用於

TryParse(ReadOnlySpan<Byte>, IFormatProvider, BigInteger)

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result) = IUtf8SpanParsable<System::Numerics::BigInteger>::TryParse;
public static bool TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out System.Numerics.BigInteger result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * BigInteger -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As BigInteger) As Boolean

參數

utf8Text
ReadOnlySpan<Byte>
provider
IFormatProvider
result
BigInteger

傳回

適用於

TryParse(String, IFormatProvider, BigInteger)

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

嘗試將字串解析成一個值。

public:
 static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result) = IParsable<System::Numerics::BigInteger>::TryParse;
public static bool TryParse(string? s, IFormatProvider? provider, out System.Numerics.BigInteger result);
static member TryParse : string * IFormatProvider * BigInteger -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As BigInteger) As Boolean

參數

s
String

要解析的字串。

provider
IFormatProvider

一個提供關於 s的文化特定格式資訊的物件。

result
BigInteger

當此方法回傳時,包含成功解析 s 的結果,或失敗時為未定義的值。

傳回

trues 成功解析;否則, false

適用於

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, BigInteger)

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result) = System::Numerics::INumberBase<System::Numerics::BigInteger>::TryParse;
public static bool TryParse(ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out System.Numerics.BigInteger result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * BigInteger -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As BigInteger) As Boolean

參數

utf8Text
ReadOnlySpan<Byte>
style
NumberStyles
provider
IFormatProvider
result
BigInteger

傳回

適用於

TryParse(String, NumberStyles, IFormatProvider, BigInteger)

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

嘗試將特定風格及文化特定格式的數字字串表示轉換為其 BigInteger 對應格式,並回傳一個表示轉換是否成功的值。

public:
 static bool TryParse(System::String ^ value, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result);
public:
 static bool TryParse(System::String ^ value, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result) = System::Numerics::INumberBase<System::Numerics::BigInteger>::TryParse;
public static bool TryParse(string value, System.Globalization.NumberStyles style, IFormatProvider provider, out System.Numerics.BigInteger result);
public static bool TryParse(string? value, System.Globalization.NumberStyles style, IFormatProvider? provider, out System.Numerics.BigInteger result);
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * BigInteger -> bool
Public Shared Function TryParse (value As String, style As NumberStyles, provider As IFormatProvider, ByRef result As BigInteger) As Boolean

參數

value
String

數字的字串表示。 字串的解譯方式由 所指定的 style樣式。

style
NumberStyles

一個位元組合的列舉值,表示可能存在 value於 中的樣式元素。 典型的指定值為 Integer

provider
IFormatProvider

一個提供關於 value的文化特定格式資訊的物件。

result
BigInteger

當此方法回傳時,包含 BigInteger 的數值等同於所 value包含的數值,或 Zero 若轉換失敗。 若value參數nullstyle格式不符合 ,轉換即失敗。 此參數以未初始化的方式傳遞。

傳回

truevalue 參數成功轉換;否則, false

例外狀況

style 不是一個 NumberStyles 數值。

-或-

style 包含 AllowHexSpecifier OR HexNumber 旗標及另一個值。

範例

以下範例使用不同參數值styleprovider組合呼叫該TryParse(String, NumberStyles, IFormatProvider, BigInteger)方法。

string numericString;
BigInteger number = BigInteger.Zero;

// Call TryParse with default values of style and provider.
numericString = "  -300   ";
if (BigInteger.TryParse(numericString, NumberStyles.Integer,
                       null, out number))
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number);
else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString);

// Call TryParse with the default value of style and
// a provider supporting the tilde as negative sign.
numericString = "  -300   ";
if (BigInteger.TryParse(numericString, NumberStyles.Integer,
                       new BigIntegerFormatProvider(), out number))
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number);
else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString);

// Call TryParse with only AllowLeadingWhite and AllowTrailingWhite.
// Method returns false because of presence of negative sign.
numericString = "  -500   ";
if (BigInteger.TryParse(numericString,
                        NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite,
                        new BigIntegerFormatProvider(), out number))
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number);
else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString);

// Call TryParse with AllowHexSpecifier and a hex value.
numericString = "F14237FFAAC086455192";
if (BigInteger.TryParse(numericString,
                        NumberStyles.AllowHexSpecifier,
                        null, out number))
   Console.WriteLine("'{0}' was converted to {1} (0x{1:x}).",
                     numericString, number);
else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString);

// Call TryParse with AllowHexSpecifier and a negative hex value.
// Conversion fails because of presence of negative sign.
numericString = "-3af";
if (BigInteger.TryParse(numericString, NumberStyles.AllowHexSpecifier,
                       new BigIntegerFormatProvider(), out number))
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number);
else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString);

// Call TryParse with only NumberStyles.None.
// Conversion fails because of presence of white space and sign.
numericString = " -300 ";
if (BigInteger.TryParse(numericString, NumberStyles.None,
                       new BigIntegerFormatProvider(), out number))
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number);
else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString);

// Call TryParse with NumberStyles.Any and a provider for the fr-FR culture.
// Conversion fails because the string is formatted for the en-US culture.
numericString = "9,031,425,666,123,546.00";
if (BigInteger.TryParse(numericString, NumberStyles.Any,
                       new CultureInfo("fr-FR"), out number))
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number);
else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString);

// Call TryParse with NumberStyles.Any and a provider for the fr-FR culture.
// Conversion succeeds because the string is properly formatted
// For the fr-FR culture.
numericString = "9 031 425 666 123 546,00";
if (BigInteger.TryParse(numericString, NumberStyles.Any,
                       new CultureInfo("fr-FR"), out number))
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number);
else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString);
// The example displays the following output:
//    '  -300   ' was converted to -300.
//    Conversion of '  -300   ' to a BigInteger failed.
//    Conversion of '  -500   ' to a BigInteger failed.
//    'F14237FFAAC086455192' was converted to -69613977002644837412462 (0xf14237ffaac086455192).
//    Conversion of '-3af' to a BigInteger failed.
//    Conversion of ' -300 ' to a BigInteger failed.
//    Conversion of '9,031,425,666,123,546.00' to a BigInteger failed.
//    '9 031 425 666 123 546,00' was converted to 9031425666123546.
    // Call TryParse with default values of style and provider.
    let numericString = "  -300   "

    match BigInteger.TryParse(numericString, NumberStyles.Integer, null) with
    | true, number -> printfn $"The string '{numericString}' parses to {number}"
    | _ -> printfn $"Conversion of {numericString} to a BigInteger failed."

    // Call TryParse with the default value of style and
    // a provider supporting the tilde as negative sign.
    let numericString = "  -300   "

    match BigInteger.TryParse(numericString, NumberStyles.Integer, new BigIntegerFormatProvider()) with
    | true, number -> printfn $"The string '{numericString}' parses to {number}"
    | _ -> printfn $"Conversion of {numericString} to a BigInteger failed."

    // Call TryParse with only AllowLeadingWhite and AllowTrailingWhite.
    // Method returns false because of presence of negative sign.
    let numericString = "  -500   "

    match
        BigInteger.TryParse(
            numericString,
            NumberStyles.AllowLeadingWhite ||| NumberStyles.AllowTrailingWhite,
            new BigIntegerFormatProvider()
        )
    with
    | true, number -> printfn $"The string '{numericString}' parses to {number}"
    | _ -> printfn $"Conversion of {numericString} to a BigInteger failed."

    // Call TryParse with AllowHexSpecifier and a hex value.
    let numericString = "F14237FFAAC086455192"

    match BigInteger.TryParse(numericString, NumberStyles.AllowHexSpecifier, null) with
    | true, number -> printfn $"The string '{numericString}' parses to {number}, or 0x{number:x}."
    | _ -> printfn $"Conversion of {numericString} to a BigInteger failed."

    // Call TryParse with AllowHexSpecifier and a negative hex value.
    // Conversion fails because of presence of negative sign.
    let numericString = "-3af"

    match BigInteger.TryParse(numericString, NumberStyles.AllowHexSpecifier, null) with
    | true, number -> printfn $"The string '{numericString}' parses to {number}, or 0x{number:x}."
    | _ -> printfn $"Conversion of {numericString} to a BigInteger failed."

    // Call TryParse with only NumberStyles.None.
    // Conversion fails because of presence of white space and sign.
    let numericString = " -300 "

    match BigInteger.TryParse(numericString, NumberStyles.None, new BigIntegerFormatProvider()) with
    | true, number -> printfn $"The string '{numericString}' parses to {number}"
    | _ -> printfn $"Conversion of {numericString} to a BigInteger failed."

    // Call TryParse with NumberStyles.Any and a provider for the fr-FR culture.
    // Conversion fails because the string is formatted for the en-US culture.
    let numericString = "9,031,425,666,123,546.00"

    match BigInteger.TryParse(numericString, NumberStyles.Any, new CultureInfo("fr-FR")) with
    | true, number -> printfn $"The string '{numericString}' parses to {number}"
    | _ -> printfn $"Conversion of {numericString} to a BigInteger failed."

    // Call TryParse with NumberStyles.Any and a provider for the fr-FR culture.
    // Conversion succeeds because the string is properly formatted
    // For the fr-FR culture.
    let numericString = "9 031 425 666 123 546,00"

    match BigInteger.TryParse(numericString, NumberStyles.Any, new CultureInfo("fr-FR")) with
    | true, number -> printfn $"The string '{numericString}' parses to {number}"
    | _ -> printfn $"Conversion of {numericString} to a BigInteger failed."

// The example displays the following output:
//    '  -300   ' was converted to -300.
//    Conversion of '  -300   ' to a BigInteger failed.
//    Conversion of '  -500   ' to a BigInteger failed.
//    'F14237FFAAC086455192' was converted to -69613977002644837412462 (0xf14237ffaac086455192).
//    Conversion of '-3af' to a BigInteger failed.
//    Conversion of ' -300 ' to a BigInteger failed.
//    Conversion of '9,031,425,666,123,546.00' to a BigInteger failed.
//    '9 031 425 666 123 546,00' was converted to 9031425666123546.
Dim numericString As String
Dim number As BigInteger = BigInteger.Zero

' Call TryParse with default values of style and provider.
numericString = "  -300   "
If BigInteger.TryParse(numericString, NumberStyles.Integer,
                       Nothing, number) Then
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number)                             
Else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString)
End If                                             

' Call TryParse with the default value of style and 
' a provider supporting the tilde as negative sign.
numericString = "  -300   "
If BigInteger.TryParse(numericString, NumberStyles.Integer,
                       New BigIntegerFormatProvider(), number) Then
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number)                             
Else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString)
End If                                             

' Call TryParse with only AllowLeadingWhite and AllowTrailingWhite.
' Method returns false because of presence of negative sign.
numericString = "  -500   "
If BigInteger.TryParse(numericString,
                    NumberStyles.AllowLeadingWhite Or NumberStyles.AllowTrailingWhite,
                    New BigIntegerFormatProvider(), number) Then
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number)                             
Else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString)
End If                                             

' Call TryParse with AllowHexSpecifier and a hex value.
numericString = "F14237FFAAC086455192"
If BigInteger.TryParse(numericString,
                    NumberStyles.AllowHexSpecifier,
                    Nothing, number) Then
   Console.WriteLine("'{0}' was converted to {1} (0x{1:x}).",
                     numericString, number)                             
Else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString)
End If                                             

' Call TryParse with AllowHexSpecifier and a negative hex value.
' Conversion fails because of presence of negative sign.
numericString = "-3af"
If BigInteger.TryParse(numericString, NumberStyles.AllowHexSpecifier,
                       New BigIntegerFormatProvider(), number) Then
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number)                             
Else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString)
End If                                             

' Call TryParse with only NumberStyles.None.
' Conversion fails because of presence of white space and sign.
numericString = " -300 "
If BigInteger.TryParse(numericString, NumberStyles.None,
                       New BigIntegerFormatProvider(), number) Then
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number)                             
Else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString)
End If 
                                            
' Call TryParse with NumberStyles.Any and a provider for the fr-FR culture.
' Conversion fails because the string is formatted for the en-US culture.
numericString = "9,031,425,666,123,546.00"
If BigInteger.TryParse(numericString, NumberStyles.Any,
                       New CultureInfo("fr-FR"), number) Then
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number)                             
Else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString)
End If

' Call TryParse with NumberStyles.Any and a provider for the fr-FR culture.
' Conversion succeeds because the string is properly formatted 
' For the fr-FR culture.
numericString = "9 031 425 666 123 546,00"
If BigInteger.TryParse(numericString, NumberStyles.Any,
                       New CultureInfo("fr-FR"), number) Then
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number)                             
Else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString)
End If
' The example displays the following output:
'    '  -300   ' was converted to -300.
'    Conversion of '  -300   ' to a BigInteger failed.
'    Conversion of '  -500   ' to a BigInteger failed.
'    'F14237FFAAC086455192' was converted to -69613977002644837412462 (0xf14237ffaac086455192).
'    Conversion of '-3af' to a BigInteger failed.
'    Conversion of ' -300 ' to a BigInteger failed.
'    Conversion of '9,031,425,666,123,546.00' to a BigInteger failed.
'    '9 031 425 666 123 546,00' was converted to 9031425666123546.

對該 TryParse(String, NumberStyles, IFormatProvider, BigInteger) 方法的多個個別呼叫會經過下一個 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 = new 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

備註

TryParse(String, NumberStyles, IFormatProvider, BigInteger) 方法與方法 Parse(String, NumberStyles, IFormatProvider) 相同,但若轉換失敗不會拋出例外。 此方法省去了使用例外處理來測試 FormatExceptionvalue 是否無效且無法成功解析的必要。

參數 style 定義了允許 value 在解析操作中成功時的樣式元素(如空白或正負符號)。 它必須是列舉中多個位元旗 NumberStyles 標的組合。 根據 的 style值, value 參數可能包含以下元素:

[ws][$][符號][數字,]數字[。fractional_digits][E[sign]exponential_digits][ws]

若參數 style 包含 AllowHexSpecifier,則 value 參數可能包含以下元素:

[ws]六角數數字[ws]

方括號內的元素([ 和 ])為可選。 下表說明每個元素。

元素 Description
WS 可選留白。 如果包含旗幟,空白可以出現在 的value開頭,或在 NumberStyles.AllowTrailingWhitestyle 包含旗幟時出現在結尾valueNumberStyles.AllowLeadingWhitestyle
$ 一種文化特定的貨幣符號。 它在字串中的位置由CurrencyPositivePattern參數方法provider回傳的GetFormat物件性質NumberFormatInfo所定義。 貨幣style符號可出現value在包含NumberStyles.AllowCurrencySymbol旗幟時。
簽署 一個可選的標誌。 如果包含旗幟,標誌可以出現在 的value開頭,如果包含NumberStyles.AllowTrailingSign該旗幟,則可以出現在 的結尾stylevalueNumberStyles.AllowLeadingSignstylestyle包含旗標,NumberStyles.AllowParentheses則可使用value括號表示負值。
數字 一串從0到9的數字序列。
, 一個文化特定的群體分離器。 providerstyle包含 旗NumberStyles.AllowThousands幟,則可出現在 中value
. 一種文化特定的小數點符號。 provider若包含NumberStyles.AllowDecimalPoint旗幟,則可出現在 中valuestyle
fractional_digits 數字 0 出現一次或多次。 只有當 style 包含該旗幟時NumberStyles.AllowDecimalPoint,小數數字才能出現value在 中。
E 「e」或「E」字元表示該數值以指數(科學)符號表示。 若style包含NumberStyles.AllowExponent旗標,參數value可用指數符號表示數字。
exponential_digits 一串從0到9的數字序列。 若style包含NumberStyles.AllowExponent旗標,參數value可用指數符號表示數字。
六角位數 一串從0到f,或0到F的十六進位數字序列。

備註

任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。

僅有十進位數字的字串(對應 NumberStyles.None 於旗標)總是能成功解析。 其餘 NumberStyles 大多數成員控制此輸入字串中可能存在但不必須存在的元素。 下表顯示個別 NumberStyles 成員如何影響可能存在於 value中的元素。

非綜合 NumberStyles 除了數字外,允許加入數值的元素
None 只用小數數字。
AllowDecimalPoint 小數點(.)和 fractional_digits 元素。 然而, fractional_digits 必須只包含一個或多個 0 位數,否則方法會返回 false
AllowExponent 「e」或「E」字元表示指數符號,並附有 exponential_digits。 若 value 以指數符號表示一個數字,則不可能有非零的分數分量。
AllowLeadingWhite 該元素位於 的開頭valuews 元素。
AllowTrailingWhite 末尾valuews 元素。
AllowLeadingSign 數字之前是符號元素。
AllowTrailingSign 數字後面符號元素。
AllowParentheses 符號 元素以 括號形式包含數值。
AllowThousands 群分離子()元素。
AllowCurrencySymbol 貨幣($)元素。
Currency 所有元素。 然而,無法 value 表示十六進位數或指數符號中的數字。
Float 起始或結尾valuew元素,符號開頭value,以及小數點(.)符號。 參數 value 也可以使用指數符號。
Number 包括 ws符號、群分隔符(,,)和小數點(.)元素。
Any 所有元素。 然而,無法 value 代表十六進位數。

這很重要

如果你用這個TryParse方法來循環輸出的字ToString串表示BigInteger,你應該用BigInteger.ToString(String)帶有「R」格式規範子的方法來產生該BigInteger值的字串表示。 否則,字 BigInteger 串表示只會保留原始值的最高 50 位位,使用 TryParse 還原 BigInteger 方法時可能會遺失資料。

NumberStyles.AllowHexSpecifier 使用旗標, value 則必須為十六進位值。 唯一可能存在於 中的 style 其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (該 NumberStyles 枚舉具有複合風格, HexNumber包含兩個空白標記。)

備註

value 是十六進位數的字串表示,則其前不能加上任何裝飾(如 0x&h)來區分其為十六進位數。 這會導致轉換失敗。

value 是十六進位字串,TryParse(String, NumberStyles, IFormatProvider, BigInteger)若前兩個十六進位數字大於或等0x80於 ,則該方法將其value解釋為以二補數表示儲存的負數。 換句話說,該方法將第一個 value 位元組的最高階位元解釋為符號位元。 為了確保十六進位字串被正確解讀為正數,該數字的 value 第一個數字必須為零。 例如,該方法將 0x80 or 解讀為負值,但同時將 0x0800x0080 解讀為正值。 以下範例說明了代表負值與正值的十六進位字串之間的差異。

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" };
      BigInteger number = BigInteger.Zero;

      foreach (string hexString in hexStrings)
      {
         if (BigInteger.TryParse(hexString, NumberStyles.AllowHexSpecifier,
                                 null, out number))
            Console.WriteLine("Converted 0x{0} to {1}.", hexString, number);
         else
            Console.WriteLine("Cannot convert '{0}' to a BigInteger.", hexString);
      }
   }
}
// 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
    match BigInteger.TryParse(hexString, NumberStyles.AllowHexSpecifier, null) with
    | true, number -> printfn $"Converted 0x{hexString} to {number}."
    | _ -> printfn $"Cannot convert '{hexString}' to a BigInteger."

// 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" }
      Dim number As BigInteger = BigInteger.Zero
      
      For Each hexString As String In hexStrings
         If BigInteger.TryParse(hexString, NumberStyles.AllowHexSpecifier, 
                                Nothing, number) Then
            Console.WriteLine("Converted 0x{0} to {1}.", hexString, number)
         Else
            Console.WriteLine("Cannot convert '{0}' to a BigInteger.", hexString)
         End If
      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 可以是以下任一:

provider為 ,nullNumberFormatInfo則使用 當前文化的物件。

另請參閱

適用於

TryParse(ReadOnlySpan<Char>, IFormatProvider, BigInteger)

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

嘗試將一串字元解析成一個數值。

public:
 static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result) = ISpanParsable<System::Numerics::BigInteger>::TryParse;
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out System.Numerics.BigInteger result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * BigInteger -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As BigInteger) As Boolean

參數

s
ReadOnlySpan<Char>

要解析的字元範圍。

provider
IFormatProvider

一個提供關於 s的文化特定格式資訊的物件。

result
BigInteger

當此方法回傳時,包含成功解析 s的結果,或失敗時為未定義的值。

傳回

trues 成功解析;否則, false

適用於