Single.TryParse Single.TryParse Single.TryParse Single.TryParse Method

定义

将数字的字符串表示形式转换为它的等效单精度浮点数。Converts the string representation of a number to its single-precision floating-point number equivalent. 一个指示转换是否成功的返回值。A return value indicates whether the conversion succeeded or failed.

重载

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Single) TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Single) TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Single) TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Single)
TryParse(String, Single) TryParse(String, Single) TryParse(String, Single) TryParse(String, Single)

将数字的字符串表示形式转换为它的等效单精度浮点数。Converts the string representation of a number to its single-precision floating-point number equivalent. 一个指示转换是否成功的返回值。A return value indicates whether the conversion succeeded or failed.

TryParse(ReadOnlySpan<Char>, Single) TryParse(ReadOnlySpan<Char>, Single) TryParse(ReadOnlySpan<Char>, Single) TryParse(ReadOnlySpan<Char>, Single)
TryParse(String, NumberStyles, IFormatProvider, Single) TryParse(String, NumberStyles, IFormatProvider, Single) TryParse(String, NumberStyles, IFormatProvider, Single) TryParse(String, NumberStyles, IFormatProvider, Single)

将具有指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效单精度浮点数。Converts the string representation of a number in a specified style and culture-specific format to its single-precision floating-point number equivalent. 一个指示转换是否成功的返回值。A return value indicates whether the conversion succeeded or failed.

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Single) TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Single) TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Single) TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Single)

public:
 static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] float % result);
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out float result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider *  -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As Single) As Boolean

参数

返回

TryParse(String, Single) TryParse(String, Single) TryParse(String, Single) TryParse(String, Single)

将数字的字符串表示形式转换为它的等效单精度浮点数。Converts the string representation of a number to its single-precision floating-point number equivalent. 一个指示转换是否成功的返回值。A return value indicates whether the conversion succeeded or failed.

public:
 static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] float % result);
public static bool TryParse (string s, out float result);
static member TryParse : string *  -> bool
Public Shared Function TryParse (s As String, ByRef result As Single) As Boolean

参数

s
String String String String

表示要转换的数字的字符串。A string representing a number to convert.

result
Single Single Single Single

当此方法返回时,如果转换成功,则包含与 s 所包含的数值或符号等效的单精度浮点数字;如果转换失败,则包含零。When this method returns, contains single-precision floating-point number equivalent to the numeric value or symbol contained in s, if the conversion succeeded, or zero if the conversion failed. 如果 s 参数为 nullEmpty、不是有效格式的数字,或者表示的数字小于 MinValue 或大于 MaxValue,则转换失败。The conversion fails if the s parameter is null or Empty, is not a number in a valid format, or represents a number less than MinValue or greater than MaxValue. 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。This parameter is passed uninitialized; any value originally supplied in result will be overwritten.

返回

如果 true 成功转换,则为 s;否则为 falsetrue if s was converted successfully; otherwise, false.

示例

下面的示例使用TryParse(String, Single)方法将转换为数字值的字符串表示Single值。The following example uses the TryParse(String, Single) method to convert the string representations of numeric values to Single values. 它假定该 EN-US 当前区域性。It assumes that en-US is the current culture.

string value;
float number;

// Parse a floating-point value with a thousands separator.
value = "1,643.57";
if (Single.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);      

// Parse a floating-point value with a currency symbol and a 
// thousands separator.
value = "$1,643.57";
if (Single.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);   

// Parse value in exponential notation.
value = "-1.643e6";
if (Single.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);   

// Parse a negative integer value.
value = "-168934617882109132";
if (Single.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);   
// The example displays the following output:
//       1643.57
//       Unable to parse '$1,643.57'.
//       -164300
//       -1.68934617882109E+17
Dim value As String
Dim number As Single

' Parse a floating-point value with a thousands separator.
value = "1,643.57"
If Single.TryParse(value, number) Then
   Console.WriteLine(number)
Else
   Console.WriteLine("Unable to parse '{0}'.", value)      
End If   

' Parse a floating-point value with a currency symbol and a 
' thousands separator.
value = "$1,643.57"
If Single.TryParse(value, number) Then
   Console.WriteLine(number)  
Else
   Console.WriteLine("Unable to parse '{0}'.", value)   
End If

' Parse value in exponential notation.
value = "-1.643e6"
If Single.TryParse(value, number)
   Console.WriteLine(number)
Else
   Console.WriteLine("Unable to parse '{0}'.", value)   
End If

' Parse a negative integer number.
value = "-168934617882109132"
If Single.TryParse(value, number)
   Console.WriteLine(number)
Else
   Console.WriteLine("Unable to parse '{0}'.", value)   
End If
' The example displays the following output:
'       1643.57
'       Unable to parse '$1,643.57'.
'       -1643000
'       -1.68934617882109E+17

注解

此重载不同于Single.Parse(String)通过返回一个布尔值,该值指示是否分析操作成功而不是返回的已分析的数字值的方法。This overload differs from the Single.Parse(String) method by returning a Boolean value that indicates whether the parse operation succeeded instead of returning the parsed numeric value. 它消除了需要使用异常处理来测试FormatException的事件中s无效,不能成功分析。It eliminates the need to use exception handling to test for a FormatException in the event that s is invalid and cannot be successfully parsed.

s参数可以包含PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol (字符串比较是区分大小写) 或格式的字符串:The s parameter can contain PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol (the string comparison is case-sensitive), 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]

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

元素Element 描述Description
wsws 一系列空白字符。A series of white-space characters.
signsign 负号或正号符号。A negative sign or positive sign symbol.
integral-digitsintegral-digits 一系列数字字符范围从 0 到 9 的指定数目的整数部分。A series of numeric characters ranging from 0 to 9 that specify the integral part of the number. 整型数字可以是不存在如果小数位数。Integral-digits can be absent if there are fractional-digits.
, 特定于区域性的组分隔符符号。A culture-specific group separator symbol.
.. 特定于区域性的小数点符号。A culture-specific decimal point symbol.
fractional-digitsfractional-digits 一系列数字字符范围从 0 到 9 的指定数字的小数部分。A series of numeric characters ranging from 0 to 9 that specify the fractional part of the number.
EE 一个大写或小写字符 e,用于指示指数 (科学型) 表示法。An uppercase or lowercase character 'e', that indicates exponential (scientific) notation.
exponential-digitsexponential-digits 一系列数字字符范围从 0 到 9,指定一个指数。A series of numeric characters 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,使用TryParse(String, NumberStyles, IFormatProvider, Single)方法重载。To explicitly define the elements (such as currency symbols, thousands separators, and white space) that can be present in s, use the TryParse(String, NumberStyles, IFormatProvider, Single) method overload.

s使用中的格式设置信息分析参数NumberFormatInfo对象,将初始化为当前系统区域性。The s parameter is parsed using the formatting information in a NumberFormatInfo object that is initialized for the current system culture. 有关详情,请参阅NumberFormatInfo.CurrentInfoFor more information, see NumberFormatInfo.CurrentInfo. 将字符串分析使用的格式信息的其他某个指定区域性,请使用TryParse(String, NumberStyles, IFormatProvider, Single)方法重载。To parse a string using the formatting information of some other specified culture, use the TryParse(String, NumberStyles, IFormatProvider, Single) method overload.

通常,如果传递Single.TryParse方法中,通过调用创建的字符串Single.ToString方法时,原始Single返回值。Ordinarily, if you pass the Single.TryParse method a string that is created by calling the Single.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.

另请参阅

TryParse(ReadOnlySpan<Char>, Single) TryParse(ReadOnlySpan<Char>, Single) TryParse(ReadOnlySpan<Char>, Single) TryParse(ReadOnlySpan<Char>, Single)

public:
 static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] float % result);
public static bool TryParse (ReadOnlySpan<char> s, out float result);
static member TryParse : ReadOnlySpan<char> *  -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As Single) As Boolean

参数

返回

TryParse(String, NumberStyles, IFormatProvider, Single) TryParse(String, NumberStyles, IFormatProvider, Single) TryParse(String, NumberStyles, IFormatProvider, Single) TryParse(String, NumberStyles, IFormatProvider, Single)

将具有指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效单精度浮点数。Converts the string representation of a number in a specified style and culture-specific format to its single-precision floating-point number equivalent. 一个指示转换是否成功的返回值。A return value indicates whether the conversion succeeded or failed.

public:
 static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] float % result);
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out float result);
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider *  -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As Single) As Boolean

参数

s
String String String String

表示要转换的数字的字符串。A string representing a number to convert.

style
NumberStyles NumberStyles NumberStyles NumberStyles

枚举值的一个按位组合,指示 s 所允许的格式。A bitwise combination of enumeration values that indicates the permitted format of 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.

result
Single Single Single Single

当此方法返回时,如果转换成功,则包含与 s 所包含的数值或符号等效的单精度浮点数字;如果转换失败,则包含零。When this method returns, contains the single-precision floating-point number equivalent to the numeric value or symbol contained in s, if the conversion succeeded, or zero if the conversion failed. 如果 s 参数为 nullEmpty、格式不符合 style、表示的数字小于MinValue 或大于 MaxValue,或者 style 不是 NumberStyles 枚举的常数的有效组合,则转换失败。The conversion fails if the s parameter is null or Empty, is not in a format compliant with style, represents a number less than MinValue or greater than MaxValue, or if style is not a valid combination of NumberStyles enumerated constants. 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。This parameter is passed uninitialized; any value originally supplied in result will be overwritten.

返回

如果 true 成功转换,则为 s;否则为 falsetrue if s was converted successfully; otherwise, false.

异常

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

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

示例

下面的示例演示如何将Single.TryParse(String, NumberStyles, IFormatProvider, Single)方法来分析具有特定样式,并且使用特定区域性的约定进行格式化的数字的字符串表示形式。The following example demonstrates the use of the Single.TryParse(String, NumberStyles, IFormatProvider, Single) method to parse the string representation of numbers that have a particular style and are formatted using the conventions of a particular culture.

string value;
System.Globalization.NumberStyles style;
System.Globalization.CultureInfo culture;
float number;

// Parse currency value using en-GB culture.
value = "�1,097.63";
style = System.Globalization.NumberStyles.Number | 
        System.Globalization.NumberStyles.AllowCurrencySymbol;
culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
if (Single.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);

value = "1345,978";
style = System.Globalization.NumberStyles.AllowDecimalPoint;
culture = System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR");
if (Single.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);

value = "1.345,978";
style = System.Globalization.NumberStyles.AllowDecimalPoint | 
        System.Globalization.NumberStyles.AllowThousands;
culture = System.Globalization.CultureInfo.CreateSpecificCulture("es-ES");
if (Single.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);

value = "1 345,978";
if (Single.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);
// The example displays the following output:
//       Converted '�1,097.63' to 1097.63.
//       Converted '1345,978' to 1345.978.
//       Converted '1.345,978' to 1345.978.
//       Unable to convert '1 345,978'.
Dim value As String
Dim style As System.Globalization.NumberStyles
Dim culture As System.Globalization.CultureInfo
Dim number As Single

' Parse currency value using en-GB culture.
value = "�1,097.63"
style = System.Globalization.NumberStyles.Number Or _
        System.Globalization.NumberStyles.AllowCurrencySymbol
culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB")
If Single.TryParse(value, style, culture, number) Then
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
   Console.WriteLine("Unable to convert '{0}'.", value)
End If    

value = "1345,978"
style = System.Globalization.NumberStyles.AllowDecimalPoint
culture = System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR")
If Single.TryParse(value, style, culture, number) Then
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
   Console.WriteLine("Unable to convert '{0}'.", value)
End If    

value = "1.345,978"
style = System.Globalization.NumberStyles.AllowDecimalPoint Or _
        System.Globalization.NumberStyles.AllowThousands
culture = System.Globalization.CultureInfo.CreateSpecificCulture("es-ES")
If Single.TryParse(value, style, culture, number) Then
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
   Console.WriteLine("Unable to convert '{0}'.", value)
End If    

value = "1 345,978"
If Single.TryParse(value, style, culture, number) Then
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
   Console.WriteLine("Unable to convert '{0}'.", value)
End If    
' The example displays the following output:
'       Converted '�1,097.63' to 1097.63.
'       Converted '1345,978' to 1345.978.
'       Converted '1.345,978' to 1345.978.
'       Unable to convert '1 345,978'.

注解

此重载不同于Parse(String, NumberStyles, IFormatProvider)通过返回一个布尔值,该值指示是否分析操作成功而不是返回的已分析的数字值的方法。This overload differs from the Parse(String, NumberStyles, IFormatProvider) method by returning a Boolean value that indicates whether the parse operation succeeded instead of returning the parsed numeric value. 它消除了需要使用异常处理来测试FormatException的事件中s无效,不能成功分析。It eliminates the need to use exception handling to test for a FormatException in the event that s is invalid and cannot be successfully parsed.

style参数定义的允许的格式s分析操作成功的参数。The style parameter defines the allowable format of 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参数可以包含PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol由指定的区域性providerThe s parameter can contain PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol for the culture indicated by provider. 此外,具体取决于值style,则s参数可能包含以下元素:In addition, depending on the value of style, the s parameter may include the following elements:

[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 可选的空白区域。Optional white space. 空白可以出现在开头s如果style包括NumberStyles.AllowLeadingWhite标志。White space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag. 它可以显示在末尾s如果style包括NumberStyles.AllowTrailingWhite标志。It can appear at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
$ 特定于区域性的货币符号。A culture-specific currency symbol. 在字符串中的位置由NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern的属性NumberFormatInfo返回的对象IFormatProvider.GetFormat方法provider参数。Its position in the string is defined by the NumberFormatInfo.CurrencyNegativePattern or NumberFormatInfo.CurrencyPositivePattern properties of the NumberFormatInfo object returned by the IFormatProvider.GetFormat method of the provider parameter. 货币符号可以出现在s如果style包括NumberStyles.AllowCurrencySymbol标志。The currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
signsign 一个可选符号后。An optional sign. 符号可以显示在开头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. 整型数字可以是不存在如果小数位数。Integral-digits can be absent if there are fractional-digits.
, 特定于区域性的千位分隔符号。A culture-specific thousands separator symbol. 当前区域性的千位分隔符符号可以出现在s如果style包括NumberStyles.AllowThousands标志。The current culture's thousands 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 字符,指示s可以表示的数字使用指数记数法。The e or E character, which indicates that s can represent a number using exponential notation. s参数可以表示指数表示法的数字,如果样式包含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 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 . 小数位数元素。and fractional-digits elements.
AllowExponent s参数还可以使用指数记数法。The s parameter can also use exponential notation. 此标志本身支持窗体的值整型数字E指数数字; 其他标志需要为成功分析字符串中与作为此类元素的指数表示法正或负号和小数点符号。This flag by itself supports values in the form integral-digitsEexponential-digits; additional flags are needed to successfully parse strings in exponential notation 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 , element.
AllowCurrencySymbol $ 元素。The $ element.
Currency 全部。All. s参数不能表示十六进制数或指数表示法的数字。The s parameter 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 . 符号。symbol. s参数还可以使用指数记数法。The s parameter can also use exponential notation.
Number wssign,千位分隔符 (,), 和小数点 () 元素。The ws, sign, thousands separator (,), and decimal point (.) elements.
Any 所有样式,除非s不能表示十六进制数。All styles, except s cannot represent a hexadecimal number.

provider参数是IFormatProvider实现其GetFormat方法将返回NumberFormatInfo提供特定于区域性的格式设置信息的对象。The provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object that provides culture-specific formatting information. TryParse(String, NumberStyles, IFormatProvider, Single)调用方法时,它调用provider参数的GetFormat方法并将其传递Type对象,表示NumberFormatInfo类型。When the TryParse(String, NumberStyles, IFormatProvider, Single) 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:

如果providernull的格式设置s被解释基于NumberFormatInfo当前区域性的对象。If provider is null, the formatting of s is interpreted based on the NumberFormatInfo object of the current culture.

如果分隔符时遇到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.

另请参阅

适用于