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)
- Source:
- BigInteger.cs
- Source:
- BigInteger.cs
- Source:
- BigInteger.cs
- Source:
- 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
当此方法返回时,包含与 value 中所包含的数字等效的 BigInteger;如果转换失败,则包含 Zero。 如果 value 参数为空字符范围或格式不符合 style,则转换失败。 此参数未经初始化即被传递。
返回
如果 true 成功转换,则为 value;否则为 false。
例外
注解
此重载类似于 Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) 方法,不同之处在于,如果转换失败,它不会引发异常。 此方法无需使用异常处理来测试 FormatExceptionvalue 是否无效且无法成功分析。
参数 style 定义样式元素 (,如空格或正或负号) ,这些元素在 参数中 value 允许分析操作成功。 它必须是 枚举中的位标志 NumberStyles 的组合。 根据 的值 style, value 参数可能包括以下元素:
[ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]
style如果 参数包括 AllowHexSpecifier,则value参数可以包含以下元素:
[ws]hexdigits[ws]
方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。
| 元素 | 说明 |
|---|---|
| ws | 可选空格。 如果包含 标志,则开头value会出现空格;如果stylestyle包含 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite 标志,value则结尾处会显示空格。 |
| $ | 区域性特定的货币符号。 它在 中value的位置由 CurrencyPositivePattern 参数的 方法provider返回GetFormat的 NumberFormatInfo 对象的 属性定义。 如果style包含 标志,NumberStyles.AllowCurrencySymbol则可以在 中value显示货币符号。 |
| sign | 可选符号。 如果包含 标志,则符号可以出现在 的value开头;如果stylestyle包含 NumberStyles.AllowTrailingSign 标志,则它可能显示在 的末尾value。NumberStyles.AllowLeadingSign 如果style包含 标志,NumberStyles.AllowParentheses则可以在 中使用value括号来指示负值。 |
| 位数 | 从 0 到 9 的数字序列。 |
| , | 区域性特定的组分隔符。 如果style包含 标志,则可以在 中value显示 由 provider 指定的区域性的NumberStyles.AllowThousands组分隔符。 |
| . | 区域性特定的小数点符号。 如果包含 标志,则 指定的provider区域性的小数点符号可以出现在 中value。NumberStyles.AllowDecimalPointstyle |
| fractional_digits | 出现一个或多个数字 0。 仅当包含 标志时style,NumberStyles.AllowDecimalPoint小数位数才能显示在 中value。 |
| E | “e”或“E”字符,指示该值以指数 (科学) 表示法表示。 如果style包含 标志,则 value 参数可以表示指数表示法中的NumberStyles.AllowExponent数字。 |
| exponential_digits | 从 0 到 9 的数字序列。 如果style包含 标志,则 value 参数可以表示指数表示法中的NumberStyles.AllowExponent数字。 |
| hexdigits | 从 0 到 f 或 0 到 F 的十六进制数字序列。 |
注意
分析操作将忽略中 s 任何 (U+0000) 字符的终止 NUL,而不考虑 参数的值 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 | 开头或结尾处的 valuews元素,符号位于 的value开头,小数点 (.) 符号。 参数 value 还可以使用指数表示法。 |
| Number | ws、符号、组分隔符 (、) 和小数点 (.) 元素。 |
| 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)
- Source:
- BigInteger.cs
- Source:
- BigInteger.cs
- Source:
- BigInteger.cs
- Source:
- 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)
- Source:
- BigInteger.cs
- Source:
- BigInteger.cs
- Source:
- BigInteger.cs
- Source:
- 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)
- Source:
- BigInteger.cs
- Source:
- BigInteger.cs
- Source:
- BigInteger.cs
- Source:
- 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,或者其格式不正确,则转换失败。 此参数未经初始化即被传递。
返回
如果 true 成功转换,则为 value;否则为 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.
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][sign]digits[ws]
方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。
| 元素 | 说明 |
|---|---|
| ws | 可选空格。 |
| sign | 可选符号。 有效符号字符由 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)
- Source:
- BigInteger.cs
- Source:
- BigInteger.cs
- Source:
- BigInteger.cs
- Source:
- 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 参数为空字符范围或格式不正确,则转换失败。 此参数未经初始化即被传递。
返回
如果 true 成功转换,则为 value;否则为 false。
另请参阅
适用于
TryParse(String, NumberStyles, IFormatProvider, BigInteger)
- Source:
- BigInteger.cs
- Source:
- BigInteger.cs
- Source:
- BigInteger.cs
- Source:
- 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。
例外
示例
以下示例使用 和 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.
// 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) 方法,只是在转换失败时不会引发异常。 此方法无需使用异常处理来测试 FormatException 是否 value 无效且无法成功分析。
参数 style 定义 (样式元素,例如空格或正或负号) ,这些元素在参数中 value 允许分析操作成功。 它必须是枚举中的位标志 NumberStyles 的组合。 根据 的值 style, value 参数可能包含以下元素:
[ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]
style如果 参数包含 AllowHexSpecifier,则 value 参数可以包含以下元素:
[ws]hexdigits[ws]
方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。
| 元素 | 说明 |
|---|---|
| ws | 可选空格。 如果包含 标志,value则开头会显示空格,如果stylestyle包含 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite 标志,则空格会显示在 末尾value。 |
| $ | 区域性特定的货币符号。 它在字符串中的位置由 CurrencyPositivePattern 参数的 方法provider返回GetFormat的 NumberFormatInfo 对象的 属性定义。 如果style包含 标志,NumberStyles.AllowCurrencySymbol则可以显示value货币符号。 |
| sign | 可选符号。 如果style包含 标志,则符号可以出现在 的value开头,如果style包含 NumberStyles.AllowTrailingSign 标志,则它可以显示在 valueNumberStyles.AllowLeadingSign 末尾。 如果style包含 NumberStyles.AllowParentheses 标志,则可以在 中使用value括号来指示负值。 |
| 位数 | 从 0 到 9 的数字序列。 |
| , | 区域性特定的组分隔符。 如果包含 标志,则 指定的provider区域性的组分隔符会显示在 中value。NumberStyles.AllowThousandsstyle |
| . | 区域性特定的小数点符号。 如果包含 标志,则 指定的provider区域性的小数点符号可能会出现在 中value。NumberStyles.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 的十六进制数字序列。 |
注意
分析操作将忽略中 s 任何 (U+0000) 字符的终止 NUL,而不考虑参数的值 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 | 开头或结尾处的 valuews 元素,符号位于 的开头value,小数点 (。) 符号。 参数 value 还可以使用指数表示法。 |
| Number | ws、符号、组分隔符 (、) 和小数点 (.) 元素。 |
| 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 为负值,但它将 0x080 或 0x0080 解释为正值。 以下示例演示表示负值和正值的十六进制字符串之间的差异。
using System;
using System.Globalization;
using System.Numerics;
public class Example
{
public static void Main()
{
string[] hexStrings = { "80", "E293", "F9A2FF", "FFFFFFFF",
"080", "0E293", "0F9A2FF", "0FFFFFFFF",
"0080", "00E293", "00F9A2FF", "00FFFFFFFF" };
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 可以是以下任一参数:
一个 CultureInfo 对象,该对象表示提供格式设置信息的区域性。 其 GetFormat 方法返回对象, NumberFormatInfo 该对象为该区域性提供数值格式设置信息。
提供 NumberFormatInfo 数值格式设置信息的 对象。 (它的实现 GetFormat 只是返回自身。)
实现 的 IFormatProvider自定义对象。 其 GetFormat 方法实例化并返回 NumberFormatInfo 提供格式设置信息的对象。
如果 provider 为 null,则 NumberFormatInfo 使用当前区域性的 对象。