BigInteger.Parse 方法

定义

将数字的字符串表示形式转换为它的等效 BigInteger 表示形式。

重载

Parse(String)

将数字的字符串表示形式转换为它的等效 BigInteger 表示形式。

Parse(ReadOnlySpan<Char>, IFormatProvider)

将字符范围解析为值。

Parse(String, NumberStyles)

将指定样式的数字的字符串表示形式转换为它的等效 BigInteger

Parse(String, IFormatProvider)

将指定的区域性特定格式的数字字符串表示形式转换为它的等效 BigInteger

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

以指定的样式将包含在指定的字符只读范围内的数字表示形式转换为其 BigInteger 等效项。

Parse(String, NumberStyles, IFormatProvider)

将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 BigInteger

Parse(String)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

将数字的字符串表示形式转换为它的等效 BigInteger 表示形式。

public:
 static System::Numerics::BigInteger Parse(System::String ^ value);
public static System.Numerics.BigInteger Parse (string value);
static member Parse : string -> System.Numerics.BigInteger
Public Shared Function Parse (value As String) As BigInteger

参数

value
String

包含要转换的数字的字符串。

返回

一个值,等于 value 参数中指定的数字。

例外

valuenull

value 的格式不正确。

示例

以下示例使用 Parse(String) 方法实例化两个 BigInteger 对象。 它将每个对象乘以另一个数字, Compare 然后调用 方法来确定两个值之间的关系。

string stringToParse = String.Empty;
try
{
   // Parse two strings.
   string string1, string2;
   string1 = "12347534159895123";
   string2 = "987654321357159852";
   stringToParse = string1;
   BigInteger number1 = BigInteger.Parse(stringToParse);
   Console.WriteLine("Converted '{0}' to {1:N0}.", stringToParse, number1);
   stringToParse = string2;
   BigInteger number2 = BigInteger.Parse(stringToParse);
   Console.WriteLine("Converted '{0}' to {1:N0}.", stringToParse, number2);
   // Perform arithmetic operations on the two numbers.
   number1 *= 3;
   number2 *= 2;
   // Compare the numbers.
   int result = BigInteger.Compare(number1, number2);
   switch (result)
   {
      case -1:
         Console.WriteLine("{0} is greater than {1}.", number2, number1);
         break;
      case 0:
         Console.WriteLine("{0} is equal to {1}.", number1, number2);
         break;
      case 1:
         Console.WriteLine("{0} is greater than {1}.", number1, number2);
         break;
   }
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse {0}.", stringToParse);
}
// The example displays the following output:
//    Converted '12347534159895123' to 12,347,534,159,895,123.
//    Converted '987654321357159852' to 987,654,321,357,159,852.
//    1975308642714319704 is greater than 37042602479685369.
Dim stringToParse As String = String.Empty
Try
   ' Parse two strings.
   Dim string1, string2 As String
   string1 = "12347534159895123"
   string2 = "987654321357159852"
   stringToParse = string1
   Dim number1 As BigInteger = BigInteger.Parse(stringToParse)
   Console.WriteLine("Converted '{0}' to {1:N0}.", stringToParse, number1)
   stringToParse = string2
   Dim number2 As BigInteger = BigInteger.Parse(stringToParse)
   Console.WriteLine("Converted '{0}' to {1:N0}.", stringToParse, number2)
   ' Perform arithmetic operations on the two numbers.
   number1 *= 3
   number2 *= 2
   ' Compare the numbers.
   Select Case BigInteger.Compare(number1, number2)
      Case -1
         Console.WriteLine("{0} is greater than {1}.", number2, number1)
      Case 0
         Console.WriteLine("{0} is equal to {1}.", number1, number2)
      Case 1
         Console.WriteLine("{0} is greater than {1}.", number1, number2)
   End Select      
Catch e As FormatException
   Console.WriteLine("Unable to parse {0}.", stringToParse)
End Try
' The example displays the following output:
'    Converted '12347534159895123' to 12,347,534,159,895,123.
'    Converted '987654321357159852' to 987,654,321,357,159,852.
'    1975308642714319704 is greater than 37042602479685369.

注解

参数 value 应为数字的字符串表示形式,格式如下。

[ws][sign]digits[ws]

方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。

元素 说明
ws 可选空格。
sign 可选符号。 有效符号字符由 NumberFormatInfo.NegativeSign 当前区域性的 和 NumberFormatInfo.PositiveSign 属性决定。
位数 数字序列,范围从 0 到 9。 将忽略任何前导零。

注意

参数指定的 value 字符串通过使用 NumberStyles.Integer 样式进行解释。 它不能包含任何组分隔符或小数点分隔符,也不能包含小数部分。

参数 value 通过使用为当前系统区域性初始化的 对象中的 System.Globalization.NumberFormatInfo 格式设置信息进行分析。 有关详细信息,请参阅 NumberFormatInfo.CurrentInfo。 若要使用特定区域性的格式设置信息分析字符串,请使用 Parse(String, IFormatProvider) 方法。

重要

如果使用 Parse 方法往返方法输出ToString的值的BigInteger字符串表示形式,则应使用 BigInteger.ToString(String) 带有“R”格式说明符的 方法来生成值的字符串表示形式BigInteger。 否则,的 BigInteger 字符串表示形式仅保留原始值的 50 个最有效数字,使用 Parse 方法还原 BigInteger 值时,数据可能会丢失。

另请参阅

适用于

Parse(ReadOnlySpan<Char>, IFormatProvider)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

将字符范围解析为值。

public:
 static System::Numerics::BigInteger Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<System::Numerics::BigInteger>::Parse;
public static System.Numerics.BigInteger Parse (ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> System.Numerics.BigInteger
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As BigInteger

参数

s
ReadOnlySpan<Char>

要分析的字符范围。

provider
IFormatProvider

一个对象,提供有关 s 的区域性特定格式设置信息。

返回

分析 s的结果。

实现

适用于

Parse(String, NumberStyles)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

将指定样式的数字的字符串表示形式转换为它的等效 BigInteger

public:
 static System::Numerics::BigInteger Parse(System::String ^ value, System::Globalization::NumberStyles style);
public static System.Numerics.BigInteger Parse (string value, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> System.Numerics.BigInteger
Public Shared Function Parse (value As String, style As NumberStyles) As BigInteger

参数

value
String

包含要转换的数字的字符串。

style
NumberStyles

枚举值的按位组合,这些枚举值指定 value 所允许的格式。

返回

一个值,等于 value 参数中指定的数字。

例外

style 不是 NumberStyles 值。

- 或 -

style 包括 AllowHexSpecifierHexNumber 标志以及另一个值。

valuenull

value 不符合由 NumberStyles 指定的输入模式。

示例

以下示例演示了对 方法的调用, Parse(String, NumberStyles) 其中包含 参数的 style 多个可能值。 它说明了如何将字符串解释为十六进制值,以及如何禁止空格和符号符号。

BigInteger number;
// Method should succeed (white space and sign allowed)
number = BigInteger.Parse("   -68054   ", NumberStyles.Integer);
Console.WriteLine(number);
// Method should succeed (string interpreted as hexadecimal)
number = BigInteger.Parse("68054", NumberStyles.AllowHexSpecifier);
Console.WriteLine(number);
// Method call should fail: sign not allowed
try
{
   number = BigInteger.Parse("   -68054  ", NumberStyles.AllowLeadingWhite
                                            | NumberStyles.AllowTrailingWhite);
   Console.WriteLine(number);
}
catch (FormatException e)
{
   Console.WriteLine(e.Message);
}
// Method call should fail: white space not allowed
try
{
   number = BigInteger.Parse("   68054  ", NumberStyles.AllowLeadingSign);
   Console.WriteLine(number);
}
catch (FormatException e)
{
   Console.WriteLine(e.Message);
}
//
// The method produces the following output:
//
//     -68054
//     426068
//     Input string was not in a correct format.
//     Input string was not in a correct format.
Dim number As BigInteger 
' Method should succeed (white space and sign allowed)
number = BigInteger.Parse("   -68054   ", NumberStyles.Integer)
Console.WriteLine(number)
' Method should succeed (string interpreted as hexadecimal)
number = BigInteger.Parse("68054", NumberStyles.AllowHexSpecifier)
Console.WriteLine(number)
' Method call should fail: sign not allowed
Try
   number = BigInteger.Parse("   -68054  ", NumberStyles.AllowLeadingWhite _
                                            Or NumberStyles.AllowTrailingWhite)
   Console.WriteLine(number)
Catch e As FormatException
   Console.WriteLine(e.Message)
End Try                                                     
' Method call should fail: white space not allowed
Try
   number = BigInteger.Parse("   68054  ", NumberStyles.AllowLeadingSign)
   Console.WriteLine(number)
Catch e As FormatException
   Console.WriteLine(e.Message)
End Try    
'
' The method produces the following output:
'
'     -68054
'     426068
'     Input string was not in a correct format.
'     Input string was not in a correct format.

注解

参数 style 定义 (样式元素,例如空格、正或负号符号、组分隔符或小数点符号) ,这些元素在参数中 value 允许分析操作成功。 styles 必须是枚举中的位标志 NumberStyles 的组合。 stylevalue 包含十六进制值的字符串表示形式、表示的数字系统 (十进制或十六进制) value仅在运行时已知时,或者想要禁止空格或中的value符号符号时,参数使此方法重载非常有用。

根据 的值 stylevalue 参数可能包括以下元素:

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

如果 style 包含 NumberStyles.AllowHexSpecifier,则 value 参数可以包含以下元素:

[ws]hexdigits[ws]

方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。

元素 说明
ws 可选空格。 如果 包含标志,则空白可以出现在 的value开头;如果style包含 NumberStyles.AllowTrailingWhite 标志,则它可能显示在 的末尾valueNumberStyles.AllowLeadingWhitestyle
$ 区域性特定的货币符号。 它在字符串中的位置由 NumberFormatInfo.CurrencyNegativePattern 当前区域性的 和 NumberFormatInfo.CurrencyPositivePattern 属性定义。 如果style包含 标志,则当前区域性的货币符号会显示在 NumberStyles.AllowCurrencySymbolvalue
sign 可选符号。 如果style包含 标志,则符号可以出现在 的value开头,如果style包含 NumberStyles.AllowTrailingSign 标志,则它可以显示在 valueNumberStyles.AllowLeadingSign 末尾。 如果style包含 NumberStyles.AllowParentheses 标志,则可以在 中使用value括号来指示负值。
位数

fractional_digits

exponential_digits
从 0 到 9 的数字序列。 对于 fractional_digits,只有数字 0 有效。
, 区域性特定的组分隔符。 如果style包含 标志,则当前区域性的组分隔符会显示在 NumberStyles.AllowThousandsvalue
. 区域性特定的小数点符号。 如果style包含 标志,则当前区域性的小数点符号会显示在 NumberStyles.AllowDecimalPointvalue。 只有数字 0 可以显示为小数位数,以便分析操作成功;如果 fractional_digits 包含任何其他数字, FormatException 则会引发 。
E “e”或“E”字符,指示值以指数 (科学) 表示法表示。 如果style包含 标志,参数value可以表示指数表示法的数字NumberStyles.AllowExponent
hexdigits 从 0 到 f 或 0 到 F 的十六进制数字序列。

注意

分析操作将忽略中 s 任何 (U+0000) 字符的终止 NUL,而不考虑参数的值 style

仅包含数字的 NumberStyles.None 字符串 (对应于样式) 始终成功分析。 大多数剩余 NumberStyles 成员控制可能存在于输入字符串中但不需要存在的元素。 下表指示各个 NumberStyles 成员如何影响 中可能存在的 value元素。

NumberStyles 除数字外允许 value 的元素
None 仅限 digits 元素。
AllowDecimalPoint 小数点 (.) 和 小数位数 元素。
AllowExponent 指示指数表示法以及 exponential_digits的“e”或“E”字符。
AllowLeadingWhite 开头的 valuews 元素。
AllowTrailingWhite 末尾的 valuews 元素。
AllowLeadingSign 开头的 valuesign 元素。
AllowTrailingSign 末尾的 valuesign 元素。
AllowParentheses 以括号形式将数值括起来的 符号 元素。
AllowThousands 组分隔符 (,) 元素。
AllowCurrencySymbol 货币 ($) 元素。
Currency 所有元素。 但是, value 不能表示十六进制数或指数表示法中的数字。
Float 开头或末尾的 valuews 元素,符号位于 的开头value,小数点 () 符号。 参数 value 还可以使用指数表示法。
Number 、、 组分隔符 () 和 小数点 (.) 元素。 signws
Any 所有元素。 但是, value 不能表示十六进制数。

重要

如果使用 Parse 方法往返方法输出ToString的值的BigInteger字符串表示形式,则应使用 BigInteger.ToString(String) 带有“R”格式说明符的 方法来生成值的字符串表示形式BigInteger。 否则,的 BigInteger 字符串表示形式仅保留原始值的 50 个最有效数字,使用 Parse 方法还原 BigInteger 值时,数据可能会丢失。

与其他 NumberStyles 值不同,这些值允许(但不要求)中 value存在特定的样式元素, NumberStyles.AllowHexSpecifier 样式值意味着 中的 value 单个数字字符始终被解释为十六进制字符。 有效的十六进制字符为 0-9、A-F 和 a-f。 唯一可与 参数组合 style 的其他标志是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (枚举 NumberStyles 包含复合数字样式 , HexNumber其中包含两个空格标志。)

注意

如果 value 是十六进制数的字符串表示形式,则它前面不能有任何修饰 ((如 0x&h) )将其区分为十六进制数。 这会导致转换失败。

如果 value 是十六进制字符串,则Parse(String, NumberStyles)如果前两个十六进制数字大于或等于 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" };
      foreach (string hexString in hexStrings)
      {
         BigInteger number = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier);
         Console.WriteLine("Converted 0x{0} to {1}.", hexString, number);
      }
   }
}
// The example displays the following output:
//       Converted 0x80 to -128.
//       Converted 0xE293 to -7533.
//       Converted 0xF9A2FF to -417025.
//       Converted 0xFFFFFFFF to -1.
//       Converted 0x080 to 128.
//       Converted 0x0E293 to 58003.
//       Converted 0x0F9A2FF to 16360191.
//       Converted 0x0FFFFFFFF to 4294967295.
//       Converted 0x0080 to 128.
//       Converted 0x00E293 to 58003.
//       Converted 0x00F9A2FF to 16360191.
//       Converted 0x00FFFFFFFF to 4294967295.
Imports System.Globalization
Imports System.Numerics

Module Example
   Public Sub Main()
      Dim hexStrings() As String = { "80", "E293", "F9A2FF", "FFFFFFFF", 
                                     "080", "0E293", "0F9A2FF", "0FFFFFFFF",  
                                     "0080", "00E293", "00F9A2FF", "00FFFFFFFF" }
      For Each hexString As String In hexStrings
         Dim number As BigInteger = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier)
         Console.WriteLine("Converted 0x{0} to {1}.", hexString, number)
      Next         
   End Sub
End Module
' The example displays the following output:
'       Converted 0x80 to -128.
'       Converted 0xE293 to -7533.
'       Converted 0xF9A2FF to -417025.
'       Converted 0xFFFFFFFF to -1.
'       Converted 0x080 to 128.
'       Converted 0x0E293 to 58003.
'       Converted 0x0F9A2FF to 16360191.
'       Converted 0x0FFFFFFFF to 4294967295.
'       Converted 0x0080 to 128.
'       Converted 0x00E293 to 58003.
'       Converted 0x00F9A2FF to 16360191.
'       Converted 0x00FFFFFFFF to 4294967295.

参数 value 通过使用为当前系统区域性初始化的 对象中的 NumberFormatInfo 格式设置信息进行分析。 若要指定其格式信息用于分析操作的区域性,请调用 Parse(String, NumberStyles, IFormatProvider) 重载。

另请参阅

适用于

Parse(String, IFormatProvider)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

将指定的区域性特定格式的数字字符串表示形式转换为它的等效 BigInteger

public:
 static System::Numerics::BigInteger Parse(System::String ^ value, IFormatProvider ^ provider);
public:
 static System::Numerics::BigInteger Parse(System::String ^ value, IFormatProvider ^ provider) = IParsable<System::Numerics::BigInteger>::Parse;
public static System.Numerics.BigInteger Parse (string value, IFormatProvider provider);
public static System.Numerics.BigInteger Parse (string value, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> System.Numerics.BigInteger
Public Shared Function Parse (value As String, provider As IFormatProvider) As BigInteger

参数

value
String

包含要转换的数字的字符串。

provider
IFormatProvider

一个对象,提供有关 value 的区域性特定格式设置信息。

返回

一个值,等于 value 参数中指定的数字。

实现

例外

valuenull

value 的格式不正确。

示例

以下示例演示了将平铺 (~) 定义为设置值格式 BigInteger 的负号的两种方法。 请注意,若要以与原始字符串相同的格式显示 BigInteger 值,代码必须调用 BigInteger.ToString(IFormatProvider) 方法,并向其 NumberFormatInfo 传递提供格式设置信息的对象。

第一个示例定义一个类,该类实现 IFormatProvider 并使用 GetFormat 方法返回 NumberFormatInfo 提供格式设置信息的对象。

public class BigIntegerFormatProvider : IFormatProvider
{
   public object GetFormat(Type formatType)
   {
      if (formatType == typeof(NumberFormatInfo))
      {
         NumberFormatInfo numberFormat = new NumberFormatInfo();
         numberFormat.NegativeSign = "~";
         return numberFormat;
      }
      else
      {
         return null;
      }
   }
}
Public Class BigIntegerFormatProvider : Implements IFormatProvider
   Public Function GetFormat(formatType As Type) As Object _
                            Implements IFormatProvider.GetFormat
      If formatType Is GetType(NumberFormatInfo) Then
         Dim numberFormat As New NumberFormatInfo
         numberFormat.NegativeSign = "~"
         Return numberFormat
      Else
         Return Nothing
      End If      
   End Function
End Class

BigInteger然后,可以使用以下代码实例化对象:

BigInteger number = BigInteger.Parse("~6354129876", new BigIntegerFormatProvider());
// Display value using same formatting information
Console.WriteLine(number.ToString(new BigIntegerFormatProvider()));
// Display value using formatting of current culture
Console.WriteLine(number);
Dim number As BigInteger = BigInteger.Parse("~6354129876", New BigIntegerFormatProvider)
' Display value using same formatting information
Console.WriteLine(number.ToString(New BigIntegerFormatProvider))
' Display value using formatting of current culture
Console.WriteLine(number)

第二个示例更为简单。 它将提供格式设置信息的 对象传递给 NumberFormatInfoprovider 参数。

NumberFormatInfo fmt = new NumberFormatInfo();
fmt.NegativeSign = "~";

BigInteger number = BigInteger.Parse("~6354129876", fmt);
// Display value using same formatting information
Console.WriteLine(number.ToString(fmt));
// Display value using formatting of current culture
Console.WriteLine(number);
Dim fmt As New NumberFormatInfo()
fmt.NegativeSign = "~"

Dim number As BigInteger = BigInteger.Parse("~6354129876", fmt)
' Display value using same formatting information
Console.WriteLine(number.ToString(fmt))
' Display value using formatting of current culture
Console.WriteLine(number)

注解

参数 value 应是数字的字符串表示形式,格式如下:

[ws][sign]digits[ws]

方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。

元素 说明
ws 可选空格。
sign 可选符号。 有效符号字符由 NumberFormatInfo.NegativeSign 对象的 方法返回providerGetFormatNumberFormatInfo 对象的 和 NumberFormatInfo.PositiveSign 属性确定。
位数 数字序列,范围从 0 到 9。 将忽略任何前导零。

注意

参数指定的 value 字符串使用 NumberStyles.Integer 样式进行解释。 它不能包含任何组分隔符或小数点分隔符,也不能包含小数部分。

重要

如果使用 Parse 方法往返方法输出ToString的值的BigInteger字符串表示形式,则应使用 BigInteger.ToString(String) 带有“R”格式说明符的 方法来生成值的字符串表示形式BigInteger。 否则,的 BigInteger 字符串表示形式仅保留原始值的 50 个最有效数字,使用 Parse 方法还原 BigInteger 值时,数据可能会丢失。

provider参数是一个IFormatProvider实现,其GetFormat方法返回一个NumberFormatInfo对象,该对象提供特定于区域性的格式设置信息。 Parse(String, IFormatProvider)调用 方法时,它会调用provider参数的 GetFormat 方法,并为其传递一个Type表示类型的 NumberFormatInfo 对象。 然后, GetFormat 方法返回 对象, NumberFormatInfo 该对象提供有关 参数格式 value 的信息。 有三种方法可以使用 provider 参数向分析操作提供自定义格式设置信息:

如果 providernull,则根据NumberFormatInfo当前区域性的对象解释 的格式value

另请参阅

适用于

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

以指定的样式将包含在指定的字符只读范围内的数字表示形式转换为其 BigInteger 等效项。

public static System.Numerics.BigInteger Parse (ReadOnlySpan<char> value, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
public static System.Numerics.BigInteger Parse (ReadOnlySpan<char> value, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> System.Numerics.BigInteger
Public Shared Function Parse (value As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As BigInteger

参数

value
ReadOnlySpan<Char>

字符的只读范围,其中包含要转换的数字。

style
NumberStyles

枚举值的按位组合,这些枚举值指定 value 所允许的格式。

provider
IFormatProvider

一个对象,提供有关 value 的区域性特定格式设置信息。

返回

一个值,等于 value 参数中指定的数字。

实现

例外

style 不是 NumberStyles 值。

- 或 -

style 包括 AllowHexSpecifierHexNumber 标志以及另一个值。

valuenull

value 不符合由 style 指定的输入模式。

注解

参数 style 定义 (样式元素,例如空格、正或负号符号、组分隔符或小数点符号) ,这些元素在参数中 value 允许分析操作成功。 styles 必须是枚举中的位标志 NumberStyles 的组合。 stylevalue包含十六进制值的表示形式、表示的数字系统 (十进制或十六进制) value仅在运行时已知,或者想要禁止空格或中的value符号符号时,参数使此方法重载非常有用。

根据 的值 stylevalue 参数可能包括以下元素:

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

如果 style 包含 NumberStyles.AllowHexSpecifier,则 value 参数可以包含以下元素:

[ws]hexdigits[ws]

方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。

元素 说明
ws 可选空格。 如果 包含标志,则空白可以出现在 的value开头;如果style包含 NumberStyles.AllowTrailingWhite 标志,则它可能显示在 的末尾valueNumberStyles.AllowLeadingWhitestyle
$ 区域性特定的货币符号。 它在 中value的位置由 NumberFormatInfo.CurrencyNegativePattern 参数指示provider的区域性的 和 NumberFormatInfo.CurrencyPositivePattern 属性定义。 如果style包含 标志,则当前区域性的货币符号会显示在 NumberStyles.AllowCurrencySymbolvalue
sign 可选符号。 如果style包含 标志,则符号可以出现在 的value开头,如果style包含 NumberStyles.AllowTrailingSign 标志,则它可以显示在 valueNumberStyles.AllowLeadingSign 末尾。 如果style包含 NumberStyles.AllowParentheses 标志,则可以在 中使用value括号来指示负值。
位数

fractional_digits

exponential_digits
从 0 到 9 的数字序列。 对于 fractional_digits,只有数字 0 有效。
, 区域性特定的组分隔符。 如果包含 标志,则 指定的provider区域性的组分隔符可以出现在 中valueNumberStyles.AllowThousandsstyle
. 区域性特定的小数点符号。 如果style包含 NumberStyles.AllowDecimalPoint 标志,则可以显示 valueprovider 指定的区域性的小数点符号。 只有数字 0 可以显示为小数位数,以便分析操作成功;如果 fractional_digits 包含任何其他数字, FormatException 则会引发 。
E “e”或“E”字符,指示值以指数 (科学) 表示法表示。 如果style包含 标志,参数value可以表示指数表示法的数字NumberStyles.AllowExponent
hexdigits 从 0 到 f 或 0 到 F 的十六进制数字序列。

注意

分析操作将忽略中 s 任何 (U+0000) 字符的终止 NUL,而不考虑参数的值 style

value仅包含数字 (对应于NumberStyles.None样式) 始终成功分析的 。 在 中value,大多数剩余NumberStyles成员控制可能存在但不一定存在的元素。 下表指示各个 NumberStyles 成员如何影响 中可能存在的 value元素。

NumberStyles 值 除数字外,值中允许的元素
None 仅限 digits 元素。
AllowDecimalPoint 小数点 (.) 和 小数位数 元素。
AllowExponent 指示指数表示法的“e”或“E”字符。 以及 exponential_digits
AllowLeadingWhite 开头的 valuews 元素。
AllowTrailingWhite 末尾的 valuews 元素。
AllowLeadingSign 开头的 valuesign 元素。
AllowTrailingSign 末尾的 valuesign 元素。
AllowParentheses 以括号形式将数值括起来的 符号 元素。
AllowThousands 组分隔符 (,) 元素。
AllowCurrencySymbol 货币 ($) 元素。
Currency 所有元素。 但是, value 不能表示十六进制数或指数表示法中的数字。
Float ws 元素位于 开头或末尾value符号位于 开头,value小数点 (.) 符号。 参数 value 还可以使用指数表示法。
Number wssign、 组分隔符 (、) 和小数点 (.) 元素。
Any 所有元素。 但是, value 不能表示十六进制数。

与其他允许但不要求在 中value存在特定样式元素的其他NumberStyles值不同,NumberStyles.AllowHexSpecifier样式值意味着 中的value单个数字字符始终被解释为十六进制字符。 有效的十六进制字符为 0-9、A-F 和 a-f。 唯一可与 参数组合 style 的其他标志是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (枚举 NumberStyles 包含复合数字样式 , HexNumber其中包含两个空格标志。)

参数 provider 是一个 IFormatProvider 实现。 其 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关 格式的 value区域性特定信息。 通常, provider 可以是以下任一项:

如果 providernull,则 NumberFormatInfo 使用当前区域性的 对象。

另请参阅

适用于

Parse(String, NumberStyles, IFormatProvider)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 BigInteger

public:
 static System::Numerics::BigInteger Parse(System::String ^ value, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
 static System::Numerics::BigInteger Parse(System::String ^ value, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<System::Numerics::BigInteger>::Parse;
public static System.Numerics.BigInteger Parse (string value, System.Globalization.NumberStyles style, IFormatProvider provider);
public static System.Numerics.BigInteger Parse (string value, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> System.Numerics.BigInteger
Public Shared Function Parse (value As String, style As NumberStyles, provider As IFormatProvider) As BigInteger

参数

value
String

包含要转换的数字的字符串。

style
NumberStyles

枚举值的按位组合,这些枚举值指定 value 所允许的格式。

provider
IFormatProvider

一个对象,提供有关 value 的区域性特定格式设置信息。

返回

一个值,等于 value 参数中指定的数字。

实现

例外

style 不是 NumberStyles 值。

- 或 -

style 包括 AllowHexSpecifierHexNumber 标志以及另一个值。

valuenull

value 不符合由 style 指定的输入模式。

示例

以下示例使用 和 provider 参数的各种值style组合对 方法进行多次调用Parse(String, NumberStyles, IFormatProvider)

// Call parse with default values of style and provider
Console.WriteLine(BigInteger.Parse("  -300   ",
                  NumberStyles.Integer, CultureInfo.CurrentCulture));
// Call parse with default values of style and provider supporting tilde as negative sign
Console.WriteLine(BigInteger.Parse("   ~300  ",
                                   NumberStyles.Integer, new BigIntegerFormatProvider()));
// Call parse with only AllowLeadingWhite and AllowTrailingWhite
// Exception thrown because of presence of negative sign
try
{
   Console.WriteLine(BigInteger.Parse("    ~300   ",
                                NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite,
                                new BigIntegerFormatProvider()));
}
catch (FormatException e)
{
   Console.WriteLine("{0}: \n   {1}", e.GetType().Name, e.Message);
}
// Call parse with only AllowHexSpecifier
// Exception thrown because of presence of negative sign
try
{
   Console.WriteLine(BigInteger.Parse("-3af", NumberStyles.AllowHexSpecifier,
                                      new BigIntegerFormatProvider()));
}
catch (FormatException e)
{
   Console.WriteLine("{0}: \n   {1}", e.GetType().Name, e.Message);
}
// Call parse with only NumberStyles.None
// Exception thrown because of presence of white space and sign
try
{
   Console.WriteLine(BigInteger.Parse(" -300 ", NumberStyles.None,
                                      new BigIntegerFormatProvider()));
}
catch (FormatException e)
{
   Console.WriteLine("{0}: \n   {1}", e.GetType().Name, e.Message);
}
// The example displays the followingoutput:
//       -300
//       -300
//       FormatException:
//          The value could not be parsed.
//       FormatException:
//          The value could not be parsed.
//       FormatException:
//          The value could not be parsed.
' Call parse with default values of style and provider
Console.WriteLine(BigInteger.Parse("  -300   ", _
                  NumberStyles.Integer, CultureInfo.CurrentCulture))
' Call parse with default values of style and provider supporting tilde as negative sign
Console.WriteLine(BigInteger.Parse("   ~300  ", _
                                   NumberStyles.Integer, New BigIntegerFormatProvider()))
' Call parse with only AllowLeadingWhite and AllowTrailingWhite
' Exception thrown because of presence of negative sign
Try
   Console.WriteLIne(BigInteger.Parse("    ~300   ", _
                                      NumberStyles.AllowLeadingWhite Or NumberStyles.AllowTrailingWhite, _
                                      New BigIntegerFormatProvider()))
Catch e As FormatException
   Console.WriteLine("{0}: {1}   {2}", e.GetType().Name, vbCrLf, e.Message)
End Try                                   
' Call parse with only AllowHexSpecifier
' Exception thrown because of presence of negative sign
Try
   Console.WriteLIne(BigInteger.Parse("-3af", NumberStyles.AllowHexSpecifier, _
                                      New BigIntegerFormatProvider()))
Catch e As FormatException
   Console.WriteLine("{0}: {1}   {2}", e.GetType().Name, vbCrLf, e.Message)
End Try                                 
' Call parse with only NumberStyles.None
' Exception thrown because of presence of white space and sign
Try
   Console.WriteLIne(BigInteger.Parse(" -300 ", NumberStyles.None, _
                                      New BigIntegerFormatProvider()))
Catch e As FormatException
   Console.WriteLine("{0}: {1}   {2}", e.GetType().Name, vbCrLf, e.Message)
End Try
' The example displays the following output:
'       -300
'       -300
'       FormatException:
'          The value could not be parsed.
'       FormatException:
'          The value could not be parsed.
'       FormatException:
'          The value could not be parsed.

对 方法的一些单独调用 Parse(String, NumberStyles, IFormatProvider) 会传递以下 BigIntegerFormatProvider 类的实例,该类将平铺 (~) 定义为负号。

public class BigIntegerFormatProvider : IFormatProvider
{
   public object GetFormat(Type formatType)
   {
      if (formatType == typeof(NumberFormatInfo))
      {
         NumberFormatInfo numberFormat = new NumberFormatInfo();
         numberFormat.NegativeSign = "~";
         return numberFormat;
      }
      else
      {
         return null;
      }
   }
}
Public Class BigIntegerFormatProvider : Implements IFormatProvider
   Public Function GetFormat(formatType As Type) As Object _
                            Implements IFormatProvider.GetFormat
      If formatType Is GetType(NumberFormatInfo) Then
         Dim numberFormat As New NumberFormatInfo
         numberFormat.NegativeSign = "~"
         Return numberFormat
      Else
         Return Nothing
      End If      
   End Function
End Class

注解

参数 style 定义 (样式元素,例如空格、正或负号符号、组分隔符或小数点符号) ,这些元素在参数中 value 允许分析操作成功。 styles 必须是枚举中的位标志 NumberStyles 的组合。 stylevalue 包含十六进制值的字符串表示形式、表示的数字系统 (十进制或十六进制) value仅在运行时已知时,或者想要禁止空格或中的value符号符号时,参数使此方法重载非常有用。

根据 的值 stylevalue 参数可能包括以下元素:

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

如果 style 包含 NumberStyles.AllowHexSpecifier,则 value 参数可以包含以下元素:

[ws]hexdigits[ws]

方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。

元素 说明
ws 可选空格。 如果 包含标志,则空白可以出现在 的value开头;如果style包含 NumberStyles.AllowTrailingWhite 标志,则它可能显示在 的末尾valueNumberStyles.AllowLeadingWhitestyle
$ 区域性特定的货币符号。 它在字符串中的位置由 NumberFormatInfo.CurrencyNegativePattern 参数指示provider的区域性的 和 NumberFormatInfo.CurrencyPositivePattern 属性定义。 如果style包含 标志,则当前区域性的货币符号会显示在 NumberStyles.AllowCurrencySymbolvalue
sign 可选符号。 如果style包含 标志,则符号可以出现在 的value开头,如果style包含 NumberStyles.AllowTrailingSign 标志,则它可以显示在 valueNumberStyles.AllowLeadingSign 末尾。 如果style包含 NumberStyles.AllowParentheses 标志,则可以在 中使用value括号来指示负值。
位数

fractional_digits

exponential_digits
从 0 到 9 的数字序列。 对于 fractional_digits,只有数字 0 有效。
, 区域性特定的组分隔符。 如果包含 标志,则 指定的provider区域性的组分隔符可以出现在 中valueNumberStyles.AllowThousandsstyle
. 区域性特定的小数点符号。 如果包含 标志,则 由 provider 指定的区域性的小数点符号可以出现在 中valueNumberStyles.AllowDecimalPointstyle 只有数字 0 可以显示为分析操作成功的小数位数;如果 fractional_digits 包含任何其他数字, FormatException 则引发 。
E “e”或“E”字符,指示该值以指数 (科学) 表示法表示。 如果style包含 标志,则 value 参数可以表示指数表示法中的NumberStyles.AllowExponent数字。
hexdigits 从 0 到 f 或 0 到 F 的十六进制数字序列。

注意

分析操作将忽略中 s 任何 (U+0000) 字符的终止 NUL,而不考虑 参数的值 style

仅包含数字的字符串 (对应于 NumberStyles.None 样式) 始终成功分析。 其余 NumberStyles 大多数成员控制输入字符串中可能存在但不需要存在的元素。 下表指示各个 NumberStyles 成员如何影响 中 value可能存在的元素。

NumberStyles 值 除数字外的值允许的元素
None 仅限 digits 元素。
AllowDecimalPoint 小数点 (.) 和 小数位数 元素。
AllowExponent 指示指数表示法的“e”或“E”字符。 以及 exponential_digits
AllowLeadingWhite 开头的 valuews 元素。
AllowTrailingWhite 位于 末尾的 valuews 元素。
AllowLeadingSign 开头的value符号元素。
AllowTrailingSign 末尾的value符号元素。
AllowParentheses 用括号括住数值的 符号 元素。
AllowThousands 组分隔符 (,) 元素。
AllowCurrencySymbol $) 元素 (货币。
Currency 所有元素。 但是, value 不能表示十六进制数或指数表示法中的数字。
Float 开头或结尾处的 valuews元素,符号位于 开头,value小数点 (.) 符号。 参数 value 还可以使用指数表示法。
Number wssign、组分隔符 (、) 和小数点 (.) 元素。
Any 所有元素。 但是, value 不能表示十六进制数。

重要

如果使用 Parse 方法对方法ToString输出的值的BigInteger字符串表示形式进行往返,则应将 BigInteger.ToString(String) 方法与“R”格式说明符一起使用,以生成值的字符串表示形式BigInteger。 否则, 的 BigInteger 字符串表示形式仅保留原始值的 50 个最有效数字,使用 Parse 方法还原 BigInteger 值时,数据可能会丢失。

与其他 NumberStyles 值不同,这些值允许但不需要在 中 value存在特定的样式元素, NumberStyles.AllowHexSpecifier 样式值意味着 中的 value 单个数字字符始终解释为十六进制字符。 有效的十六进制字符为 0-9、A-F 和 a-f。 唯一可以与 style 参数组合的其他标志是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (NumberStyles 枚举包含复合数字样式 HexNumber,其中包含空格标志)

注意

如果 value 是十六进制数的字符串表示形式,则它前面不能有任何修饰 (,如 0x&h) 将其区分为十六进制数。 这会导致转换失败。

如果 value 是十六进制字符串,则 Parse(String, NumberStyles) 该方法将解释 value 为使用二的补数表示形式的负数,前提是其前两个十六进制数字大于或等于 0x80。 换句话说, 方法将 中 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" };
      foreach (string hexString in hexStrings)
      {
         BigInteger number = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier);
         Console.WriteLine("Converted 0x{0} to {1}.", hexString, number);
      }
   }
}
// The example displays the following output:
//       Converted 0x80 to -128.
//       Converted 0xE293 to -7533.
//       Converted 0xF9A2FF to -417025.
//       Converted 0xFFFFFFFF to -1.
//       Converted 0x080 to 128.
//       Converted 0x0E293 to 58003.
//       Converted 0x0F9A2FF to 16360191.
//       Converted 0x0FFFFFFFF to 4294967295.
//       Converted 0x0080 to 128.
//       Converted 0x00E293 to 58003.
//       Converted 0x00F9A2FF to 16360191.
//       Converted 0x00FFFFFFFF to 4294967295.
Imports System.Globalization
Imports System.Numerics

Module Example
   Public Sub Main()
      Dim hexStrings() As String = { "80", "E293", "F9A2FF", "FFFFFFFF", 
                                     "080", "0E293", "0F9A2FF", "0FFFFFFFF",  
                                     "0080", "00E293", "00F9A2FF", "00FFFFFFFF" }
      For Each hexString As String In hexStrings
         Dim number As BigInteger = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier)
         Console.WriteLine("Converted 0x{0} to {1}.", hexString, number)
      Next         
   End Sub
End Module
' The example displays the following output:
'       Converted 0x80 to -128.
'       Converted 0xE293 to -7533.
'       Converted 0xF9A2FF to -417025.
'       Converted 0xFFFFFFFF to -1.
'       Converted 0x080 to 128.
'       Converted 0x0E293 to 58003.
'       Converted 0x0F9A2FF to 16360191.
'       Converted 0x0FFFFFFFF to 4294967295.
'       Converted 0x0080 to 128.
'       Converted 0x00E293 to 58003.
'       Converted 0x00F9A2FF to 16360191.
'       Converted 0x00FFFFFFFF to 4294967295.

参数 provider 是实现 IFormatProvider 。 其 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关 格式的 value区域性特定信息。 通常, provider 可以是以下任一项:

如果 providernull,则 NumberFormatInfo 使用当前区域性的 对象。

另请参阅

适用于