UInt64.Parse 方法

定义

将数字的字符串表示形式转换为它的等效 64 位无符号整数。Converts the string representation of a number to its 64-bit unsigned integer equivalent.

重载

Parse(String, NumberStyles, IFormatProvider)

将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 64 位无符号整数。Converts the string representation of a number in a specified style and culture-specific format to its 64-bit unsigned integer equivalent.

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

将指定样式和区域性特定格式的数字的范围表示形式转换为其等效的 64 位无符号整数。Converts the span representation of a number in a specified style and culture-specific format to its 64-bit unsigned integer equivalent.

Parse(String, NumberStyles)

将指定样式的数字的字符串表示形式转换为它的等效 64 位无符号整数。Converts the string representation of a number in a specified style to its 64-bit unsigned integer equivalent.

Parse(String)

将数字的字符串表示形式转换为它的等效 64 位无符号整数。Converts the string representation of a number to its 64-bit unsigned integer equivalent.

Parse(String, IFormatProvider)

将指定区域性特定格式的数字的字符串表示形式转换为它的等效 64 位无符号整数。Converts the string representation of a number in a specified culture-specific format to its 64-bit unsigned integer equivalent.

Parse(String, NumberStyles, IFormatProvider)

重要

此 API 不符合 CLS。

符合 CLS 的替代方案
System.Decimal.Parse(String)

将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 64 位无符号整数。Converts the string representation of a number in a specified style and culture-specific format to its 64-bit unsigned integer equivalent.

public:
 static System::UInt64 Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public static ulong Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static ulong Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
[System.CLSCompliant(false)]
public static ulong Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
[System.CLSCompliant(false)]
public static ulong Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> uint64
[<System.CLSCompliant(false)>]
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> uint64
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As ULong

参数

s
String

表示要转换的数字的字符串。A string that represents the number to convert. 该字符串使用由 style 参数指定的样式来进行解释。The string is interpreted by using the style specified by the style parameter.

style
NumberStyles

枚举值的按位组合,用于指示可出现在 s 中的样式元素。A bitwise combination of enumeration values that indicates the style elements that can be present in s. 要指定的一个典型值为 IntegerA typical value to specify is Integer.

provider
IFormatProvider

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

返回

UInt64

s 中指定的数字等效的 64 位无符号整数。A 64-bit unsigned integer equivalent to the number specified in s.

属性

例外

s 参数为 nullThe s parameter is null.

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

- 或 --or- style 不是 AllowHexSpecifierHexNumber 值的组合。style is not a combination of AllowHexSpecifier and HexNumber values.

参数 s 的格式不符合 styleThe s parameter is not in a format compliant with style.

参数 s 表示一个小于 MinValue 或大于 MaxValue 的数字。The s parameter represents a number less than MinValue or greater than MaxValue.

- 或 --or- s 包含非零的小数位。s includes non-zero, fractional digits.

示例

下面的示例使用 Parse(String, NumberStyles, IFormatProvider) 方法将数字的各种字符串表示形式转换为64位无符号整数值。The following example uses the Parse(String, NumberStyles, IFormatProvider) method to convert various string representations of numbers to 64-bit unsigned integer values.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] cultureNames= { "en-US", "fr-FR" };
      NumberStyles[] styles= { NumberStyles.Integer,
                               NumberStyles.Integer | NumberStyles.AllowDecimalPoint };
      string[] values = { "170209", "+170209.0", "+170209,0", "-103214.00",
                                 "-103214,00", "104561.1", "104561,1" };
      
      // Parse strings using each culture
      foreach (string cultureName in cultureNames)
      {
         CultureInfo ci = new CultureInfo(cultureName);
         Console.WriteLine("Parsing strings using the {0} culture", 
                           ci.DisplayName);
         // Use each style.
         foreach (NumberStyles style in styles)
         {
            Console.WriteLine("   Style: {0}", style.ToString());
            // Parse each numeric string.
            foreach (string value in values)
            {
               try {
                  Console.WriteLine("      Converted '{0}' to {1}.", value,
                                    UInt64.Parse(value, style, ci));
               }
               catch (FormatException) {
                  Console.WriteLine("      Unable to parse '{0}'.", value);
               }      
               catch (OverflowException) {
                  Console.WriteLine("      '{0}' is out of range of the UInt64 type.",
                                    value);
               }
            }
         }
      }                                    
   }
}
// The example displays the following output:
//       Style: Integer
//          Converted '170209' to 170209.
//          Unable to parse '+170209.0'.
//          Unable to parse '+170209,0'.
//          Unable to parse '-103214.00'.
//          Unable to parse '-103214,00'.
//          Unable to parse '104561.1'.
//          Unable to parse '104561,1'.
//       Style: Integer, AllowDecimalPoint
//          Converted '170209' to 170209.
//          Converted '+170209.0' to 170209.
//          Unable to parse '+170209,0'.
//          '-103214.00' is out of range of the UInt64 type.
//          Unable to parse '-103214,00'.
//          '104561.1' is out of range of the UInt64 type.
//          Unable to parse '104561,1'.
//    Parsing strings using the French (France) culture
//       Style: Integer
//          Converted '170209' to 170209.
//          Unable to parse '+170209.0'.
//          Unable to parse '+170209,0'.
//          Unable to parse '-103214.00'.
//          Unable to parse '-103214,00'.
//          Unable to parse '104561.1'.
//          Unable to parse '104561,1'.
//       Style: Integer, AllowDecimalPoint
//          Converted '170209' to 170209.
//          Unable to parse '+170209.0'.
//          Converted '+170209,0' to 170209.
//          Unable to parse '-103214.00'.
//          '-103214,00' is out of range of the UInt64 type.
//          Unable to parse '104561.1'.
//          '104561,1' is out of range of the UInt64 type.
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim cultureNames() As String = { "en-US", "fr-FR" }
      Dim styles() As NumberStyles = { NumberStyles.Integer, _
                                       NumberStyles.Integer Or NumberStyles.AllowDecimalPoint }
      Dim values() As String = { "170209", "+170209.0", "+170209,0", "-103214.00", _
                                 "-103214,00", "104561.1", "104561,1" }
      
      ' Parse strings using each culture
      For Each cultureName As String In cultureNames
         Dim ci As New CultureInfo(cultureName)
         Console.WriteLine("Parsing strings using the {0} culture", ci.DisplayName)
         ' Use each style.
         For Each style As NumberStyles In styles
            Console.WriteLine("   Style: {0}", style.ToString())
            ' Parse each numeric string.
            For Each value As String In values
               Try
                  Console.WriteLine("      Converted '{0}' to {1}.", value, _
                                    UInt64.Parse(value, style, ci))
               Catch e As FormatException
                  Console.WriteLine("      Unable to parse '{0}'.", value)   
               Catch e As OverflowException
                  Console.WriteLine("      '{0}' is out of range of the UInt64 type.", _
                                    value)         
               End Try
            Next
         Next
      Next                                    
   End Sub
End Module
' The example displays the following output:
'       Style: Integer
'          Converted '170209' to 170209.
'          Unable to parse '+170209.0'.
'          Unable to parse '+170209,0'.
'          Unable to parse '-103214.00'.
'          Unable to parse '-103214,00'.
'          Unable to parse '104561.1'.
'          Unable to parse '104561,1'.
'       Style: Integer, AllowDecimalPoint
'          Converted '170209' to 170209.
'          Converted '+170209.0' to 170209.
'          Unable to parse '+170209,0'.
'          '-103214.00' is out of range of the UInt64 type.
'          Unable to parse '-103214,00'.
'          '104561.1' is out of range of the UInt64 type.
'          Unable to parse '104561,1'.
'    Parsing strings using the French (France) culture
'       Style: Integer
'          Converted '170209' to 170209.
'          Unable to parse '+170209.0'.
'          Unable to parse '+170209,0'.
'          Unable to parse '-103214.00'.
'          Unable to parse '-103214,00'.
'          Unable to parse '104561.1'.
'          Unable to parse '104561,1'.
'       Style: Integer, AllowDecimalPoint
'          Converted '170209' to 170209.
'          Unable to parse '+170209.0'.
'          Converted '+170209,0' to 170209.
'          Unable to parse '-103214.00'.
'          '-103214,00' is out of range of the UInt64 type.
'          Unable to parse '104561.1'.
'          '104561,1' is out of range of the UInt64 type.

注解

style参数定义在参数中允许的样式元素 (如空格或正号或负号符号) ,以便 s 分析操作成功。The style parameter defines the style elements (such as white space or the positive or negative sign symbol) that are allowed in the s parameter for the parse operation to succeed. 它必须是枚举中的位标志的组合 NumberStylesIt must be a combination of bit flags from the NumberStyles enumeration.

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

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

方括号 ([ and ]) 中的元素是可选的。Elements in square brackets ([ and ]) are optional. 如果 style 包含 NumberStyles.AllowHexSpecifier ,则 s 参数可能包括以下元素:If style includes NumberStyles.AllowHexSpecifier, the s parameter may include the following elements:

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

下表对每个元素进行了描述。The following table describes each element.

元素Element 说明Description
wsws 可选空白。Optional white space. 如果包含标志,则空格可以出现在的开头 s style NumberStyles.AllowLeadingWhite ,如果包含标志,则可以在的末尾出现空白 s style NumberStyles.AllowTrailingWhiteWhite space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
$ 区域性特定的货币符号。A culture-specific currency symbol. 其在字符串中的位置由参数的 CurrencyPositivePattern 方法返回的对象的属性定义 NumberFormatInfo GetFormat providerIts position in the string is defined by the CurrencyPositivePattern property of the NumberFormatInfo object that is returned by the GetFormat method of the provider parameter. s如果包含标志,则货币符号可以出现在中 style NumberStyles.AllowCurrencySymbolThe currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
signsign 一个可选的符号。An optional sign. OverflowException如果 s 包含一个负号并表示一个非零数字,则 (方法将引发。如果包含标志,则该方法将引发 s ,如果包含 style NumberStyles.AllowLeadingSign 标志,则它可以 s style NumberStyles.AllowTrailingSign 出现在的结尾 ) 。(The method throws an OverflowException if s includes a negative sign and represents a non-zero number.) The sign can appear at the beginning of s if style includes the NumberStyles.AllowLeadingSign flag, and it can appear the end of s if style includes the NumberStyles.AllowTrailingSign flag. 如果包含标志,则可以在中使用括号 s 来表示负值 style NumberStyles.AllowParenthesesParentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.
位数digits 介于0到9之间的一系列数字。A sequence of digits from 0 through 9.
.. 区域性特定的小数点符号。A culture-specific decimal point symbol. s如果包含标志,则当前区域性的小数点符号可以出现在中 style NumberStyles.AllowDecimalPointThe current culture's decimal point symbol can appear in s if style includes the NumberStyles.AllowDecimalPoint flag.
fractional_digitsfractional_digits 如果包含标志,则为数字0-9 的一个或多个匹配项 style NumberStyles.AllowExponent ,如果未出现,则为数字0的一个或多个匹配项。One or more occurrences of the digit 0-9 if style includes the NumberStyles.AllowExponent flag, or one or more occurrences of the digit 0 if it does not. s仅当包含标志时,小数位才能出现在中 style NumberStyles.AllowDecimalPointFractional digits can appear in s only if style includes the NumberStyles.AllowDecimalPoint flag.
EE "E" 或 "E" 字符,指示以指数 (科学) 记数法表示的值。The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation. s如果包含标志,则参数可以表示指数表示法中的数字 style NumberStyles.AllowExponentThe s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.
exponential_digitsexponential_digits 介于0到9之间的一系列数字。A sequence of digits from 0 through 9. s如果包含标志,则参数可以表示指数表示法中的数字 style NumberStyles.AllowExponentThe s 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.

备注

s无论参数的值如何,分析操作都将忽略中任何终止的 NUL (U + 0000) 字符 styleAny 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 decimal 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 this input string. 下表指示各个成员如何 NumberStyles 影响可能出现在中的元素 sThe following table indicates how individual NumberStyles members affect the elements that may be present in s.

非复合 NumberStylesNon-composite NumberStyles values 中允许的元素 s 以及数字Elements permitted in s in addition to digits
NumberStyles.None 仅十进制数字。Decimal digits only.
NumberStyles.AllowDecimalPoint 小数点 () 和fractional_digits 元素。The decimal point (.) and fractional_digits elements. 但是,如果样式不包含 NumberStyles.AllowExponent 标志,则 fractional_digits 必须仅包含一个或多个0位数; 否则, OverflowException 将引发。However, if style does not include the NumberStyles.AllowExponent flag, fractional_digits must consist of only one or more 0 digits; otherwise, an OverflowException is thrown.
NumberStyles.AllowExponent "E" 或 "E" 字符(指示指数表示法)以及 exponential_digitsThe "e" or "E" character, which indicates exponential notation, along with exponential_digits.
NumberStyles.AllowLeadingWhite 开头的 ws 元素 sThe ws element at the beginning of s.
NumberStyles.AllowTrailingWhite 末尾处的 ws 元素 sThe ws element at the end of s.
NumberStyles.AllowLeadingSign 数字之前的符号。A sign before digits.
NumberStyles.AllowTrailingSign 数字后的符号。A sign after digits.
NumberStyles.AllowParentheses 用于指示负值的 位数 前后的括号。Parentheses before and after digits to indicate a negative value.
NumberStyles.AllowThousands 组分隔符 () 元素。The group separator (,) element.
NumberStyles.AllowCurrencySymbol 货币 ($) 元素。The currency ($) element.

如果 NumberStyles.AllowHexSpecifier 使用标志,则 s 必须是十六进制值。If the NumberStyles.AllowHexSpecifier flag is used, s must be a hexadecimal value. 有效的十六进制字符为0-9、A-f 和 a-f。Valid hexadecimal characters are 0-9, A-F, and a-f. 前缀(如 "0x")不受支持,导致分析操作失败。A prefix such as "0x" is not supported and causes the parse operation to fail. 可以与之组合的其他标志只能是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteThe only other flags that can be combined with it are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. 枚举 (NumberStyles 包含复合数字样式, NumberStyles.HexNumber 其中包含两个空白标志。 ) (The NumberStyles enumeration includes a composite number style, NumberStyles.HexNumber, that includes both white-space flags.)

备注

如果 s 参数是十六进制数的字符串表示形式,则它的前面不能有任何修饰 (例如 0x 或) ,它将 &h 其视为十六进制数。If the s parameter 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 parse operation to throw an exception.

provider参数是一个 IFormatProvider 实现,其 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关格式的区域性特定信息 sThe provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of s. 使用 provider 参数为分析操作提供自定义格式设置信息的方法有三种:There are three ways to use the provider parameter to supply custom formatting information to the parse operation:

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

另请参阅

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

重要

此 API 不符合 CLS。

将指定样式和区域性特定格式的数字的范围表示形式转换为其等效的 64 位无符号整数。Converts the span representation of a number in a specified style and culture-specific format to its 64-bit unsigned integer equivalent.

public static ulong Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
[System.CLSCompliant(false)]
public static ulong Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
[System.CLSCompliant(false)]
public static ulong Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> uint64
[<System.CLSCompliant(false)>]
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> uint64
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As ULong

参数

s
ReadOnlySpan<Char>

一个范围,包含表示要转换的数字的字符。A span containing the characters that represent the number to convert. 该范围使用由 style 参数指定的样式来进行解释。The span is interpreted by using the style specified by the style parameter.

style
NumberStyles

枚举值的按位组合,用于指示可出现在 s 中的样式元素。A bitwise combination of enumeration values that indicates the style elements that can be present in s. 要指定的一个典型值为 IntegerA typical value to specify is Integer.

provider
IFormatProvider

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

返回

UInt64

s 中指定的数字等效的 64 位无符号整数。A 64-bit unsigned integer equivalent to the number specified in s.

属性

Parse(String, NumberStyles)

重要

此 API 不符合 CLS。

符合 CLS 的替代方案
System.Decimal.Parse(String)

将指定样式的数字的字符串表示形式转换为它的等效 64 位无符号整数。Converts the string representation of a number in a specified style to its 64-bit unsigned integer equivalent.

public:
 static System::UInt64 Parse(System::String ^ s, System::Globalization::NumberStyles style);
public static ulong Parse (string s, System.Globalization.NumberStyles style);
[System.CLSCompliant(false)]
public static ulong Parse (string s, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> uint64
[<System.CLSCompliant(false)>]
static member Parse : string * System.Globalization.NumberStyles -> uint64
Public Shared Function Parse (s As String, style As NumberStyles) As ULong

参数

s
String

表示要转换的数字的字符串。A string that represents the number to convert. 该字符串使用由 style 参数指定的样式来进行解释。The string is interpreted by using the style specified by the style parameter.

style
NumberStyles

枚举值的按位组合,这些枚举值指定 s 所允许的格式。A bitwise combination of the enumeration values that specifies the permitted format of s. 要指定的一个典型值为 IntegerA typical value to specify is Integer.

返回

UInt64

s 中指定的数字等效的 64 位无符号整数。A 64-bit unsigned integer equivalent to the number specified in s.

属性

例外

s 参数为 nullThe s parameter is null.

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

- 或 --or- style 不是 AllowHexSpecifierHexNumber 值的组合。style is not a combination of AllowHexSpecifier and HexNumber values.

参数 s 的格式不符合 styleThe s parameter is not in a format compliant with style.

参数 s 表示一个小于 MinValue 或大于 MaxValue 的数字。The s parameter represents a number less than MinValue or greater than MaxValue.

- 或 --or- s 包含非零的小数位。s includes non-zero, fractional digits.

示例

下面的示例尝试使用多个值分析字符串数组中的每个元素 NumberStylesThe following example tries to parse each element in a string array by using a number of NumberStyles values.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] values= { " 214309 ", "1,064,181", "(0)", "10241+", " + 21499 ", 
                         " +21499 ", "122153.00", "1e03ff", "91300.0e-2" };
      NumberStyles whitespace =  NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite;
      NumberStyles[] styles= { NumberStyles.None, whitespace, 
                               NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingSign | whitespace, 
                               NumberStyles.AllowThousands | NumberStyles.AllowCurrencySymbol, 
                               NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint };

      // Attempt to convert each number using each style combination.
      foreach (string value in values)
      {
         Console.WriteLine("Attempting to convert '{0}':", value);
         foreach (NumberStyles style in styles)
         {
            try {
               ulong number = UInt64.Parse(value, style);
               Console.WriteLine("   {0}: {1}", style, number);
            }   
            catch (FormatException) {
               Console.WriteLine("   {0}: Bad Format", style);
            }   
            catch (OverflowException)
            {
               Console.WriteLine("   {0}: Overflow", value);         
            }         
         }
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//    Attempting to convert ' 214309 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: 214309
//       Integer, AllowTrailingSign: 214309
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '1,064,181':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: 1064181
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '(0)':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '10241+':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: 10241
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert ' + 21499 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert ' +21499 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: 21499
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '122153.00':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: 122153
//    
//    Attempting to convert '1e03ff':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '91300.0e-2':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: 913
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim values() As String = { " 214309 ", "1,064,181", "(0)", "10241+", _
                                 " + 21499 ", " +21499 ", "122153.00", _
                                 "1e03ff", "91300.0e-2" }
      Dim whitespace As NumberStyles =  NumberStyles.AllowLeadingWhite Or NumberStyles.AllowTrailingWhite
      Dim styles() As NumberStyles = { NumberStyles.None, _
                                       whitespace, _
                                       NumberStyles.AllowLeadingSign Or NumberStyles.AllowTrailingSign Or whitespace, _
                                       NumberStyles.AllowThousands Or NumberStyles.AllowCurrencySymbol, _
                                       NumberStyles.AllowExponent Or NumberStyles.AllowDecimalPoint }

      ' Attempt to convert each number using each style combination.
      For Each value As String In values
         Console.WriteLine("Attempting to convert '{0}':", value)
         For Each style As NumberStyles In styles
            Try
               Dim number As ULong = UInt64.Parse(value, style)
               Console.WriteLine("   {0}: {1}", style, number)
            Catch e As FormatException
               Console.WriteLine("   {0}: Bad Format", style)
            Catch e As OverflowException
               Console.WriteLine("   {0}: Overflow", value)         
            End Try         
         Next
         Console.WriteLine()
      Next
   End Sub
End Module
' The example displays the following output:
'    Attempting to convert ' 214309 ':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: 214309
'       Integer, AllowTrailingSign: 214309
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert '1,064,181':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: 1064181
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert '(0)':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert '10241+':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: 10241
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert ' + 21499 ':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert ' +21499 ':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: 21499
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert '122153.00':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: 122153
'    
'    Attempting to convert '1e03ff':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert '91300.0e-2':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: 913

注解

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

根据的值 styles 参数可能包括以下元素:Depending on the value of style, the s 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]

方括号 ([ and ]) 中的元素是可选的。Elements in square brackets ([ and ]) are optional. 如果 style 包含 NumberStyles.AllowHexSpecifier ,则 s 参数可能包含以下元素:If style includes NumberStyles.AllowHexSpecifier, the s parameter may contain the following elements:

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

下表对每个元素进行了描述。The following table describes each element.

元素Element 说明Description
wsws 可选空白。Optional white space. 如果包含标志,则空格可以出现在的开头 s style NumberStyles.AllowLeadingWhite ,如果包含标志,则可以在的末尾出现空白 s style NumberStyles.AllowTrailingWhiteWhite space can appear at the start of s if style includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
$ 区域性特定的货币符号。A culture-specific currency symbol. 其在字符串中的位置由 NumberFormatInfo.CurrencyNegativePattern NumberFormatInfo.CurrencyPositivePattern 当前区域性的和属性定义。Its position in the string is defined by the NumberFormatInfo.CurrencyNegativePattern and NumberFormatInfo.CurrencyPositivePattern properties of the current culture. s如果包含标志,则当前区域性的货币符号可以出现在中 style NumberStyles.AllowCurrencySymbolThe current culture's currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
signsign 一个可选的符号。An optional sign. 如果包含标志,则符号可以出现在的开头 s style NumberStyles.AllowLeadingSign ,如果包含标志,则它可以出现在的结尾 s style NumberStyles.AllowTrailingSignThe sign can appear at the start of s if style includes the NumberStyles.AllowLeadingSign flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingSign flag. 如果包含标志,则可以在中使用括号 s 来表示负值 style NumberStyles.AllowParenthesesParentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag. 但负号符号只能与零一起使用;否则,该方法将引发 OverflowExceptionHowever, the negative sign symbol can be used only with zero; otherwise, the method throws an OverflowException.
位数digits

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. s如果包含标志,则当前区域性的组分隔符可以出现在中 style NumberStyles.AllowThousandsThe current culture's group separator can appear in s if style includes the NumberStyles.AllowThousands flag.
.. 区域性特定的小数点符号。A culture-specific decimal point symbol. s如果包含标志,则当前区域性的小数点符号可以出现在中 style NumberStyles.AllowDecimalPointThe current culture's decimal point symbol can appear in s if style includes the NumberStyles.AllowDecimalPoint flag. 只有数字0才能作为小数位出现,以便分析操作成功;如果 fractional_digits 包含任何其他数字, FormatException 则会引发。Only 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. s如果包含标志,则参数可以表示指数表示法中的数字 style NumberStyles.AllowExponentThe s 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.

备注

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

只包含数字的字符串 (与 NumberStyles.None 样式) 始终在类型范围内分析成功 UInt64A string with digits only (which corresponds to the NumberStyles.None style) always parses successfully if it is in the range of the UInt64 type. 大多数剩余 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 影响可能出现在中的元素 sThe following table indicates how individual NumberStyles members affect the elements that may be present in s.

NumberStylesNumberStyles value 中允许的元素 s 以及数字Elements permitted in s 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 开头的 ws 元素 sThe ws element at the start of s.
AllowTrailingWhite 末尾处的 ws 元素 sThe ws element at the end of s.
AllowLeadingSign 开头的 sign 元素 sThe sign element at the start of s.
AllowTrailingSign 末尾的 sign 元素 sThe sign element at the end of s.
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. 但是, s 不能表示十六进制数或以指数表示法表示的数字。However, s cannot represent a hexadecimal number or a number in exponential notation.
Float 开头或结尾处的ws元素,在 s 的开头处进行签名s 小数点 () 符号。 .The ws element at the start or end of s, sign at the start of s, and the decimal point (.) symbol. s参数还可以使用指数表示法。The s parameter can also use exponential notation.
Number wssign 、组分隔符 () 和小数点 () 元素。 .The ws, sign, group separator (,), and decimal point (.) elements.
Any 所有元素。All elements. 但是, s 不能表示十六进制数。However, s cannot represent a hexadecimal number.

与其他值不同, NumberStyles 它们允许但不要求在中存在特定样式元素 sNumberStyles.AllowHexSpecifier 样式值意味着中的单个数字字符 s 始终解释为十六进制字符。Unlike the other NumberStyles values, which allow for, but do not require, the presence of particular style elements in s, the NumberStyles.AllowHexSpecifier style value means that the individual numeric characters in s are always interpreted as hexadecimal characters. 有效的十六进制字符为0-9、A-f 和 a-f。Valid hexadecimal characters are 0-9, A-F, and a-f. 前缀(如 "0x")不受支持,导致分析操作失败。A prefix such as "0x" is not supported and causes the parse operation to fail. 只能与参数组合的其他标志 styleNumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteThe only other flags that can be combined with the style parameter are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. 枚举 (NumberStyles 包含复合数字样式, NumberStyles.HexNumber 其中包含两个空白标志。 ) (The NumberStyles enumeration includes a composite number style, NumberStyles.HexNumber, that includes both white-space flags.)

备注

如果 s 是十六进制数的字符串表示形式,则它的前面不能有任何修饰 (例如 0x&h) ,它将其视为十六进制数。If s 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.

s通过使用为 NumberFormatInfo 当前系统区域性初始化的对象中的格式设置信息来分析参数。The s 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)

重要

此 API 不符合 CLS。

符合 CLS 的替代方案
System.Decimal.Parse(String)

将数字的字符串表示形式转换为它的等效 64 位无符号整数。Converts the string representation of a number to its 64-bit unsigned integer equivalent.

public:
 static System::UInt64 Parse(System::String ^ s);
public static ulong Parse (string s);
[System.CLSCompliant(false)]
public static ulong Parse (string s);
static member Parse : string -> uint64
[<System.CLSCompliant(false)>]
static member Parse : string -> uint64
Public Shared Function Parse (s As String) As ULong

参数

s
String

表示要转换的数字的字符串。A string that represents the number to convert.

返回

UInt64

s 中包含的数字等效的 64 位无符号整数。A 64-bit unsigned integer equivalent to the number contained in s.

属性

例外

s 参数为 nullThe s parameter is null.

参数 s 的格式不正确。The s parameter is not in the correct format.

参数 s 表示一个小于 MinValue 或大于 MaxValue 的数字。The s parameter represents a number less than MinValue or greater than MaxValue.

示例

下面的示例使用 Parse 方法分析字符串值的数组。The following example uses the Parse method to parse an array of string values.

string[] values = { "+13230", "-0", "1,390,146", "$190,235,421,127",
                    "0xFA1B", "163042", "-10", "14065839182",
                    "16e07", "134985.0", "-12034" };
foreach (string value in values)
{
   try {
      ulong number = UInt64.Parse(value); 
      Console.WriteLine("{0} --> {1}", value, number);
   }
   catch (FormatException) {
      Console.WriteLine("{0}: Bad Format", value);
   }   
   catch (OverflowException) {
      Console.WriteLine("{0}: Overflow", value);   
   }  
}
// The example displays the following output:
//       +13230 --> 13230
//       -0 --> 0
//       1,390,146: Bad Format
//       $190,235,421,127: Bad Format
//       0xFA1B: Bad Format
//       163042 --> 163042
//       -10: Overflow
//       14065839182 --> 14065839182
//       16e07: Bad Format
//       134985.0: Bad Format
//       -12034: Overflow
Dim values() As String = { "+13230", "-0", "1,390,146", "$190,235,421,127", _
                           "0xFA1B", "163042", "-10", "14065839182", _
                           "16e07", "134985.0", "-12034" }
For Each value As String In values
   Try
      Dim number As ULong = UInt64.Parse(value) 
      Console.WriteLine("{0} --> {1}", value, number)
   Catch e As FormatException
      Console.WriteLine("{0}: Bad Format", value)
   Catch e As OverflowException
      Console.WriteLine("{0}: Overflow", value)   
   End Try  
Next
' The example displays the following output:
'       +13230 --> 13230
'       -0 --> 0
'       1,390,146: Bad Format
'       $190,235,421,127: Bad Format
'       0xFA1B: Bad Format
'       163042 --> 163042
'       -10: Overflow
'       14065839182 --> 14065839182
'       16e07: Bad Format
'       134985.0: Bad Format
'       -12034: Overflow

注解

s参数应为以下形式的数字的字符串表示形式。The s 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.NegativeSign NumberFormatInfo.PositiveSign 当前区域性的和属性确定。Valid sign characters are determined by the NumberFormatInfo.NegativeSign and NumberFormatInfo.PositiveSign properties of the current culture. 但负号符号只能与零一起使用;否则,该方法将引发 OverflowExceptionHowever, the negative sign symbol can be used only with zero; otherwise, the method throws an OverflowException.
位数digits 介于0到9之间的一系列数字。A sequence of digits ranging from 0 to 9. 任何前导零都将被忽略。Any leading zeros are ignored.

备注

由参数指定的字符串 s 使用样式来进行解释 NumberStyles.IntegerThe string specified by the s 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.

s通过使用为 System.Globalization.NumberFormatInfo 当前系统区域性初始化的对象中的格式设置信息来分析参数。The s 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(String, IFormatProvider)

重要

此 API 不符合 CLS。

符合 CLS 的替代方案
System.Decimal.Parse(String)

将指定区域性特定格式的数字的字符串表示形式转换为它的等效 64 位无符号整数。Converts the string representation of a number in a specified culture-specific format to its 64-bit unsigned integer equivalent.

public:
 static System::UInt64 Parse(System::String ^ s, IFormatProvider ^ provider);
public static ulong Parse (string s, IFormatProvider provider);
public static ulong Parse (string s, IFormatProvider? provider);
[System.CLSCompliant(false)]
public static ulong Parse (string s, IFormatProvider provider);
[System.CLSCompliant(false)]
public static ulong Parse (string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> uint64
[<System.CLSCompliant(false)>]
static member Parse : string * IFormatProvider -> uint64
Public Shared Function Parse (s As String, provider As IFormatProvider) As ULong

参数

s
String

表示要转换的数字的字符串。A string that represents the number to convert.

provider
IFormatProvider

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

返回

UInt64

s 中指定的数字等效的 64 位无符号整数。A 64-bit unsigned integer equivalent to the number specified in s.

属性

例外

s 参数为 nullThe s parameter is null.

s 参数的样式不正确。The s parameter is not in the correct style.

参数 s 表示一个小于 MinValue 或大于 MaxValue 的数字。The s parameter represents a number less than MinValue or greater than MaxValue.

示例

下面的示例是 Web 窗体的按钮单击事件处理程序。The following example is the button click event handler of a Web form. 它使用属性返回的数组 HttpRequest.UserLanguages 来确定用户的区域设置。It uses the array returned by the HttpRequest.UserLanguages property to determine the user's locale. 然后,它将实例化 CultureInfo 对应于该区域设置的对象。It then instantiates a CultureInfo object that corresponds to that locale. 然后,将 NumberFormatInfo 属于该对象的对象 CultureInfo 传递给 Parse(String, IFormatProvider) 方法,以将用户的输入转换为 UInt64 值。The NumberFormatInfo object that belongs to that CultureInfo object is then passed to the Parse(String, IFormatProvider) method to convert the user's input to a UInt64 value.

protected void OkToSingle_Click(object sender, EventArgs e)
{
   string locale;
   float number;
   CultureInfo culture;

   // Return if string is empty
   if (String.IsNullOrEmpty(this.inputNumber.Text))
      return;

   // Get locale of web request to determine possible format of number
   if (Request.UserLanguages.Length == 0)
      return;
   locale = Request.UserLanguages[0];
   if (String.IsNullOrEmpty(locale))
      return;

   // Instantiate CultureInfo object for the user's locale
   culture = new CultureInfo(locale);

   // Convert user input from a string to a number
   try
   {
      number = Single.Parse(this.inputNumber.Text, culture.NumberFormat);
   }
   catch (FormatException)
   {
      return;
   }
   catch (Exception)
   {
      return;
   }
   // Output number to label on web form
   this.outputNumber.Text = "Number is " + number.ToString();
}
Protected Sub OkToSingle_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToSingle.Click
   Dim locale As String
   Dim culture As CultureInfo
   Dim number As Single

   ' Return if string is empty
   If String.IsNullOrEmpty(Me.inputNumber.Text) Then Exit Sub

   ' Get locale of web request to determine possible format of number
   If Request.UserLanguages.Length = 0 Then Exit Sub
   locale = Request.UserLanguages(0)
   If String.IsNullOrEmpty(locale) Then Exit Sub

   ' Instantiate CultureInfo object for the user's locale
   culture = New CultureInfo(locale)

   ' Convert user input from a string to a number
   Try
      number = Single.Parse(Me.inputNumber.Text, culture.NumberFormat)
   Catch ex As FormatException
      Exit Sub
   Catch ex As OverflowException
      Exit Sub
   End Try

   ' Output number to label on web form
   Me.outputNumber.Text = "Number is " & number.ToString()
End Sub

注解

此方法的重载 Parse(String, IFormatProvider) 通常用于将可以多种方式设置格式的文本转换为 UInt64 值。This overload of the Parse(String, IFormatProvider) method is typically used to convert text that can be formatted in a variety of ways to a UInt64 value. 例如,它可用于将用户输入的文本转换为一个数字值。For example, it can be used to convert the text entered by a user into an HTML text box to a numeric value.

s参数包含以下格式的编号:The s parameter contains a number of the form:

ws[sign] 位数 [ws][ws][sign]digits[ws]

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

元素Element 说明Description
wsws 可选空白。Optional white space.
signsign 可选正号或负号(如果 s 表示值零)。An optional positive sign, or a negative sign if s represents the value zero.
位数digits 介于0到9之间的一系列数字。A sequence of digits ranging from 0 to 9.

使用样式解释 s 参数 NumberStyles.IntegerThe s parameter is interpreted using the NumberStyles.Integer style. 除了无符号整数值的十进制数字外,只允许使用前导和尾随空格以及前导符号。In addition to the unsigned integer value's decimal digits, only leading and trailing spaces along with a leading sign is allowed. (如果存在负号,则 s 必须表示值为零,否则方法将引发 OverflowException 。 ) 若要显式定义样式元素以及可在中存在的区域性特定格式设置信息 s ,请使用 Parse(String, NumberStyles, IFormatProvider) 方法。(If the negative sign is present, s must represent a value of zero, or the method throws an OverflowException.) To explicitly define the style elements together with the culture-specific formatting information that can be present in s, use the Parse(String, NumberStyles, IFormatProvider) method.

provider参数是一个 IFormatProvider 实现,其 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关格式的区域性特定信息 sThe provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of s. 使用 provider 参数为分析操作提供自定义格式设置信息的方法有三种:There are three ways to use the provider parameter to supply custom formatting information to the parse operation:

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

另请参阅

适用于