BigInteger.Parse 方法

定义

将数字的字符串表示形式转换为它的等效 BigInteger 表示形式。Converts the string representation of a number to its BigInteger equivalent.

重载

Parse(String)

将数字的字符串表示形式转换为它的等效 BigInteger 表示形式。Converts the string representation of a number to its BigInteger equivalent.

Parse(String, NumberStyles)

将指定样式的数字的字符串表示形式转换为它的等效 BigIntegerConverts the string representation of a number in a specified style to its BigInteger equivalent.

Parse(String, IFormatProvider)

将指定的区域性特定格式的数字字符串表示形式转换为它的等效 BigIntegerConverts the string representation of a number in a specified culture-specific format to its BigInteger equivalent.

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

以指定的样式将包含在指定的字符只读范围内的数字表示形式转换为其 BigInteger 等效项。Converts the representation of a number, contained in the specified read-only span of characters, in a specified style to its BigInteger equivalent.

Parse(String, NumberStyles, IFormatProvider)

将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 BigIntegerConverts the string representation of a number in a specified style and culture-specific format to its BigInteger equivalent.

Parse(String)

将数字的字符串表示形式转换为它的等效 BigInteger 表示形式。Converts the string representation of a number to its BigInteger equivalent.

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

包含要转换的数字的字符串。A string that contains the number to convert.

返回

一个值,等于 value 参数中指定的数字。A value that is equivalent to the number specified in the value parameter.

例外

valuenullvalue is null.

value 的格式不正确。value is not in the correct format.

示例

下面的示例使用 Parse(String) 方法来实例化两个 BigInteger 对象。The following example uses the Parse(String) method to instantiate two BigInteger objects. 它将每个对象乘以另一个数字,然后调用 Compare 方法来确定这两个值之间的关系。It multiplies each object by another number and then calls the Compare method to determine the relationship between the two values.

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 参数应为以下形式的数字的字符串表示形式。The value parameter should be the string representation of a number in the following form.

[ws][sign]数字[ws][ws][sign]digits[ws]

方括号 ([ and ]) 中的元素是可选的。Elements in square brackets ([ and ]) are optional. 下表对每个元素进行了描述。The following table describes each element.

元素Element 描述Description
wsws 可选空白。Optional white space.
signsign 一个可选的符号。An optional sign. 有效的符号字符由当前区域性的 NumberFormatInfo.NegativeSignNumberFormatInfo.PositiveSign 属性确定。Valid sign characters are determined by the NumberFormatInfo.NegativeSign and NumberFormatInfo.PositiveSign properties of the current culture.
digitsdigits 介于0到9之间的一系列数字。A sequence of digits ranging from 0 to 9. 任何前导零都将被忽略。Any leading zeros are ignored.

备注

使用 NumberStyles.Integer 样式来解释由 value 参数指定的字符串。The string specified by the value parameter is interpreted by using the NumberStyles.Integer style. 它不能包含任何组分隔符或小数分隔符,也不能包含小数部分。It cannot contain any group separators or decimal separator, and it cannot have a decimal portion.

value 参数是使用为当前系统区域性初始化 System.Globalization.NumberFormatInfo 对象中的格式设置信息分析的。The value parameter is parsed by using the formatting information in a System.Globalization.NumberFormatInfo object that is initialized for the current system culture. 有关详细信息,请参阅 NumberFormatInfo.CurrentInfoFor more information, see NumberFormatInfo.CurrentInfo. 若要通过使用特定区域性的格式设置信息分析字符串,请使用 Parse(String, IFormatProvider) 方法。To parse a string by using the formatting information of a specific culture, use the Parse(String, IFormatProvider) method.

重要

如果使用 Parse 方法往返 ToString 方法输出的 BigInteger 值的字符串表示形式,则应将 BigInteger.ToString(String) 方法与 "R" 格式说明符一起使用,以生成 BigInteger 值的字符串表示形式。If you use the Parse method to round-trip the string representation of a BigInteger value that was output by the ToString method, you should use the BigInteger.ToString(String) method with the "R" format specifier to generate the string representation of the BigInteger value. 否则,BigInteger 的字符串表示形式仅保留原始值的50最高有效位,并且当你使用 Parse 方法还原 BigInteger 值时,可能会丢失数据。Otherwise, the string representation of the BigInteger preserves only the 50 most significant digits of the original value, and data may be lost when you use the Parse method to restore the BigInteger value.

另请参阅

Parse(String, NumberStyles)

将指定样式的数字的字符串表示形式转换为它的等效 BigIntegerConverts the string representation of a number in a specified style to its BigInteger equivalent.

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

包含要转换的数字的字符串。A string that contains a number to convert.

style
NumberStyles

枚举值的按位组合,这些枚举值指定 value 所允许的格式。A bitwise combination of the enumeration values that specify the permitted format of value.

返回

一个值,等于 value 参数中指定的数字。A value that is equivalent to the number specified in the value parameter.

例外

style 不是 NumberStyles 值。style is not a NumberStyles value.

- 或 --or- style 包括 AllowHexSpecifierHexNumber 标志以及另一个值。style includes the AllowHexSpecifier or HexNumber flag along with another value.

valuenullvalue is null.

value 不符合由 NumberStyles 指定的输入模式。value does not comply with the input pattern specified by NumberStyles.

示例

下面的示例演示对 Parse(String, NumberStyles) 方法的调用,其中包含 style 参数的几个可能的值。The following example illustrates calls to the Parse(String, NumberStyles) method with several possible values for the style parameter. 它演示如何将字符串解释为十六进制值,以及如何禁止空格和符号符号。It illustrates how to interpret a string as a hexadecimal value, and how to disallow spaces and sign symbols.

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 参数中允许的样式元素(如空格、正号或负号符号、组分隔符符号或小数点符号),以便分析操作成功。The style parameter defines the style elements (such as white space, the positive or negative sign symbol, the group separator symbol, or the decimal point symbol) that are allowed in the value parameter for the parse operation to succeed. styles 必须是 NumberStyles 枚举中的位标志的组合。styles must be a combination of bit flags from the NumberStyles enumeration. value 包含十六进制值的字符串表示形式时,style 参数会使此方法重载非常有用,当 value 表示的数值系统(十进制或十六进制)仅在运行时已知,或者您想要禁止在 value中使用空格或符号符号时。The style parameter makes this method overload useful when value contains the string representation of a hexadecimal value, when the number system (decimal or hexadecimal) represented by value is known only at run time, or when you want to disallow white space or a sign symbol in value.

根据 style的值,value 参数可能包括以下元素:Depending on the value of style, the value parameter may include the following elements:

[ws][$][sign][数字,]位数[。fractional_digits] [E [sign]exponential_digits] [ws][ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]

如果 style 包括 NumberStyles.AllowHexSpecifier,则 value 参数可能包含以下元素:If style includes NumberStyles.AllowHexSpecifier, the value parameter may contain the following elements:

[ws]hexdigits[ws][ws]hexdigits[ws]

方括号 ([ and ]) 中的元素是可选的。Elements in square brackets ([ and ]) are optional. 下表对每个元素进行了描述。The following table describes each element.

元素Element 描述Description
wsws 可选空白。Optional white space. 如果 style 包含 NumberStyles.AllowLeadingWhite 标志,则空格可以出现在 value 的开头,如果 style 包含 NumberStyles.AllowTrailingWhite 标志,则它可以出现在 value 的末尾。White space can appear at the start of value if style includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of value if style includes the NumberStyles.AllowTrailingWhite flag.
$ 区域性特定的货币符号。A culture-specific currency symbol. 其在字符串中的位置由当前区域性的 NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern 属性定义。Its position in the string is defined by the NumberFormatInfo.CurrencyNegativePattern and NumberFormatInfo.CurrencyPositivePattern properties of the current culture. 如果 style 包含 NumberStyles.AllowCurrencySymbol 标志,当前区域性的货币符号可以出现在 value 中。The current culture's currency symbol can appear in value if style includes the NumberStyles.AllowCurrencySymbol flag.
signsign 一个可选的符号。An optional sign. 如果 style 包含 NumberStyles.AllowLeadingSign 标志,则符号会出现在 value 的开头,如果 style 包含 NumberStyles.AllowTrailingSign 标志,则它可以出现在 value 的末尾。The sign can appear at the start of value if style includes the NumberStyles.AllowLeadingSign flag, and it can appear at the end of value if style includes the NumberStyles.AllowTrailingSign flag. 如果 style 包含 NumberStyles.AllowParentheses 标志,则可以在 value 中使用括号来表示负值。Parentheses can be used in value to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.
digitsdigits

fractional_digitsfractional_digits

exponential_digitsexponential_digits
介于0到9之间的一系列数字。A sequence of digits from 0 through 9. 对于fractional_digits,只有数字0有效。For fractional_digits, only the digit 0 is valid.
, 区域性特定的组分隔符符号。A culture-specific group separator symbol. 如果 style 包含 NumberStyles.AllowThousands 标志,当前区域性的组分隔符可能出现在 value 中。The current culture's group separator can appear in value if style includes the NumberStyles.AllowThousands flag.
.. 区域性特定的小数点符号。A culture-specific decimal point symbol. 如果 style 包含 NumberStyles.AllowDecimalPoint 标志,当前区域性的小数点符号会出现在 value 中。The current culture's decimal point symbol can appear in value if style includes the NumberStyles.AllowDecimalPoint flag. 只有数字0才能作为小数位出现,以便分析操作成功;如果fractional_digits包含任何其他数字,则会引发 FormatExceptionOnly the digit 0 can appear as a fractional digit for the parse operation to succeed; if fractional_digits includes any other digit, a FormatException is thrown.
EE "E" 或 "E" 字符,指示值以指数(科学型)表示法表示。The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation. 如果 style 包含 NumberStyles.AllowExponent 标志,则 value 参数可以表示指数表示法中的数字。The value parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.
hexdigitshexdigits 从0到 f 的十六进制数字序列,或者从0到 F。A sequence of hexadecimal digits from 0 through f, or 0 through F.

备注

无论 style 参数的值是什么,分析操作都将忽略 s 中任何终止的 NUL (U + 0000)字符。Any terminating NUL (U+0000) characters in s are ignored by the parsing operation, regardless of the value of the style argument.

只包含数字的字符串(对应于 NumberStyles.None 样式)始终会成功分析。A string with digits only (which corresponds to the NumberStyles.None style) always parses successfully. 大多数其余 NumberStyles 成员控制可能存在但不需要存在于输入字符串中的元素。Most of the remaining NumberStyles members control elements that may be present, but are not required to be present, in the input string. 下表指示单个 NumberStyles 成员如何影响可能出现在 value中的元素。The following table indicates how individual NumberStyles members affect the elements that may be present in value.

NumberStylesNumberStyles value value 中允许的元素以及数字Elements permitted in value in addition to digits
None 仅限数字元素。The digits element only.
AllowDecimalPoint 小数点(.)和小数位数元素。The decimal point (.) and fractional-digits elements.
AllowExponent "E" 或 "E" 字符(指示指数表示法)以及exponential_digitsThe "e" or "E" character, which indicates exponential notation, along with exponential_digits.
AllowLeadingWhite value开头的ws元素。The ws element at the start of value.
AllowTrailingWhite value末尾的ws元素。The ws element at the end of value.
AllowLeadingSign value开头的sign元素。The sign element at the start of value.
AllowTrailingSign value末尾的sign元素。The sign element at the end of value.
AllowParentheses 用括号括起数值的符号元素。The sign element in the form of parentheses enclosing the numeric value.
AllowThousands 组分隔符(,)元素。The group separator (,) element.
AllowCurrencySymbol 货币符号($)。The currency ($) element.
Currency 所有元素。All elements. 但是,value 不能表示十六进制数或以指数表示法表示的数字。However, value cannot represent a hexadecimal number or a number in exponential notation.
Float value开头或结尾处的ws元素,在 value开头处进行签名,并在小数点(.)符号处进行签名The ws element at the start or end of value, sign at the start of value, and the decimal point (.) symbol. value 参数还可以使用指数表示法。The value parameter can also use exponential notation.
Number wssign、组分隔符()和小数点(.)元素。The ws, sign, group separator (,), and decimal point (.) elements.
Any 所有元素。All elements. value 不能表示十六进制数。However, value cannot represent a hexadecimal number.

重要

如果使用 Parse 方法往返 ToString 方法输出的 BigInteger 值的字符串表示形式,则应将 BigInteger.ToString(String) 方法与 "R" 格式说明符一起使用,以生成 BigInteger 值的字符串表示形式。If you use the Parse method to round-trip the string representation of a BigInteger value that was output by the ToString method, you should use the BigInteger.ToString(String) method with the "R" format specifier to generate the string representation of the BigInteger value. 否则,BigInteger 的字符串表示形式仅保留原始值的50最高有效位,并且当你使用 Parse 方法还原 BigInteger 值时,可能会丢失数据。Otherwise, the string representation of the BigInteger preserves only the 50 most significant digits of the original value, and data may be lost when you use the Parse method to restore the BigInteger value.

不同于其他 NumberStyles 值,它们允许但不要求在 value中存在特定样式元素,NumberStyles.AllowHexSpecifier 样式值表示 value 中的单个数字字符始终解释为十六进制字符。Unlike the other NumberStyles values, which allow for, but do not require, the presence of particular style elements in value, the NumberStyles.AllowHexSpecifier style value means that the individual numeric characters in value are always interpreted as hexadecimal characters. 有效的十六进制字符为0-9、A-f 和 a-f。Valid hexadecimal characters are 0-9, A-F, and a-f. 可以与 style 参数组合的其他标志仅 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteThe only other flags that can be combined with the style parameter are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. NumberStyles 枚举包含复合数字样式,HexNumber,其中包含两个空白标志。)(The NumberStyles enumeration includes a composite number style, HexNumber, that includes both white-space flags.)

备注

如果 value 是十六进制数的字符串表示形式,则它的前面不能有将其视为十六进制数的任何修饰(如 0x&h)。If value is the string representation of a hexadecimal number, it cannot be preceded by any decoration (such as 0x or &h) that differentiates it as a hexadecimal number. 这将导致转换失败。This causes the conversion to fail.

如果 value 是十六进制字符串,则 Parse(String, NumberStyles) 方法会将 value 解释为使用两个补数表示形式存储的负数(如果它的前两个十六进制数字大于或等于 0x80)。If value is a hexadecimal string, the Parse(String, NumberStyles) method interprets value as a negative number stored by using two's complement representation if its first two hexadecimal digits are greater than or equal to 0x80. 换言之,该方法将 value 中第一个字节的最高序位解释为符号位。In other words, the method interprets the highest-order bit of the first byte in value as the sign bit. 若要确保十六进制字符串正确解释为正数,value 中的第一个数字的值必须为零。To make sure that a hexadecimal string is correctly interpreted as a positive number, the first digit in value must have a value of zero. 例如,方法将 0x80 解释为负值,但会将 0x0800x0080 解释为正值。For example, the method interprets 0x80 as a negative value, but it interprets either 0x080 or 0x0080 as a positive value. 下面的示例演示表示负值和正值的十六进制字符串之间的差异。The following example illustrates the difference between hexadecimal strings that represent negative and positive values.

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 对象中的格式设置信息分析的。The value parameter is parsed by using the formatting information in a NumberFormatInfo object that is initialized for the current system culture. 若要指定其格式设置信息用于分析操作的区域性,请调用 Parse(String, NumberStyles, IFormatProvider) 重载。To specify the culture whose formatting information is used for the parse operation, call the Parse(String, NumberStyles, IFormatProvider) overload.

另请参阅

Parse(String, IFormatProvider)

将指定的区域性特定格式的数字字符串表示形式转换为它的等效 BigIntegerConverts the string representation of a number in a specified culture-specific format to its BigInteger equivalent.

public:
 static System::Numerics::BigInteger Parse(System::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

包含要转换的数字的字符串。A string that contains a number to convert.

provider
IFormatProvider

一个对象,提供有关 value 的区域性特定格式设置信息。An object that provides culture-specific formatting information about value.

返回

一个值,等于 value 参数中指定的数字。A value that is equivalent to the number specified in the value parameter.

例外

valuenullvalue is null.

value 的格式不正确。value is not in the correct format.

示例

下面的示例演示了两种将颚化符(~)定义为负数格式 BigInteger 值的方法。The following examples show two ways to define the tilde (~) as a negative sign for formatting BigInteger values. 请注意,若要显示与原始字符串格式相同的 BigInteger 值,你的代码必须调用 BigInteger.ToString(IFormatProvider) 方法并向其传递提供格式设置信息的 NumberFormatInfo 对象。Note that to display the BigInteger values in the same format as the original strings, your code must call the BigInteger.ToString(IFormatProvider) method and pass it the NumberFormatInfo object that provides formatting information.

第一个示例定义一个实现 IFormatProvider 的类,并使用 GetFormat 方法返回提供格式设置信息的 NumberFormatInfo 对象。The first example defines a class that implements IFormatProvider and uses the GetFormat method to return the NumberFormatInfo object that provides formatting information.

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 对象:A BigInteger object can then be instantiated with the following code:

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)

第二个示例更简单。The second example is more straightforward. 它将提供格式设置信息的 NumberFormatInfo 对象传递给 provider 参数。It passes the NumberFormatInfo object that provides formatting information to the provider parameter.

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 参数应为以下形式的数字的字符串表示形式:The value parameter should be the string representation of a number in the following form:

[ws][sign]数字[ws][ws][sign]digits[ws]

方括号 ([ and ]) 中的元素是可选的。Elements in square brackets ([ and ]) are optional. 下表对每个元素进行了描述。The following table describes each element.

元素Element 描述Description
wsws 可选空白。Optional white space.
signsign 一个可选的符号。An optional sign. 有效的符号字符由 provider 对象的 GetFormat 方法返回的 NumberFormatInfo 对象的 NumberFormatInfo.NegativeSignNumberFormatInfo.PositiveSign 属性确定。Valid sign characters are determined by the NumberFormatInfo.NegativeSign and NumberFormatInfo.PositiveSign properties of the NumberFormatInfo object that is returned by the provider object's GetFormat method.
digitsdigits 介于0到9之间的一系列数字。A sequence of digits ranging from 0 to 9. 任何前导零都将被忽略。Any leading zeros are ignored.

备注

使用 NumberStyles.Integer 样式解释 value 参数指定的字符串。The string specified by the value parameter is interpreted using the NumberStyles.Integer style. 它不能包含任何组分隔符或小数分隔符,也不能包含小数部分。It cannot contain any group separators or decimal separator, and it cannot have a decimal portion.

重要

如果使用 Parse 方法往返 ToString 方法输出的 BigInteger 值的字符串表示形式,则应将 BigInteger.ToString(String) 方法与 "R" 格式说明符一起使用,以生成 BigInteger 值的字符串表示形式。If you use the Parse method to round-trip the string representation of a BigInteger value that was output by the ToString method, you should use the BigInteger.ToString(String) method with the "R" format specifier to generate the string representation of the BigInteger value. 否则,BigInteger 的字符串表示形式仅保留原始值的50最高有效位,并且当你使用 Parse 方法还原 BigInteger 值时,可能会丢失数据。Otherwise, the string representation of the BigInteger preserves only the 50 most significant digits of the original value, and data may be lost when you use the Parse method to restore the BigInteger value.

provider 参数是一个 IFormatProvider 实现,其 GetFormat 方法返回提供区域性特定格式设置信息的 NumberFormatInfo 对象。The provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object that provides culture-specific formatting information. 调用 Parse(String, IFormatProvider) 方法时,它将调用 provider 参数的 GetFormat 方法,并向其传递一个表示 NumberFormatInfo 类型的 Type 对象。When the Parse(String, IFormatProvider) method is invoked, it calls the provider parameter's GetFormat method and passes it a Type object that represents the NumberFormatInfo type. 然后 GetFormat 方法返回 NumberFormatInfo 对象,该对象提供有关 value 参数格式的信息。The GetFormat method then returns the NumberFormatInfo object that provides information about the format of the value parameter. 有三种方法可以使用 provider 参数向分析操作提供自定义格式设置信息:There are three ways to use the provider parameter to supply custom formatting information to the parse operation:

  • 可以传递表示提供格式设置信息的区域性的 CultureInfo 对象。You can pass a CultureInfo object that represents the culture that supplies formatting information. 它的 GetFormat 方法返回 NumberFormatInfo 对象,该对象提供该区域性的数字格式设置信息。Its GetFormat method returns the NumberFormatInfo object that provides numeric formatting information for that culture.

  • 可以传递提供数字格式设置信息的实际 NumberFormatInfo 对象。You can pass the actual NumberFormatInfo object that provides numeric formatting information. (其 GetFormat 的实现只返回自身。)(Its implementation of GetFormat just returns itself.)

  • 可以传递实现 IFormatProvider的自定义对象。You can pass a custom object that implements IFormatProvider. 它的 GetFormat 方法实例化并返回提供格式设置信息的 NumberFormatInfo 对象。Its GetFormat method instantiates and returns the NumberFormatInfo object that provides formatting information.

如果 null``provider,则将根据当前区域性的 NumberFormatInfo 对象来解释 value 的格式设置。If provider is null, the formatting of value is interpreted based on the NumberFormatInfo object of the current culture.

另请参阅

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

以指定的样式将包含在指定的字符只读范围内的数字表示形式转换为其 BigInteger 等效项。Converts the representation of a number, contained in the specified read-only span of characters, in a specified style to its BigInteger equivalent.

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 = null) As BigInteger

参数

value
ReadOnlySpan<Char>

字符的只读范围,其中包含要转换的数字。A read-only span of characters that contains the number to convert.

style
NumberStyles

枚举值的按位组合,这些枚举值指定 value 所允许的格式。A bitwise combination of the enumeration values that specify the permitted format of value.

provider
IFormatProvider

一个对象,提供有关 value 的区域性特定格式设置信息。An object that provides culture-specific formatting information about value.

返回

一个值,等于 value 参数中指定的数字。A value that is equivalent to the number specified in the value parameter.

例外

style 不是 NumberStyles 值。style is not a NumberStyles value.

- 或 --or- style 包括 AllowHexSpecifierHexNumber 标志以及另一个值。style includes the AllowHexSpecifier or HexNumber flag along with another value.

valuenullvalue is null.

value 不符合由 style 指定的输入模式。value does not comply with the input pattern specified by style.

注解

style 参数定义在 value 参数中允许的样式元素(如空格、正号或负号符号、组分隔符符号或小数点符号),以便分析操作成功。The style parameter defines the style elements (such as white space, the positive or negative sign symbol, the group separator symbol, or the decimal point symbol) that are allowed in the value parameter for the parse operation to succeed. styles 必须是 NumberStyles 枚举中的位标志的组合。styles must be a combination of bit flags from the NumberStyles enumeration. value 包含十六进制值的表示形式时,style 参数会使此方法重载非常有用,当 value 表示的数字系统(十进制或十六进制)仅在运行时已知,或者当你要禁止在 value中使用空格或符号符号时。The style parameter makes this method overload useful when value contains the representation of a hexadecimal value, when the number system (decimal or hexadecimal) represented by value is known only at run time, or when you want to disallow white space or a sign symbol in value.

根据 style的值,value 参数可能包括以下元素:Depending on the value of style, the value parameter may include the following elements:

[ws][$][sign][数字,]位数[. Fractional_digits] [E [sign]exponential_digits] [ws][ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]

如果 style 包括 NumberStyles.AllowHexSpecifier,则 value 参数可能包括以下元素:If style includes NumberStyles.AllowHexSpecifier, the value parameter may include the following elements:

[ws]hexdigits[ws][ws]hexdigits[ws]

方括号 ([ and ]) 中的元素是可选的。Elements in square brackets ([ and ]) are optional. 下表对每个元素进行了描述。The following table describes each element.

元素Element 描述Description
wsws 可选空白。Optional white space. 如果 style 包含 NumberStyles.AllowLeadingWhite 标志,则空格可以出现在 value 的开头,如果 style 包含 NumberStyles.AllowTrailingWhite 标志,则它可以出现在 value 的末尾。White space can appear at the start of value if style includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of value if style includes the NumberStyles.AllowTrailingWhite flag.
$ 区域性特定的货币符号。A culture-specific currency symbol. 它在 value 中的位置由 provider 参数所指示的区域性的 NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern 属性定义。Its position in value is defined by the NumberFormatInfo.CurrencyNegativePattern and NumberFormatInfo.CurrencyPositivePattern properties of the culture indicated by the provider parameter. 如果 style 包含 NumberStyles.AllowCurrencySymbol 标志,当前区域性的货币符号可以出现在 value 中。The current culture's currency symbol can appear in value if style includes the NumberStyles.AllowCurrencySymbol flag.
signsign 一个可选的符号。An optional sign. 如果 style 包含 NumberStyles.AllowLeadingSign 标志,则符号会出现在 value 的开头,如果 style 包含 NumberStyles.AllowTrailingSign 标志,则它可以出现在 value 的末尾。The sign can appear at the start of value if style includes the NumberStyles.AllowLeadingSign flag, and it can appear at the end of value if style includes the NumberStyles.AllowTrailingSign flag. 如果 style 包含 NumberStyles.AllowParentheses 标志,则可以在 value 中使用括号来表示负值。Parentheses can be used in value to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.
digitsdigits

fractional_digitsfractional_digits

exponential_digitsexponential_digits
介于0到9之间的一系列数字。A sequence of digits from 0 through 9. 对于fractional_digits,只有数字0有效。For fractional_digits, only the digit 0 is valid.
, 区域性特定的组分隔符符号。A culture-specific group separator symbol. 如果 style 包括 NumberStyles.AllowThousands 标志,则 provider 指定的区域性的组分隔符符号可以出现在 value 中。The group separator symbol of the culture specified by provider can appear in value if style includes the NumberStyles.AllowThousands flag.
.. 区域性特定的小数点符号。A culture-specific decimal point symbol. 如果 style 包括 NumberStyles.AllowDecimalPoint 标志,则 provider 指定的区域性的小数点符号可以出现在 value 中。The decimal point symbol of the culture designated by provider can appear in value if style includes the NumberStyles.AllowDecimalPoint flag. 只有数字0才能作为小数位出现,以便分析操作成功;如果fractional_digits包含任何其他数字,则会引发 FormatExceptionOnly the digit 0 can appear as a fractional digit for the parse operation to succeed; if fractional_digits includes any other digit, a FormatException is thrown.
EE "E" 或 "E" 字符,指示值以指数(科学型)表示法表示。The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation. 如果 style 包含 NumberStyles.AllowExponent 标志,则 value 参数可以表示指数表示法中的数字。The value parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.
hexdigitshexdigits 从0到 f 的十六进制数字序列,或者从0到 F。A sequence of hexadecimal digits from 0 through f, or 0 through F.

备注

无论 style 参数的值是什么,分析操作都将忽略 s 中任何终止的 NUL (U + 0000)字符。Any terminating NUL (U+0000) characters in s are ignored by the parsing operation, regardless of the value of the style argument.

仅使用数字(对应于 NumberStyles.None 样式)的 value 始终会成功分析。A value with digits only (which corresponds to the NumberStyles.None style) always parses successfully. 大多数其余 NumberStyles 成员控制可能存在但不需要存在于 value中的元素。Most of the remaining NumberStyles members control elements that may be present, but are not required to be present, in value. 下表指示单个 NumberStyles 成员如何影响可能出现在 value中的元素。The following table indicates how individual NumberStyles members affect the elements that may be present in value.

NumberStyles 值NumberStyles value 除了数字以外,还允许在值中使用元素Elements permitted in value in addition to digits
None 仅限数字元素。The digits element only.
AllowDecimalPoint 小数点(.)和小数位数元素。The decimal point (.) and fractional-digits elements.
AllowExponent "E" 或 "E" 字符,指示指数表示法。The "e" or "E" character, which indicates exponential notation. 以及exponential_digitsalong with exponential_digits.
AllowLeadingWhite value开头的ws元素。The ws element at the start of value.
AllowTrailingWhite value末尾的ws元素。The ws element at the end of value.
AllowLeadingSign value开头的sign元素。The sign element at the start of value.
AllowTrailingSign value末尾的sign元素。The sign element at the end of value.
AllowParentheses 用括号括起数值的符号元素。The sign element in the form of parentheses enclosing the numeric value.
AllowThousands 组分隔符(,)元素。The group separator (,) element.
AllowCurrencySymbol 货币符号($)。The currency ($) element.
Currency 所有元素。All elements. 但是,value 不能表示十六进制数或以指数表示法表示的数字。However, value cannot represent a hexadecimal number or a number in exponential notation.
Float value开头或结尾处的ws元素,在 value开头处进行签名,并在小数点(.)符号处进行签名The ws element at the start or end of value, sign at the start of value, and the decimal point (.) symbol. value 参数还可以使用指数表示法。The value parameter can also use exponential notation.
Number wssign、组分隔符(、)和小数点(.)元素。The ws, sign, group separator (,), and decimal point (.) elements.
Any 所有元素。All elements. value 不能表示十六进制数。However, value cannot represent a hexadecimal number.

与其他 NumberStyles 值不同,它们允许但不要求 value中存在特定样式元素,NumberStyles.AllowHexSpecifier 样式值表示 value 中的单个数字字符始终解释为十六进制字符。Unlike the other NumberStyles values, which allow for but do not require the presence of particular style elements in value, the NumberStyles.AllowHexSpecifier style value means that the individual numeric characters in value are always interpreted as hexadecimal characters. 有效的十六进制字符为0-9、A-f 和 a-f。Valid hexadecimal characters are 0-9, A-F, and a-f. 可以与 style 参数组合的其他标志仅 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteThe only other flags that can be combined with the style parameter are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. NumberStyles 枚举包含复合数字样式,HexNumber,其中包含两个空白标志。)(The NumberStyles enumeration includes a composite number style, HexNumber, that includes both white-space flags.)

provider 参数是一个 IFormatProvider 实现。The provider parameter is an IFormatProvider implementation. 它的 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关 value格式的区域性特定信息。Its GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of value. 通常,provider 可以是以下任一项:Typically, provider can be any one of the following:

如果 null``provider,则使用当前区域性的 NumberFormatInfo 对象。If provider is null, the NumberFormatInfo object for the current culture is used.

另请参阅

Parse(String, NumberStyles, IFormatProvider)

将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 BigIntegerConverts the string representation of a number in a specified style and culture-specific format to its BigInteger equivalent.

public:
 static System::Numerics::BigInteger Parse(System::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

包含要转换的数字的字符串。A string that contains a number to convert.

style
NumberStyles

枚举值的按位组合,这些枚举值指定 value 所允许的格式。A bitwise combination of the enumeration values that specify the permitted format of value.

provider
IFormatProvider

一个对象,提供有关 value 的区域性特定格式设置信息。An object that provides culture-specific formatting information about value.

返回

一个值,等于 value 参数中指定的数字。A value that is equivalent to the number specified in the value parameter.

例外

style 不是 NumberStyles 值。style is not a NumberStyles value.

- 或 --or- style 包括 AllowHexSpecifierHexNumber 标志以及另一个值。style includes the AllowHexSpecifier or HexNumber flag along with another value.

valuenullvalue is null.

value 不符合由 style 指定的输入模式。value does not comply with the input pattern specified by style.

示例

下面的示例使用 styleprovider 参数的值的各种组合对 Parse(String, NumberStyles, IFormatProvider) 方法进行多次调用。The following example makes several calls to the Parse(String, NumberStyles, IFormatProvider) method using various combinations of values for the style and provider parameters.

// 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 类的实例,该类将一个波形符(~)定义为负号。A number of the individual calls to the Parse(String, NumberStyles, IFormatProvider) method pass an instance of the following BigIntegerFormatProvider class, which defines a tilde (~) as the negative sign.

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 参数中允许的样式元素(如空格、正号或负号符号、组分隔符符号或小数点符号),以便分析操作成功。The style parameter defines the style elements (such as white space, the positive or negative sign symbol, the group separator symbol, or the decimal point symbol) that are allowed in the value parameter for the parse operation to succeed. styles 必须是 NumberStyles 枚举中的位标志的组合。styles must be a combination of bit flags from the NumberStyles enumeration. value 包含十六进制值的字符串表示形式时,style 参数会使此方法重载非常有用,当 value 表示的数值系统(十进制或十六进制)仅在运行时已知,或者您想要禁止在 value中使用空格或符号符号时。The style parameter makes this method overload useful when value contains the string representation of a hexadecimal value, when the number system (decimal or hexadecimal) represented by value is known only at run time, or when you want to disallow white space or a sign symbol in value.

根据 style的值,value 参数可能包括以下元素:Depending on the value of style, the value parameter may include the following elements:

[ws][$][sign][数字,]位数[. Fractional_digits] [E [sign]exponential_digits] [ws][ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]

如果 style 包括 NumberStyles.AllowHexSpecifier,则 value 参数可能包括以下元素:If style includes NumberStyles.AllowHexSpecifier, the value parameter may include the following elements:

[ws]hexdigits[ws][ws]hexdigits[ws]

方括号 ([ and ]) 中的元素是可选的。Elements in square brackets ([ and ]) are optional. 下表对每个元素进行了描述。The following table describes each element.

元素Element 描述Description
wsws 可选空白。Optional white space. 如果 style 包含 NumberStyles.AllowLeadingWhite 标志,则空格可以出现在 value 的开头,如果 style 包含 NumberStyles.AllowTrailingWhite 标志,则它可以出现在 value 的末尾。White space can appear at the start of value if style includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of value if style includes the NumberStyles.AllowTrailingWhite flag.
$ 区域性特定的货币符号。A culture-specific currency symbol. 其在字符串中的位置由 provider 参数指示的区域性的 NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern 属性定义。Its position in the string is defined by the NumberFormatInfo.CurrencyNegativePattern and NumberFormatInfo.CurrencyPositivePattern properties of the culture indicated by the provider parameter. 如果 style 包含 NumberStyles.AllowCurrencySymbol 标志,当前区域性的货币符号可以出现在 value 中。The current culture's currency symbol can appear in value if style includes the NumberStyles.AllowCurrencySymbol flag.
signsign 一个可选的符号。An optional sign. 如果 style 包含 NumberStyles.AllowLeadingSign 标志,则符号会出现在 value 的开头,如果 style 包含 NumberStyles.AllowTrailingSign 标志,则它可以出现在 value 的末尾。The sign can appear at the start of value if style includes the NumberStyles.AllowLeadingSign flag, and it can appear at the end of value if style includes the NumberStyles.AllowTrailingSign flag. 如果 style 包含 NumberStyles.AllowParentheses 标志,则可以在 value 中使用括号来表示负值。Parentheses can be used in value to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.
digitsdigits

fractional_digitsfractional_digits

exponential_digitsexponential_digits
介于0到9之间的一系列数字。A sequence of digits from 0 through 9. 对于fractional_digits,只有数字0有效。For fractional_digits, only the digit 0 is valid.
, 区域性特定的组分隔符符号。A culture-specific group separator symbol. 如果 style 包括 NumberStyles.AllowThousands 标志,则 provider 指定的区域性的组分隔符符号可以出现在 value 中。The group separator symbol of the culture specified by provider can appear in value if style includes the NumberStyles.AllowThousands flag.
.. 区域性特定的小数点符号。A culture-specific decimal point symbol. 如果 style 包括 NumberStyles.AllowDecimalPoint 标志,则 provider 指定的区域性的小数点符号可以出现在 value 中。The decimal point symbol of the culture designated by provider can appear in value if style includes the NumberStyles.AllowDecimalPoint flag. 只有数字0才能作为小数位出现,以便分析操作成功;如果fractional_digits包含任何其他数字,则会引发 FormatExceptionOnly the digit 0 can appear as a fractional digit for the parse operation to succeed; if fractional_digits includes any other digit, a FormatException is thrown.
EE "E" 或 "E" 字符,指示值以指数(科学型)表示法表示。The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation. 如果 style 包含 NumberStyles.AllowExponent 标志,则 value 参数可以表示指数表示法中的数字。The value parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.
hexdigitshexdigits 从0到 f 的十六进制数字序列,或者从0到 F。A sequence of hexadecimal digits from 0 through f, or 0 through F.

备注

无论 style 参数的值是什么,分析操作都将忽略 s 中任何终止的 NUL (U + 0000)字符。Any terminating NUL (U+0000) characters in s are ignored by the parsing operation, regardless of the value of the style argument.

只包含数字的字符串(对应于 NumberStyles.None 样式)始终会成功分析。A string with digits only (which corresponds to the NumberStyles.None style) always parses successfully. 大多数其余 NumberStyles 成员控制可能存在但不需要存在于输入字符串中的元素。Most of the remaining NumberStyles members control elements that may be present, but are not required to be present, in the input string. 下表指示单个 NumberStyles 成员如何影响可能出现在 value中的元素。The following table indicates how individual NumberStyles members affect the elements that may be present in value.

NumberStyles 值NumberStyles value 除了数字以外,还允许在值中使用元素Elements permitted in value in addition to digits
None 仅限数字元素。The digits element only.
AllowDecimalPoint 小数点(.)和小数位数元素。The decimal point (.) and fractional-digits elements.
AllowExponent "E" 或 "E" 字符,指示指数表示法。The "e" or "E" character, which indicates exponential notation. 以及exponential_digitsalong with exponential_digits.
AllowLeadingWhite value开头的ws元素。The ws element at the start of value.
AllowTrailingWhite value末尾的ws元素。The ws element at the end of value.
AllowLeadingSign value开头的sign元素。The sign element at the start of value.
AllowTrailingSign value末尾的sign元素。The sign element at the end of value.
AllowParentheses 用括号括起数值的符号元素。The sign element in the form of parentheses enclosing the numeric value.
AllowThousands 组分隔符(,)元素。The group separator (,) element.
AllowCurrencySymbol 货币符号($)。The currency ($) element.
Currency 所有元素。All elements. 但是,value 不能表示十六进制数或以指数表示法表示的数字。However, value cannot represent a hexadecimal number or a number in exponential notation.
Float value开头或结尾处的ws元素,在 value开头处进行签名,并在小数点(.)符号处进行签名The ws element at the start or end of value, sign at the start of value, and the decimal point (.) symbol. value 参数还可以使用指数表示法。The value parameter can also use exponential notation.
Number wssign、组分隔符(、)和小数点(.)元素。The ws, sign, group separator (,), and decimal point (.) elements.
Any 所有元素。All elements. value 不能表示十六进制数。However, value cannot represent a hexadecimal number.

重要

如果使用 Parse 方法往返 ToString 方法输出的 BigInteger 值的字符串表示形式,则应将 BigInteger.ToString(String) 方法与 "R" 格式说明符一起使用,以生成 BigInteger 值的字符串表示形式。If you use the Parse method to round-trip the string representation of a BigInteger value that was output by the ToString method, you should use the BigInteger.ToString(String) method with the "R" format specifier to generate the string representation of the BigInteger value. 否则,BigInteger 的字符串表示形式仅保留原始值的50最高有效位,并且当你使用 Parse 方法还原 BigInteger 值时,可能会丢失数据。Otherwise, the string representation of the BigInteger preserves only the 50 most significant digits of the original value, and data may be lost when you use the Parse method to restore the BigInteger value.

与其他 NumberStyles 值不同,它们允许但不要求 value中存在特定样式元素,NumberStyles.AllowHexSpecifier 样式值表示 value 中的单个数字字符始终解释为十六进制字符。Unlike the other NumberStyles values, which allow for but do not require the presence of particular style elements in value, the NumberStyles.AllowHexSpecifier style value means that the individual numeric characters in value are always interpreted as hexadecimal characters. 有效的十六进制字符为0-9、A-f 和 a-f。Valid hexadecimal characters are 0-9, A-F, and a-f. 可以与 style 参数组合的其他标志仅 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteThe only other flags that can be combined with the style parameter are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. NumberStyles 枚举包含复合数字样式,HexNumber,其中包含两个空白标志。)(The NumberStyles enumeration includes a composite number style, HexNumber, that includes both white-space flags.)

备注

如果 value 是十六进制数的字符串表示形式,则它的前面不能有将其视为十六进制数的任何修饰(如 0x&h)。If value is the string representation of a hexadecimal number, it cannot be preceded by any decoration (such as 0x or &h) that differentiates it as a hexadecimal number. 这将导致转换失败。This causes the conversion to fail.

如果 value 是十六进制字符串,则 Parse(String, NumberStyles) 方法会将 value 解释为使用两个补数表示形式存储的负数(如果它的前两个十六进制数字大于或等于 0x80)。If value is a hexadecimal string, the Parse(String, NumberStyles) method interprets value as a negative number stored by using two's complement representation if its first two hexadecimal digits are greater than or equal to 0x80. 换言之,该方法将 value 中第一个字节的最高序位解释为符号位。In other words, the method interprets the highest-order bit of the first byte in value as the sign bit. 若要确保十六进制字符串正确解释为正数,value 中的第一个数字的值必须为零。To make sure that a hexadecimal string is correctly interpreted as a positive number, the first digit in value must have a value of zero. 例如,方法将 0x80 解释为负值,但会将 0x0800x0080 解释为正值。For example, the method interprets 0x80 as a negative value, but it interprets either 0x080 or 0x0080 as a positive value. 下面的示例演示表示负值和正值的十六进制字符串之间的差异。The following example illustrates the difference between hexadecimal strings that represent negative and positive values.

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 实现。The provider parameter is an IFormatProvider implementation. 它的 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关 value格式的区域性特定信息。Its GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of value. 通常,provider 可以是以下任一项:Typically, provider can be any one of the following:

如果 null``provider,则使用当前区域性的 NumberFormatInfo 对象。If provider is null, the NumberFormatInfo object for the current culture is used.

另请参阅

适用于