BigInteger.TryParse 方法

定義

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

多載

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

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

TryParse(String, IFormatProvider, BigInteger)

嘗試將字串剖析成值。

TryParse(ReadOnlySpan<Char>, IFormatProvider, BigInteger)

嘗試將字元範圍剖析成值。

TryParse(String, BigInteger)

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

TryParse(ReadOnlySpan<Char>, BigInteger)

嘗試將所指定唯讀字元範圍中包含的數字表示法轉換為其 BigInteger 對等項目,並傳回指出轉換是否成功的值。

TryParse(String, NumberStyles, IFormatProvider, BigInteger)

使用指定的文化特性特定格式資訊和格式樣式,將日期和時間的指定字串表示轉換為其對等的 BigInteger,並傳回值,這個值表示轉換是否成功。

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

來源:
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);
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, 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

當這個方法傳回時,如果轉換成功,則會包含相當於 value 中所含之數字的 BigInteger;如果轉換失敗則為 Zero。 若 value 參數是空字元範圍或不是符合 style 的格式,則轉換會失敗。 這個參數會以未初始化的狀態傳遞。

傳回

如果 value 轉換成功,則為 true,否則為 false

例外狀況

style 不是 NumberStyles 值。

-或-

style 包含 AllowHexSpecifierHexNumber 旗標和其他值。

備註

這個多載就像 Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) 方法一樣,不同之處在於它不會在轉換失敗時擲回例外狀況。 這個方法不需要使用例外狀況處理來測試 FormatException 是否 value 無效,且無法成功剖析。

參數 style 會定義樣式專案 (,例如空格符或正負號) ,在參數中 value 允許剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。 根據 的值 stylevalue 參數可能包含下列元素:

[ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]

style如果 參數包含 AllowHexSpecifier,參數value可能包含下列元素:

[ws]hexdigits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。

元素 描述
ws 選擇性空格符。 如果style包含旗標,NumberStyles.AllowLeadingWhite則空格符可以出現在 開頭value,如果包含旗標,NumberStyles.AllowTrailingWhite則會出現style在 結尾value處。
$ 特定文化特性的貨幣符號。 中的位置value是由 CurrencyPositivePattern 參數的 方法provider所傳回之 對象的 屬性NumberFormatInfoGetFormat定義。 如果style包含旗標,NumberStyles.AllowCurrencySymbol貨幣符號就可以出現在 中value
簽署 選擇性符號。 如果 style 包含旗標,NumberStyles.AllowLeadingSign則符號可以出現在 開頭value,如果style包含 NumberStyles.AllowTrailingSign 旗標,它可能會出現在 結尾value。 如果style包含 NumberStyles.AllowParentheses 旗標,可以使用 value 括號來表示負值。
數字 從 0 到 9 的數位序列。
, 特定文化特性的群組分隔符。 如果包含旗標,則 指定provider文化特性的群組分隔符可以出現在 中valueNumberStyles.AllowThousandsstyle
. 特定文化特性的小數點符號。 如果包含旗標,則 所provider指定文化特性的小數點符號可以出現在 中valueNumberStyles.AllowDecimalPointstyle
fractional_digits 數位 0 的一或多個出現次數。 只有包含 NumberStyles.AllowDecimalPoint 旗標時style,小數位數才會出現在 中value
E “e” 或 “E” 字元,表示值是以指數 (科學) 表示法表示。 如果style包含旗標,參數value可以表示指數表示法的數位NumberStyles.AllowExponent
exponential_digits 從 0 到 9 的數位序列。 如果style包含旗標,參數value可以表示指數表示法的數位NumberStyles.AllowExponent
hexdigits 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

不論自變數的值style為何,剖析作業都會忽略 中s任何終止的 NUL (U+0000) 字元。

非複合 NumberStyles 除了數位之外,值中允許的專案
None 僅限十進位數。
AllowDecimalPoint 小數點 () 和 fractional_digits 元素。 不過, fractional_digits 必須只包含一或多個 0 位數,否則方法會傳 false回 。
AllowExponent “e” 或 “E” 字元,表示指數表示法,以及 exponential_digits。 如果 value 表示指數表示法中的數位,它不能有非零的小數部分。
AllowLeadingWhite 開頭的 valuews 元素。
AllowTrailingWhite 結尾處的 valuews 元素。
AllowLeadingSign 數位之前的符號專案。
AllowTrailingSign 數字後面的符號專案。
AllowParentheses 以括弧括住數值形式的 sign 元素。
AllowThousands 群組分隔符 () 元素。
AllowCurrencySymbol 貨幣 ($) 專案。
Currency 所有元素。 不過, value 不能代表十六進位數或指數表示法中的數位。
Float 開頭或結尾的 valuews 元素,會在 開頭加上符號value,小數點 () 符號。 參數 value 也可以使用指數表示法。
Number wssign、group 分隔符 () 和小數點 () 元素。
Any 所有元素。 不過, value 不能代表十六進位數。

NumberStyles.AllowHexSpecifier如果使用旗標,value則必須是十六進位值。 唯一可以出現在 中的 style 其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (列舉 NumberStyles 具有復合樣式, HexNumber其中包含空格符旗標.)

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

如果 providernull,則會 NumberFormatInfo 使用目前文化特性的物件。

另請參閱

適用於

TryParse(String, IFormatProvider, BigInteger)

來源:
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 或失敗時未定義值的結果。

傳回

true 如果 s 已成功剖析,則為 ,否則為 false

適用於

TryParse(ReadOnlySpan<Char>, IFormatProvider, BigInteger)

來源:
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的結果,或失敗時未定義的值。

傳回

true 如果 s 已成功剖析,則為 ,否則為 false

適用於

TryParse(String, BigInteger)

來源:
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

當這個方法傳回時,如果轉換成功,則會包含相當於 value 中所含之數字的 BigInteger;如果轉換失敗則為零 (0)。 轉換失敗的狀況包括:如果 value 參數為 null 或不是正確的格式。 這個參數會以未初始化的狀態傳遞。

傳回

如果 value 轉換成功,則為 true,否則為 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.
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) 方法一樣,不同之處在於,如果轉換失敗,它不會擲回例外狀況。 這個方法不需要使用例外狀況處理來測試 FormatException 是否 value 無效,而且無法成功剖析。

參數 value 應該是以下列形式表示十進位數的字串:

[ws][sign]digits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。

元素 描述
ws 選擇性空格符。
簽署 選擇性符號。 有效的符號字元取決於 NumberFormatInfo.NegativeSign 目前文化特性的 和 NumberFormatInfo.PositiveSign 屬性。
數字 小數位數序列,範圍從0到9。

注意

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

參數 value 是使用 NumberStyles.Integer 樣式來解譯。 除了十進位數之外,只允許前置和尾端有前置符號的空格。 若要使用可以存在於 value中的文化特性特定格式資訊明確定義樣式專案,請呼叫 TryParse(String, NumberStyles, IFormatProvider, BigInteger) 方法。

參數 value 是使用物件中 NumberFormatInfo 目前文化特性的格式資訊進行剖析。 如需詳細資訊,請參閱NumberFormatInfo.CurrentInfo

這個多載會將 參數中的所有 value 數位解譯為十進位數。 若要剖析十六進位數位的字串表示法,請改為呼叫 TryParse(String, NumberStyles, IFormatProvider, BigInteger) 多載。

另請參閱

適用於

TryParse(ReadOnlySpan<Char>, BigInteger)

來源:
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

當這個方法傳回時,如果轉換成功,則會包含相當於 value 中所含之數字的 BigInteger;如果轉換失敗則為零 (0)。 若 value 參數是空字元範圍或不是正確的格式,則轉換會失敗。 這個參數會以未初始化的狀態傳遞。

傳回

如果 value 轉換成功,則為 true,否則為 false

另請參閱

適用於

TryParse(String, NumberStyles, IFormatProvider, BigInteger)

來源:
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

當這個方法傳回時,如果轉換成功,則會包含相當於 value 中所含之數字的 BigInteger;如果轉換失敗則為 Zero。 轉換失敗的狀況包括:如果 value 參數為 null 或格式不符合 style。 這個參數會以未初始化的狀態傳遞。

傳回

如果 true 參數轉換成功,則為 value,否則為 false

例外狀況

style 不是 NumberStyles 值。

-或-

style 包含 AllowHexSpecifierHexNumber 旗標和其他值。

範例

下列範例會使用 和 provider 參數的各種值style組合,對方法進行一些呼叫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.
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;
      }
   }
}
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) 方法一樣,不同之處在於,如果轉換失敗,它不會擲回例外狀況。 這個方法不需要使用例外狀況處理來測試 FormatException 是否 value 無效,且無法成功剖析。

參數 style 會定義樣式專案 (,例如空格符或正負號) ,在參數中 value 允許剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。 根據 的值 stylevalue 參數可能包含下列元素:

[ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]

style如果 參數包含 AllowHexSpecifier,參數value可能包含下列元素:

[ws]hexdigits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。

元素 描述
ws 選擇性空格符。 如果style包含旗標,NumberStyles.AllowLeadingWhite則空格符可以出現在 開頭value,如果包含旗標,NumberStyles.AllowTrailingWhite則會出現style在 結尾value處。
$ 特定文化特性的貨幣符號。 字串中的位置是由 CurrencyPositivePattern 參數的 方法provider所傳回之 對象的 屬性NumberFormatInfoGetFormat定義。 如果style包含旗標,NumberStyles.AllowCurrencySymbol貨幣符號就可以出現在 中value
簽署 選擇性符號。 如果 style 包含旗標,NumberStyles.AllowLeadingSign則符號可以出現在 開頭value,如果style包含 NumberStyles.AllowTrailingSign 旗標,它可能會出現在 結尾value。 如果style包含 NumberStyles.AllowParentheses 旗標,可以使用 value 括號來表示負值。
數字 從 0 到 9 的數位序列。
, 特定文化特性的群組分隔符。 如果包含旗標,則 指定provider文化特性的群組分隔符可以出現在 中valueNumberStyles.AllowThousandsstyle
. 特定文化特性的小數點符號。 如果包含旗標,則 所provider指定文化特性的小數點符號可以出現在 中valueNumberStyles.AllowDecimalPointstyle
fractional_digits 數位 0 的一或多個出現次數。 只有包含 NumberStyles.AllowDecimalPoint 旗標時style,小數位數才會出現在 中value
E “e” 或 “E” 字元,表示值是以指數 (科學) 表示法表示。 如果style包含旗標,參數value可以表示指數表示法的數位NumberStyles.AllowExponent
exponential_digits 從 0 到 9 的數位序列。 如果style包含旗標,參數value可以表示指數表示法的數位NumberStyles.AllowExponent
hexdigits 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

不論自變數的值style為何,剖析作業都會忽略 中s任何終止的 NUL (U+0000) 字元。

只有十進位數的字串 (對應至 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 以括弧括住數值形式的 sign 元素。
AllowThousands 群組分隔符 () 元素。
AllowCurrencySymbol 貨幣 ($) 專案。
Currency 所有元素。 不過, value 不能代表十六進位數或指數表示法中的數位。
Float 開頭或結尾的 valuews 元素,會在 開頭加上符號value,小數點 () 符號。 參數 value 也可以使用指數表示法。
Number wssign、group 分隔符 () 和小數點 () 元素。
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 解譯為負值,但它會將 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.
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 可以是下列任一項:

如果 providernull,則會 NumberFormatInfo 使用目前文化特性的物件。

另請參閱

適用於