Single.Parse Single.Parse Single.Parse Single.Parse Method

定义

将数字的字符串表示形式转换为它的等效单精度浮点数。Converts the string representation of a number to its single-precision floating-point number equivalent.

重载

Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider)

将具有指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效单精度浮点数。Converts the string representation of a number in a specified style and culture-specific format to its single-precision floating-point number equivalent.

Parse(String, IFormatProvider) Parse(String, IFormatProvider) Parse(String, IFormatProvider) Parse(String, IFormatProvider)

将具有指定区域性特定格式的数字的字符串表示形式转换为它的等效单精度浮点数。Converts the string representation of a number in a specified culture-specific format to its single-precision floating-point number equivalent.

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
Parse(String) Parse(String) Parse(String) Parse(String)

将数字的字符串表示形式转换为它的等效单精度浮点数。Converts the string representation of a number to its single-precision floating-point number equivalent.

Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles)

将具有指定样式的数字的字符串表示形式转换为它的等效单精度浮点数。Converts the string representation of a number in a specified style to its single-precision floating-point number equivalent.

Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider)

将具有指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效单精度浮点数。Converts the string representation of a number in a specified style and culture-specific format to its single-precision floating-point number equivalent.

public:
 static float Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public static float Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> single
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Single

参数

s
String String String String

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

style
NumberStyles NumberStyles NumberStyles NumberStyles

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

provider
IFormatProvider IFormatProvider IFormatProvider IFormatProvider

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

返回

s 中指定的数值或符号等效的单精度浮点数字。A single-precision floating-point number equivalent to the numeric value or symbol specified in s.

异常

s 不表示一个数值。s does not represent a numeric value.

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

- 或 --or- styleAllowHexSpecifier 值。style is the AllowHexSpecifier value.

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

示例

下面的代码示例使用Parse(String, NumberStyles, IFormatProvider)方法,以便分析的字符串表示形式Single值。The following code example uses the Parse(String, NumberStyles, IFormatProvider) method to parse the string representations of Single values. 数组中的每个字符串进行分析使用 EN-US、 NL-NL 和一个自定义区域性的格式设置约定。Each string in an array is parsed using the formatting conventions of the en-US, nl-NL, and a custom culture. 自定义区域性为下划线 ("") 和两个作为其组大小定义其组分隔符符号。The custom culture defines its group separator symbol as the underscore ("") and its group size as two.

using System;
using System.Globalization;

public class Example
{
    public static void Main()
    {
      // Define an array of string values.
      string[] values = { " 987.654E-2", " 987,654E-2",  "(98765,43210)", 
                          "9,876,543.210", "9.876.543,210",  "98_76_54_32,19" };
      // Create a custom culture based on the invariant culture.
      CultureInfo ci = new CultureInfo("");
      ci.NumberFormat.NumberGroupSizes = new int[] { 2 };
      ci.NumberFormat.NumberGroupSeparator = "_";
      
      // Define an array of format providers.
      CultureInfo[] providers = { new CultureInfo("en-US"),
                                  new CultureInfo("nl-NL"), ci };       
      
      // Define an array of styles.
      NumberStyles[] styles = { NumberStyles.Currency, NumberStyles.Float };
      
      // Iterate the array of format providers.
      foreach (CultureInfo provider in providers)
      {
         Console.WriteLine("Parsing using the {0} culture:", 
                           provider.Name == String.Empty ? "Invariant" : provider.Name);
         // Parse each element in the array of string values.
         foreach (string value in values)
         {
            foreach (NumberStyles style in styles)
            {
               try {
                  float number = Single.Parse(value, style, provider);            
                  Console.WriteLine("   {0} ({1}) -> {2}", 
                                    value, style, number);
               }
               catch (FormatException) {
                  Console.WriteLine("   '{0}' is invalid using {1}.", value, style);
               }
               catch (OverflowException) {
                  Console.WriteLine("   '{0}' is out of the range of a Single.", value);
               } 
            }            
         }         
         Console.WriteLine();
      }
   }   
} 
// The example displays the following output:
//       Parsing using the en-US culture:
//       The format of // 987.654E-2// is invalid.
//       The format of // 987,654E-2// is invalid.
//       (98765,43210) (Currency) -> -9.876543E+09
//       9,876,543.210 (Currency) -> 9876543
//       The format of '9.876.543,210// is invalid.
//       The format of '98_76_54_32,19// is invalid.
//       
//       Parsing using the nl-NL culture:
//       The format of // 987.654E-2// is invalid.
//       The format of // 987,654E-2// is invalid.
//       (98765,43210) (Currency) -> -98765.43
//       The format of '9,876,543.210// is invalid.
//       9.876.543,210 (Currency) -> 9876543
//       The format of '98_76_54_32,19// is invalid.
//       
//       Parsing using the Invariant culture:
//       The format of // 987.654E-2// is invalid.
//       The format of // 987,654E-2// is invalid.
//       (98765,43210) (Currency) -> -9.876543E+09
//       9,876,543.210 (Currency) -> 9876543
//       The format of '9.876.543,210// is invalid.
//       98_76_54_32,19 (Currency) -> 9.876543E+09
Imports System.Globalization

Module Example
    Public Sub Main()
      ' Define an array of string values.
      Dim values() As String = { " 987.654E-2", " 987,654E-2", _
                                 "(98765,43210)", "9,876,543.210",  _
                                 "9.876.543,210",  "98_76_54_32,19" }
      ' Create a custom culture based on the invariant culture.
      Dim ci As New CultureInfo("")
      ci.NumberFormat.NumberGroupSizes = New Integer() { 2 }
      ci.NumberFormat.NumberGroupSeparator = "_"
      
      ' Define an array of format providers.
      Dim providers() As CultureInfo = { New CultureInfo("en-US"), _
                                             New CultureInfo("nl-NL"), ci }       
      
      ' Define an array of styles.
      Dim styles() As NumberStyles = { NumberStyles.Currency, NumberStyles.Float }
      
      ' Iterate the array of format providers.
      For Each provider As CultureInfo In providers
         Console.WriteLine("Parsing using the {0} culture:", _
                           If(provider.Name = String.Empty, "Invariant", provider.Name))
         ' Parse each element in the array of string values.
         For Each value As String In values
            For Each style As NumberStyles In styles
               Try
                  Dim number As Single = Single.Parse(value, style, provider)            
                  Console.WriteLine("   {0} ({1}) -> {2}", _
                                    value, style, number)
               Catch e As FormatException
                  Console.WriteLine("   '{0}' is invalid using {1}.", value, style)            
               Catch e As OverflowException
                  Console.WriteLine("   '{0}' is out of the range of a Single.", value)
               End Try 
            Next            
         Next         
         Console.WriteLine()
      Next
   End Sub   
End Module 
' The example displays the following output:
'       Parsing using the en-US culture:
'          ' 987.654E-2' is invalid using Currency.
'           987.654E-2 (Float) -> 9.87654
'          ' 987,654E-2' is invalid using Currency.
'          ' 987,654E-2' is invalid using Float.
'          (98765,43210) (Currency) -> -9.876543E+09
'          '(98765,43210)' is invalid using Float.
'          9,876,543.210 (Currency) -> 9876543
'          '9,876,543.210' is invalid using Float.
'          '9.876.543,210' is invalid using Currency.
'          '9.876.543,210' is invalid using Float.
'          '98_76_54_32,19' is invalid using Currency.
'          '98_76_54_32,19' is invalid using Float.
'       
'       Parsing using the nl-NL culture:
'          ' 987.654E-2' is invalid using Currency.
'          ' 987.654E-2' is invalid using Float.
'          ' 987,654E-2' is invalid using Currency.
'           987,654E-2 (Float) -> 9.87654
'          (98765,43210) (Currency) -> -98765.43
'          '(98765,43210)' is invalid using Float.
'          '9,876,543.210' is invalid using Currency.
'          '9,876,543.210' is invalid using Float.
'          9.876.543,210 (Currency) -> 9876543
'          '9.876.543,210' is invalid using Float.
'          '98_76_54_32,19' is invalid using Currency.
'          '98_76_54_32,19' is invalid using Float.
'       
'       Parsing using the Invariant culture:
'          ' 987.654E-2' is invalid using Currency.
'           987.654E-2 (Float) -> 9.87654
'          ' 987,654E-2' is invalid using Currency.
'          ' 987,654E-2' is invalid using Float.
'          (98765,43210) (Currency) -> -9.876543E+09
'          '(98765,43210)' is invalid using Float.
'          9,876,543.210 (Currency) -> 9876543
'          '9,876,543.210' is invalid using Float.
'          '9.876.543,210' is invalid using Currency.
'          '9.876.543,210' is invalid using Float.
'          98_76_54_32,19 (Currency) -> 9.876543E+09
'          '98_76_54_32,19' is invalid using Float.

注解

style参数定义的样式元素 (如空白、 千位分隔符和货币符号) 中允许s分析操作成功的参数。The style parameter defines the style elements (such as white space, thousands separators, and currency symbols) that are allowed in the s parameter for the parse operation to succeed. 它必须是从位标志的组合NumberStyles枚举。It must be a combination of bit flags from the NumberStyles enumeration. 以下NumberStyles不支持成员:The following NumberStyles members are not supported:

s参数可以包含NumberFormatInfo.PositiveInfinitySymbolNumberFormatInfo.NegativeInfinitySymbol,或NumberFormatInfo.NaNSymbol为指定的区域性providerThe s parameter can contain NumberFormatInfo.PositiveInfinitySymbol, NumberFormatInfo.NegativeInfinitySymbol, or NumberFormatInfo.NaNSymbol for the culture specified by provider. 具体取决于值style,它还可以采用以下形式:Depending on the value of style, it can also take the form:

[ws] [$] [sign][integral-digits,]integral-digits[.[fractional-digits]][E[sign]exponential-digits][ws][ws] [$] [sign][integral-digits,]integral-digits[.[fractional-digits]][E[sign]exponential-digits][ws]

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

元素Element 描述Description
wsws 一系列空白字符。A series of white-space characters. 空白可以出现在开头s如果style包括NumberStyles.AllowLeadingWhite标志,它可以出现在末尾s如果style包括NumberStyles.AllowTrailingWhite标志。White 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. 在字符串中的位置由NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern当前区域性的属性。Its position in the string is defined by the NumberFormatInfo.CurrencyNegativePattern and NumberFormatInfo.CurrencyPositivePattern properties of the current culture. 当前区域性的货币符号可以出现在s如果style包括NumberStyles.AllowCurrencySymbol标志。The current culture's currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
signsign 负号符号 (-) 或正号符号 (+)。A negative sign symbol (-) or a positive sign symbol (+). 符号可以显示在开头s如果style包括NumberStyles.AllowLeadingSign标志,它可以出现在末尾s如果style包括NumberStyles.AllowTrailingSign标志。The sign can appear at the beginning 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.AllowParentheses标志。Parentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.
integral-digitsintegral-digits 一系列的位范围从 0 到 9 的指定数目的整数部分。A series of digits ranging from 0 to 9 that specify the integral part of the number. 整型数字元素可以是当字符串包含小数位数元素。The integral-digits element can be absent if the string contains the fractional-digits element.
,, 特定于区域性的组分隔符。A culture-specific group separator. 当前区域性的组分隔符符号可以出现在s如果style包括NumberStyles.AllowThousands标志The current culture's group separator symbol can appear in s if style includes the NumberStyles.AllowThousands flag
.. 特定于区域性的小数点符号。A culture-specific decimal point symbol. 当前区域性的小数点符号可以出现在s如果style包括NumberStyles.AllowDecimalPoint标志。The current culture's decimal point symbol can appear in s if style includes the NumberStyles.AllowDecimalPoint flag.
fractional-digitsfractional-digits 一系列的位范围从 0 到 9 的指定数字的小数部分。A series of digits ranging from 0 to 9 that specify the fractional part of the number. 在中出现的小数位s如果style包括NumberStyles.AllowDecimalPoint标志。Fractional digits can appear in s 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.AllowExponent标志。The s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.
exponential-digitsexponential-digits 一系列的范围从 0 到 9 的指定指数的位数。A series of digits ranging from 0 to 9 that specify an exponent.

备注

中的任何终止 NUL (U + 0000) 字符s在分析操作,而不考虑的值,将忽略style参数。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. 剩余System.Globalization.NumberStyles成员控件可能会显示,但不是要求是存在于输入字符串中的元素。The remaining System.Globalization.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 flags affect the elements that may be present in s.

NumberStyles 值NumberStyles value 元素中允许使用s除数字之外Elements permitted in s in addition to digits
None 整型数字仅元素。The integral-digits element only.
AllowDecimalPoint 小数点 () 和小数位数元素。The decimal point (.) and fractional-digits elements.
AllowExponent "E"或者"E"字符,它指示指数记数法。The "e" or "E" character, which indicates exponential notation. 此标志本身支持窗体的值位数E数字; 其他标志需要为成功分析此类元素为正或负号和小数点符号的字符串。This flag by itself supports values in the form digitsEdigits; additional flags are needed to successfully parse strings with such elements as positive or negative signs and decimal point symbols.
AllowLeadingWhite Ws开头的元素sThe ws element at the beginning of s.
AllowTrailingWhite Ws结尾的元素sThe ws element at the end of s.
AllowLeadingSign 符号开头的元素sThe sign element at the beginning of s.
AllowTrailingSign 符号结尾的元素sThe sign element at the end of s.
AllowParentheses 登录形式的括号内包含的数字值的元素。The sign element in the form of parentheses enclosing the numeric value.
AllowThousands 千位分隔符 (,) 元素。The thousands 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 beginning or end of s, sign at the beginning of s, and the decimal point (.) symbol. s参数还可以使用指数记数法。The s parameter can also use exponential notation.
Number wssign,千位分隔符 (,) 和小数点 (.) 元素。The ws, sign, thousands separator (,) and decimal point (.) elements.
Any 所有元素。All elements. 但是,s不能表示十六进制数。However, s cannot represent a hexadecimal number.

provider参数是IFormatProvider实现。The provider parameter is an IFormatProvider implementation. GetFormat方法将返回NumberFormatInfo对象,它提供特定于区域性的格式信息valueIts 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:

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

如果分隔符时遇到s参数在分析操作,以及适用的货币或十进制数和组分隔符是相同的分析操作假定,分隔符是十进制分隔符,而不是一组分隔符。If a separator is encountered in the s parameter during a parse operation, and the applicable currency or number decimal and group separators are the same, the parse operation assumes that the separator is a decimal separator rather than a group separator. 有关分隔符的详细信息,请参阅CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparator,和NumberGroupSeparatorFor more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.

另请参阅

Parse(String, IFormatProvider) Parse(String, IFormatProvider) Parse(String, IFormatProvider) Parse(String, IFormatProvider)

将具有指定区域性特定格式的数字的字符串表示形式转换为它的等效单精度浮点数。Converts the string representation of a number in a specified culture-specific format to its single-precision floating-point number equivalent.

public:
 static float Parse(System::String ^ s, IFormatProvider ^ provider);
public static float Parse (string s, IFormatProvider provider);
static member Parse : string * IFormatProvider -> single
Public Shared Function Parse (s As String, provider As IFormatProvider) As Single

参数

s
String String String String

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

provider
IFormatProvider IFormatProvider IFormatProvider IFormatProvider

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

返回

s 中指定的数值或符号等效的单精度浮点数字。A single-precision floating-point number equivalent to the numeric value or symbol specified in s.

异常

s 不表示具有有效格式的数字。s does not represent a number in a valid format.

s 表示一个小于 MinValue 或大于 MaxValue 的数字。s 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)方法将用户输入转换为Single值。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 Single 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

注解

此重载通常用于将可以各种方式格式化文本转换Single值。This overload is typically used to convert text that can be formatted in a variety of ways to a Single value. 例如,它可以用于将输入到 HTML 文本中用户的数字值的文本转换。For example, it can be used to convert the text entered by a user into an HTML text box to a numeric value.

s参数被解释使用的组合NumberStyles.FloatNumberStyles.AllowThousands标志。The s parameter is interpreted using a combination of the NumberStyles.Float and NumberStyles.AllowThousands flags. s参数可以包含NumberFormatInfo.PositiveInfinitySymbolNumberFormatInfo.NegativeInfinitySymbol,或NumberFormatInfo.NaNSymbol为指定的区域性provider,也可以包含格式的字符串:The s parameter can contain NumberFormatInfo.PositiveInfinitySymbol, NumberFormatInfo.NegativeInfinitySymbol, or NumberFormatInfo.NaNSymbol for the culture specified by provider, or it can contain a string of the form:

[ws][sign]integral-digits[.[fractional-digits]][E[sign]exponential-digits][ws][ws][sign]integral-digits[.[fractional-digits]][E[sign]exponential-digits][ws]

可选元素括在方括号 ([和])。Optional elements are framed in square brackets ([ and ]). 包含字词"数字"的元素包含一系列的范围从 0 到 9 的数字字符。Elements that contain the term "digits" consist of a series of numeric characters ranging from 0 to 9.

元素Element 描述Description
wsws 一系列空白字符。A series of white-space characters.
signsign 负号符号 (-) 或正号符号 (+)。A negative sign symbol (-) or a positive sign symbol (+).
integral-digitsintegral-digits 一系列的位范围从 0 到 9 的指定数目的整数部分。A series of digits ranging from 0 to 9 that specify the integral part of the number. 运行的整型数字组分隔符符号可以对其进行分区。Runs of integral-digits can be partitioned by a group-separator symbol. 例如,在某些区域性中逗号 (,) 作为千位分隔符。For example, in some cultures a comma (,) separates groups of thousands. 整型数字元素可以是当字符串包含小数位数元素。The integral-digits element can be absent if the string contains the fractional-digits element.
.. 特定于区域性的小数点符号。A culture-specific decimal point symbol.
fractional-digitsfractional-digits 一系列的位范围从 0 到 9 的指定数字的小数部分。A series of digits ranging from 0 to 9 that specify the fractional part of the number.
EE "E"或者"E"字符,指示指数 (科学型) 表示法表示值。The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation.
exponential-digitsexponential-digits 一系列的范围从 0 到 9 的指定指数的位数。A series of digits ranging from 0 to 9 that specify an exponent.

有关数值格式的详细信息,请参阅格式设置类型主题。For more information about numeric formats, see the Formatting Types topic.

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方法并将其传递Type对象,表示NumberFormatInfo类型。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提供的格式有关的信息的对象s参数。The GetFormat method then returns the NumberFormatInfo object that provides information about the format of the s parameter. 有三种方法使用provider参数来提供到分析操作的自定义格式设置信息:There are three ways to use the provider parameter to supply custom formatting information to the parse operation:

如果providernullNumberFormatInfo无法获取,格式设置信息将使用在当前系统区域性。If provider is null or a NumberFormatInfo cannot be obtained, the formatting information for the current system culture is used.

如果分隔符时遇到s参数在分析操作,以及适用的货币或十进制数和组分隔符是相同的分析操作假定,分隔符是十进制分隔符,而不是一组分隔符。If a separator is encountered in the s parameter during a parse operation, and the applicable currency or number decimal and group separators are the same, the parse operation assumes that the separator is a decimal separator rather than a group separator. 有关分隔符的详细信息,请参阅CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparator,和NumberGroupSeparatorFor more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.

一些示例s包括"100"、"-123456789"、"123.45 e + 6"、"+ 500"、"5e2"、"3.1416","600","-。 123" 和"-Infinity"。Some examples of s are "100", "-123,456,789", "123.45e+6", "+500", "5e2", "3.1416", "600.", "-.123", and "-Infinity".

另请参阅

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

public static float Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowExponent | System.Globalization.NumberStyles.AllowLeadingSign | System.Globalization.NumberStyles.AllowLeadingWhite | System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.AllowTrailingWhite | System.Globalization.NumberStyles.Float | System.Globalization.NumberStyles.Integer, IFormatProvider provider = null);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> single
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowExponent | System.Globalization.NumberStyles.AllowLeadingSign | System.Globalization.NumberStyles.AllowLeadingWhite | System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.AllowTrailingWhite | System.Globalization.NumberStyles.Float | System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = null) As Single

参数

返回

Parse(String) Parse(String) Parse(String) Parse(String)

将数字的字符串表示形式转换为它的等效单精度浮点数。Converts the string representation of a number to its single-precision floating-point number equivalent.

public:
 static float Parse(System::String ^ s);
public static float Parse (string s);
static member Parse : string -> single
Public Shared Function Parse (s As String) As Single

参数

s
String String String String

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

返回

s 中指定的数值或符号等效的单精度浮点数字。A single-precision floating-point number equivalent to the numeric value or symbol specified in s.

异常

s 不表示具有有效格式的数字。s does not represent a number in a valid format.

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

示例

下面的示例使用Parse(String)方法,将一个字符串数组转换为等效Single值。The following example uses the Parse(String) method to convert an array of strings to equivalent Single values.

using System;

public class Example
{
   public static void Main()
   {
      string[] values = { "100", "(100)", "-123,456,789", "123.45e+6", 
                          "+500", "5e2", "3.1416", "600.", "-.123", 
                          "-Infinity", "-1E-16", Double.MaxValue.ToString(), 
                          Single.MinValue.ToString(), String.Empty };
      foreach (string value in values)
      {
         try {   
            float number = Single.Parse(value);
            Console.WriteLine("{0} -> {1}", value, number);
         }
         catch (FormatException) {
            Console.WriteLine("'{0}' is not in a valid format.", value);
         }
         catch (OverflowException) {
            Console.WriteLine("{0} is outside the range of a Single.", value);
         }
      }                                  
   }
}
// The example displays the following output:
//       100 -> 100
//       '(100)' is not in a valid format.
//       -123,456,789 -> -1.234568E+08
//       123.45e+6 -> 1.2345E+08
//       +500 -> 500
//       5e2 -> 500
//       3.1416 -> 3.1416
//       600. -> 600
//       -.123 -> -0.123
//       -Infinity -> -Infinity
//       -1E-16 -> -1E-16
//       1.79769313486232E+308 is outside the range of a Single.
//       -3.402823E+38 -> -3.402823E+38
//       '' is not in a valid format.
Module Example
   Public Sub Main()
      Dim values() As String = { "100", "(100)", "-123,456,789", "123.45e+6", _
                                 "+500", "5e2", "3.1416", "600.", "-.123", _
                                 "-Infinity", "-1E-16", Double.MaxValue.ToString(), _
                                 Single.MinValue.ToString(), String.Empty }
      For Each value As String In values
         Try   
            Dim number As Single = Single.Parse(value)
            Console.WriteLine("{0} -> {1}", value, number)
         Catch e As FormatException
            Console.WriteLine("'{0}' is not in a valid format.", value)
         Catch e As OverflowException
            Console.WriteLine("{0} is outside the range of a Single.", value)
         End Try
      Next                                  
   End Sub
End Module
' The example displays the following output:
'       100 -> 100
'       '(100)' is not in a valid format.
'       -123,456,789 -> -1.234568E+08
'       123.45e+6 -> 1.2345E+08
'       +500 -> 500
'       5e2 -> 500
'       3.1416 -> 3.1416
'       600. -> 600
'       -.123 -> -0.123
'       -Infinity -> -Infinity
'       -1E-16 -> -1E-16
'       1.79769313486232E+308 is outside the range of a Single.
'       -3.402823E+38 -> -3.402823E+38
'       '' is not in a valid format.

注解

s参数可以包含当前区域性PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol,或格式的字符串:The s parameter can contain the current culture's PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or a string of the form:

[ws][sign] [integral-digits[,]]integral-digits[.[fractional-digits]][e[sign]exponential-digits][ws][ws][sign] [integral-digits[,]]integral-digits[.[fractional-digits]][e[sign]exponential-digits][ws]

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

元素Element 描述Description
wsws 一系列的空白字符。A series of white space characters.
signsign 负号符号或正号符号。A negative sign symbol or a positive sign symbol. 有效的符号字符由NumberFormatInfo.NegativeSignNumberFormatInfo.PositiveSign当前区域性的属性。Valid sign characters are determined by the NumberFormatInfo.NegativeSign and NumberFormatInfo.PositiveSign properties of the current culture. 可以使用前导符号。Only a leading sign can be used.
integral-digitsintegral-digits 一系列的位范围从 0 到 9 的指定数目的整数部分。A series of digits ranging from 0 to 9 that specify the integral part of the number. 运行的整型数字组分隔符符号可以对其进行分区。Runs of integral-digits can be partitioned by a group-separator symbol. 例如,在某些区域性中逗号 (,) 作为千位分隔符。For example, in some cultures a comma (,) separates groups of thousands. 整型数字元素可以是当字符串包含小数位数元素。The integral-digits element can be absent if the string contains the fractional-digits element.
, 特定于区域性的千位分隔符号。A culture-specific thousands separator symbol.
.. 特定于区域性的小数点符号。A culture-specific decimal point symbol.
fractional-digitsfractional-digits 一系列的位范围从 0 到 9 的指定数字的小数部分。A series of digits ranging from 0 to 9 that specify the fractional part of the number.
EE "E"或者"E"字符,指示指数 (科学型) 表示法表示值。The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation.
exponential-digitsexponential-digits 一系列的范围从 0 到 9 的指定指数的位数。A series of digits ranging from 0 to 9 that specify an exponent.

s参数被解释使用的组合NumberStyles.FloatNumberStyles.AllowThousands标志。The s parameter is interpreted using a combination of the NumberStyles.Float and NumberStyles.AllowThousands flags. 这意味着该空白和千位分隔符允许但不是货币符号。This means that white space and thousands separators are allowed but currency symbols are not. 若要显式定义的元素 (如货币符号,千位分隔符和空格),可能会包含s,使用Parse(String, NumberStyles)方法重载。To explicitly define the elements (such as currency symbols, thousands separators, and white space) that can be present in s, use the Parse(String, NumberStyles) method overload.

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

通常,如果传递Parse方法中,通过调用创建的字符串ToString方法时,原始Single返回值。Ordinarily, if you pass the Parse method a string that is created by calling the ToString method, the original Single value is returned. 但是,由于丢失精度,值可能不相等。However, because of a loss of precision, the values may not be equal.

如果分隔符时遇到s参数在分析操作,以及适用的货币或十进制数和组分隔符是相同的分析操作假定,分隔符是十进制分隔符,而不是一组分隔符。If a separator is encountered in the s parameter during a parse operation, and the applicable currency or number decimal and group separators are the same, the parse operation assumes that the separator is a decimal separator rather than a group separator. 有关分隔符的详细信息,请参阅CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparator,和NumberGroupSeparatorFor more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.

另请参阅

Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles)

将具有指定样式的数字的字符串表示形式转换为它的等效单精度浮点数。Converts the string representation of a number in a specified style to its single-precision floating-point number equivalent.

public:
 static float Parse(System::String ^ s, System::Globalization::NumberStyles style);
public static float Parse (string s, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> single
Public Shared Function Parse (s As String, style As NumberStyles) As Single

参数

s
String String String String

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

style
NumberStyles NumberStyles NumberStyles NumberStyles

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

返回

与在 s 中指定的数值或符号等效的单精度浮点数字。A single-precision floating-point number that is equivalent to the numeric value or symbol specified in s.

异常

s 不是一个具有有效格式的数字。s is not a number in a valid format.

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

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

- 或 --or- style 包括 AllowHexSpecifier 值。style includes the AllowHexSpecifier value.

示例

下面的示例使用Parse(String, NumberStyles)方法,以便分析的字符串表示形式Single值。The following example uses the Parse(String, NumberStyles) method to parse the string representations of Single values. 该示例使用 EN-US 区域性的格式设置信息。The example uses formatting information for the en-US culture.

using System;
using System.Globalization;
using System.Threading;

public class ParseString
{
   public static void Main()
   {
      // Set current thread culture to en-US.
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
      
      string value;
      NumberStyles styles;
      
      // Parse a string in exponential notation with only the AllowExponent flag. 
      value = "-1.063E-02";
      styles = NumberStyles.AllowExponent;
      ShowNumericValue(value, styles);
      
      // Parse a string in exponential notation
      // with the AllowExponent and Number flags.
      styles = NumberStyles.AllowExponent | NumberStyles.Number;
      ShowNumericValue(value, styles);

      // Parse a currency value with leading and trailing white space, and
      // white space after the U.S. currency symbol.
      value = " $ 6,164.3299  ";
      styles = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
      ShowNumericValue(value, styles);
      
      // Parse negative value with thousands separator and decimal.
      value = "(4,320.64)";
      styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign |
               NumberStyles.Float; 
      ShowNumericValue(value, styles);
      
      styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign |
               NumberStyles.Float | NumberStyles.AllowThousands;
      ShowNumericValue(value, styles);
   }

   private static void ShowNumericValue(string value, NumberStyles styles)
   {
      Single number;
      try
      {
         number = Single.Parse(value, styles);
         Console.WriteLine("Converted '{0}' using {1} to {2}.", 
                           value, styles.ToString(), number);
      }
      catch (FormatException)
      {
         Console.WriteLine("Unable to parse '{0}' with styles {1}.", 
                           value, styles.ToString());
      }
      Console.WriteLine();                           
   }   
}
// The example displays the following output to the console:
//    Unable to parse '-1.063E-02' with styles AllowExponent.
//    
//    Converted '-1.063E-02' using AllowTrailingSign, AllowThousands, Float to -0.01063.
//    
//    Converted ' $ 6,164.3299  ' using Number, AllowCurrencySymbol to 6164.3299.
//    
//    Unable to parse '(4,320.64)' with styles AllowTrailingSign, AllowParentheses, Float.
//    
//    Converted '(4,320.64)' using AllowTrailingSign, AllowParentheses, AllowThousands, Float to -4320.64.   
Imports System.Globalization
Imports System.Threading

Module ParseStrings
   Public Sub Main()
      ' Set current thread culture to en-US.
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
            
      Dim value As String
      Dim styles As NumberStyles
      
      ' Parse a string in exponential notation with only the AllowExponent flag. 
      value = "-1.063E-02"
      styles = NumberStyles.AllowExponent
      ShowNumericValue(value, styles) 
      
      ' Parse a string in exponential notation
      ' with the AllowExponent and Number flags.
      styles = NumberStyles.AllowExponent Or NumberStyles.Number
      ShowNumericValue(value, styles)

      ' Parse a currency value with leading and trailing white space, and
      ' white space after the U.S. currency symbol.
      value = " $ 6,164.3299  "
      styles = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
      ShowNumericValue(value, styles)
      
      ' Parse negative value with thousands separator and decimal.
      value = "(4,320.64)"
      styles = NumberStyles.AllowParentheses Or NumberStyles.AllowTrailingSign _
               Or NumberStyles.Float 
      ShowNumericValue(value, styles)
      
      styles = NumberStyles.AllowParentheses Or NumberStyles.AllowTrailingSign _
               Or NumberStyles.Float Or NumberStyles.AllowThousands
      ShowNumericValue(value, styles)
   End Sub
   
   Private Sub ShowNumericValue(value As String, styles As NumberStyles)
      Dim number As Single
      Try
         number = Single.Parse(value, styles)
         Console.WriteLine("Converted '{0}' using {1} to {2}.", _
                           value, styles.ToString(), number)
      Catch e As FormatException
         Console.WriteLine("Unable to parse '{0}' with styles {1}.", _
                           value, styles.ToString())
      End Try
      Console.WriteLine()                           
   End Sub
End Module
' The example displays the following output to the console:
'    Unable to parse '-1.063E-02' with styles AllowExponent.
'    
'    Converted '-1.063E-02' using AllowTrailingSign, AllowThousands, Float to -0.01063.
'    
'    Converted ' $ 6,164.3299  ' using Number, AllowCurrencySymbol to 6164.3299.
'    
'    Unable to parse '(4,320.64)' with styles AllowTrailingSign, AllowParentheses, Float.
'    
'    Converted '(4,320.64)' using AllowTrailingSign, AllowParentheses, AllowThousands, Float to -4320.64.   

注解

style参数定义的样式元素 (如空白、 千位分隔符和货币符号) 中允许s分析操作成功的参数。The style parameter defines the style elements (such as white space, thousands separators, and currency symbols) that are allowed in the s parameter for the parse operation to succeed. 它必须是从位标志的组合NumberStyles枚举。It must be a combination of bit flags from the NumberStyles enumeration. 以下NumberStyles不支持成员:The following NumberStyles members are not supported:

s参数可以包含当前区域性PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbolThe s parameter can contain the current culture's PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol. 具体取决于值style,它还可以采用以下形式:Depending on the value of style, it can also take the form:

[ws][$][sign][integral-digits[,]]integral-digits[.[fractional-digits]][E[sign]exponential-digits][ws][ws][$][sign][integral-digits[,]]integral-digits[.[fractional-digits]][E[sign]exponential-digits][ws]

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

wsws
一系列空白字符。A series of white-space characters. 空白可以出现在开头s如果style包括NumberStyles.AllowLeadingWhite标志,它可以出现在末尾s如果style包括NumberStyles.AllowTrailingWhite标志。White 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. 在字符串中的位置由NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern当前区域性的属性。Its position in the string is defined by the NumberFormatInfo.CurrencyNegativePattern and NumberFormatInfo.CurrencyPositivePattern properties of the current culture. 当前区域性的货币符号可以出现在s如果style包括NumberStyles.AllowCurrencySymbol标志。The current culture's currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.

signsign
负号符号 (-) 或正号符号 (+)。A negative sign symbol (-) or a positive sign symbol (+). 符号可以显示在开头s如果style包括NumberStyles.AllowLeadingSign标志,它可以出现在末尾s如果style包括NumberStyles.AllowTrailingSign标志。The sign can appear at the beginning 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.AllowParentheses标志。Parentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.

integral-digitsintegral-digits
一系列的位范围从 0 到 9 的指定数目的整数部分。A series of digits ranging from 0 to 9 that specify the integral part of the number. 整型数字元素可以是当字符串包含小数位数元素。The integral-digits element can be absent if the string contains the fractional-digits element.

,,
特定于区域性的组分隔符。A culture-specific group separator. 当前区域性的组分隔符符号可以出现在s如果style包括NumberStyles.AllowThousands标志The current culture's group separator symbol can appear in s if style includes the NumberStyles.AllowThousands flag

..
特定于区域性的小数点符号。A culture-specific decimal point symbol. 当前区域性的小数点符号可以出现在s如果style包括NumberStyles.AllowDecimalPoint标志。The current culture's decimal point symbol can appear in s if style includes the NumberStyles.AllowDecimalPoint flag.

fractional-digitsfractional-digits
一系列的位范围从 0 到 9 的指定数字的小数部分。A series of digits ranging from 0 to 9 that specify the fractional part of the number. 在中出现的小数位s如果style包括NumberStyles.AllowDecimalPoint标志。Fractional digits can appear in s 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. value参数可以表示指数表示法的数字,如果style包括NumberStyles.AllowExponent标志。The value parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.

exponential-digitsexponential-digits
一系列的范围从 0 到 9 的指定指数的位数。A series of digits ranging from 0 to 9 that specify an exponent.

备注

中的任何终止 NUL (U + 0000) 字符s在分析操作,而不考虑的值,将忽略style参数。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. 剩余System.Globalization.NumberStyles成员控件可能会显示,但不是要求是存在于输入字符串中的元素。The remaining System.Globalization.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 flags affect the elements that may be present in s.

NumberStyles 值NumberStyles value 元素中允许使用s除数字之外Elements permitted in s in addition to digits
None 整型数字仅元素。The integral-digits element only.
AllowDecimalPoint 小数点 () 和小数位数元素。The decimal point (.) and fractional-digits elements.
AllowExponent "E"或者"E"字符,它指示指数记数法。The "e" or "E" character, which indicates exponential notation. 此标志本身支持窗体的值位数E数字; 其他标志需要为成功分析此类元素为正或负号和小数点符号的字符串。This flag by itself supports values in the form digitsEdigits; additional flags are needed to successfully parse strings with such elements as positive or negative signs and decimal point symbols.
AllowLeadingWhite Ws开头的元素sThe ws element at the beginning of s.
AllowTrailingWhite Ws结尾的元素sThe ws element at the end of s.
AllowLeadingSign 符号开头的元素sThe sign element at the beginning of s.
AllowTrailingSign 符号结尾的元素sThe sign element at the end of s.
AllowParentheses 登录形式的括号内包含的数字值的元素。The sign element in the form of parentheses enclosing the numeric value.
AllowThousands 千位分隔符 (,) 元素。The thousands 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 beginning or end of s, sign at the beginning of s, and the decimal point (.) symbol. s参数还可以使用指数记数法。The s parameter can also use exponential notation.
Number wssign,千位分隔符 (,) 和小数点 (.) 元素。The ws, sign, thousands separator (,) and decimal point (.) elements.
Any 所有元素。All elements. 但是,s不能表示十六进制数。However, s cannot represent a hexadecimal number.

一些示例s包括"100"、"-123456789"、"123.45 e + 6"、"+ 500"、"5e2"、"3.1416","600","-。 123" 和"-Infinity"。Some examples of s are "100", "-123,456,789", "123.45e+6", "+500", "5e2", "3.1416", "600.", "-.123", and "-Infinity".

s使用中的格式设置信息分析参数NumberFormatInfo对象,将初始化为当前系统区域性。The s parameter is parsed 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方法中,通过调用创建的字符串ToString方法时,原始Single返回值。Ordinarily, if you pass the Parse method a string that is created by calling the ToString method, the original Single value is returned. 但是,由于丢失精度,值可能不相等。However, because of a loss of precision, the values may not be equal.

如果分隔符时遇到s参数在分析操作,以及适用的货币或十进制数和组分隔符是相同的分析操作假定,分隔符是十进制分隔符,而不是一组分隔符。If a separator is encountered in the s parameter during a parse operation, and the applicable currency or number decimal and group separators are the same, the parse operation assumes that the separator is a decimal separator rather than a group separator. 有关分隔符的详细信息,请参阅CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparator,和NumberGroupSeparatorFor more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.

另请参阅

适用于