BigInteger.TryParse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
嘗試將數字的字串表示轉換成其相等的 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,並傳回一個值表示轉換是否成功。
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
。
例外狀況
備註
這個多載就像 Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) 方法一樣,不同之處在于,如果轉換失敗,它不會擲回例外狀況。 這個方法不需要使用例外狀況處理來測試 FormatException 是否 value
無效,而且無法成功剖析。
參數 style
會定義樣式專案 (,例如空白字元或正負號) ,參數中 value
允許剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。 根據 的值 style
, value
參數可能包含下列元素:
[ws][ $ ][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]
style
如果參數包含 AllowHexSpecifier ,則 value
參數可能包含下列元素:
[ws]hexdigits[ws]
在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。
元素 | 描述 |
---|---|
ws | 選擇性空白字元。 如果 包含 旗標,則會在 開頭 value 出現空白字元,如果包含 NumberStyles.AllowTrailingWhite 旗標,則會出現在 結尾 style value 。 NumberStyles.AllowLeadingWhitestyle |
$ | 特定文化特性的貨幣符號。 中的位置 value 是由 CurrencyPositivePattern 參數方法 provider 所 GetFormat 傳回之 物件的 屬性 NumberFormatInfo 所定義。 如果 style 包含 旗標, NumberStyles.AllowCurrencySymbol 則可以在 中 value 顯示貨幣符號。 |
簽署 | 選擇性符號。 如果 包含 旗標,則符號可以出現在 value 開頭,如果包含 NumberStyles.AllowTrailingSign 旗標,則會出現在 結尾 style value 。 NumberStyles.AllowLeadingSignstyle 如果包含 NumberStyles.AllowParentheses 旗標,則可以在 中使用 value 括弧來表示負值 style 。 |
數字 | 從 0 到 9 的數位序列。 |
, | 特定文化特性的群組分隔符號。 如果包含 旗標,則 所 provider 指定文化特性的群組分隔符號可能會出現在 中 value 。 NumberStyles.AllowThousandsstyle |
. | 特定文化特性的小數點符號。 如果包含 旗標,則 所指定 provider 文化特性的小數點符號可能會出現在 中 value 。 NumberStyles.AllowDecimalPointstyle |
fractional_digits | 數位 0 的一或多個出現次數。 只有包含 NumberStyles.AllowDecimalPoint 旗標時 style ,小數位數才會出現在 中 value 。 |
E | 「e」 或 「E」 字元,表示該值是以指數 (科學) 標記法表示。 如果 style 包含 NumberStyles.AllowExponent 旗標,參數 value 可以代表指數標記法的數位。 |
exponential_digits | 從 0 到 9 的數位序列。 如果 style 包含 NumberStyles.AllowExponent 旗標,參數 value 可以代表指數標記法的數位。 |
hexdigits | 從 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 | 開頭的 value ws元素。 |
AllowTrailingWhite | 結尾處的 value ws專案。 |
AllowLeadingSign | 數位之前的符號元素。 |
AllowTrailingSign | 數位後面的符號專案。 |
AllowParentheses | 以括弧括住數值形式的 sign 元素。 |
AllowThousands | 群組分隔符號 (,) 專案。 |
AllowCurrencySymbol | 貨幣 ($) 專案。 |
Currency | 所有元素。 不過, value 不能以指數標記法表示十六進位數位或數位。 |
Float | 開頭或結尾的 value ws元素,在 開頭 value 為 符號,而小數點 (。) 符號。 參數 value 也可以使用指數標記法。 |
Number | ws、sign、group separator (、) 和小數點 (。) 元素。 |
Any | 所有元素。 不過, value 不能代表十六進位數位。 |
NumberStyles.AllowHexSpecifier如果使用 旗標, value
必須是十六進位值。 唯一可以存在的 style
旗標是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite 。 NumberStyles (列舉具有複合樣式 , HexNumber 其中包含空白字元旗標.)
參數 provider
是實作 IFormatProvider 。 其 GetFormat 方法會傳 NumberFormatInfo 回 物件,提供 有關 格式 value
的文化特性特定資訊。 參數 provider
可以是下列任一項:
CultureInfo物件,表示提供格式資訊的文化特性。 其 GetFormat 方法會傳 NumberFormatInfo 回 物件,該物件會提供該文化特性的數值格式資訊。
物件 NumberFormatInfo ,提供數值格式資訊。 (其 實作 GetFormat 只會傳回本身。)
實作 的 IFormatProvider 自訂物件。 其 GetFormat 方法會具現化並傳回 NumberFormatInfo 提供格式化資訊的物件。
如果 為 provider
null
,則會 NumberFormatInfo 使用目前文化特性的物件。
另請參閱
適用於
TryParse(String, IFormatProvider, BigInteger)
嘗試將字串剖析成值。
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)
嘗試將字元範圍剖析為值。
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,並傳回一個值表示轉換是否成功。
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
。
例外狀況
value
為 null
。
範例
下列範例會 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 對等項目,並傳回指出轉換是否成功的值。
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,並傳回值,這個值表示轉換是否成功。
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
。
例外狀況
範例
下列範例會使用 和 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 位旗標組合。 根據 的值 style
, value
參數可能包含下列元素:
[ws][ $ ][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]
style
如果參數包含 AllowHexSpecifier ,則 value
參數可能包含下列元素:
[ws]hexdigits[ws]
在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。
元素 | 描述 |
---|---|
ws | 選擇性空白字元。 如果 包含 旗標,則會在 開頭 value 出現空白字元,如果包含 NumberStyles.AllowTrailingWhite 旗標,則會出現在 結尾 style value 。 NumberStyles.AllowLeadingWhitestyle |
$ | 特定文化特性的貨幣符號。 字串中的位置是由 CurrencyPositivePattern 參數的 方法 provider 所 GetFormat 傳回之 物件的 屬性 NumberFormatInfo 所定義。 如果 style 包含 旗標, NumberStyles.AllowCurrencySymbol 則可以在 中 value 顯示貨幣符號。 |
簽署 | 選擇性符號。 如果 包含 旗標,則符號可以出現在 value 開頭,如果包含 NumberStyles.AllowTrailingSign 旗標,則會出現在 結尾 style value 。 NumberStyles.AllowLeadingSignstyle 如果包含 NumberStyles.AllowParentheses 旗標,則可以在 中使用 value 括弧來表示負值 style 。 |
數字 | 從 0 到 9 的數位序列。 |
, | 特定文化特性的群組分隔符號。 如果包含 旗標,則 所 provider 指定文化特性的群組分隔符號可能會出現在 中 value 。 NumberStyles.AllowThousandsstyle |
. | 特定文化特性的小數點符號。 如果包含 旗標,則 所指定 provider 文化特性的小數點符號可能會出現在 中 value 。 NumberStyles.AllowDecimalPointstyle |
fractional_digits | 數位 0 的一或多個出現次數。 只有包含 NumberStyles.AllowDecimalPoint 旗標時 style ,小數位數才會出現在 中 value 。 |
E | 「e」 或 「E」 字元,表示該值是以指數 (科學) 標記法表示。 如果 style 包含 NumberStyles.AllowExponent 旗標,參數 value 可以代表指數標記法的數位。 |
exponential_digits | 從 0 到 9 的數位序列。 如果 style 包含 NumberStyles.AllowExponent 旗標,參數 value 可以代表指數標記法的數位。 |
hexdigits | 從 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 | 開頭的 value ws元素。 |
AllowTrailingWhite | 結尾處的 value ws專案。 |
AllowLeadingSign | 數位之前的符號元素。 |
AllowTrailingSign | 數位後面的符號專案。 |
AllowParentheses | 以括弧括住數值形式的 sign 元素。 |
AllowThousands | 群組分隔符號 (,) 專案。 |
AllowCurrencySymbol | 貨幣 ($) 專案。 |
Currency | 所有元素。 不過, value 不能以指數標記法表示十六進位數位或數位。 |
Float | 開頭或結尾的 value ws元素,在 開頭 value 為 符號,而小數點 (。) 符號。 參數 value 也可以使用指數標記法。 |
Number | ws、sign、group separator (、) 和小數點 (。) 元素。 |
Any | 所有元素。 不過, value 不能代表十六進位數位。 |
重要
如果您使用 TryParse 方法來回傳回方法所輸出 ToString 值的字串表示 BigInteger 法,您應該使用 BigInteger.ToString(String) 方法搭配 「R」 格式規範來產生值的字串表示 BigInteger 。 否則,的 BigInteger 字串表示只會保留原始值的 50 個最大有效位數,而且當您使用 TryParse 方法來還原 BigInteger 值時,可能會遺失資料。
NumberStyles.AllowHexSpecifier如果使用 旗標, value
必須是十六進位值。 唯一可以存在的 style
旗標是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite 。 NumberStyles (列舉具有複合樣式 , HexNumber 其中包含空白字元旗標.)
注意
如果 value
是十六進位數位的字串標記法,則不能在前面加上任何裝飾 (,例如 0x
或 &h
) 將它區分為十六進位數位。 這會導致轉換失敗。
如果 value
是十六進位字串, TryParse(String, NumberStyles, IFormatProvider, BigInteger) 則方法會在前兩個十六進位數位大於或等於 0x80
時,使用兩個補數標記法解譯 value
為儲存的負數。 換句話說,方法會將 中 value
第一個位元組的最高順序位解譯為符號位。 為了確保十六進位字串正確解譯為正數,中的 value
第一個數位必須有零的值。 例如,方法會解譯 0x80
為負值,但會將 或 0x0080
解譯 0x080
為正值。 下列範例說明代表負值和正值的十六進位字串之間的差異。
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
可以是下列任一項:
CultureInfo物件,表示提供格式資訊的文化特性。 其 GetFormat 方法會傳 NumberFormatInfo 回 物件,該物件會提供該文化特性的數值格式資訊。
物件 NumberFormatInfo ,提供數值格式資訊。 (其 實作 GetFormat 只會傳回本身。)
實作 的 IFormatProvider 自訂物件。 其 GetFormat 方法會具現化並傳回 NumberFormatInfo 提供格式化資訊的物件。
如果 為 provider
null
,則會 NumberFormatInfo 使用目前文化特性的物件。
另請參閱
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應