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 * single -> 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 * single -> 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.

参数可以包含PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol (字符串比较区分大小写)或格式的字符串: sThe 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.

参数使用NumberStyles.FloatNumberStyles.AllowThousands标志的组合来进行解释。 sThe 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.

使用为当前系统区域性初始化的NumberFormatInfo对象中的格式设置信息分析参数。sThe 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. 有关分隔符的详细信息,请CurrencyDecimalSeparator参阅NumberDecimalSeparator CurrencyGroupSeparator、、和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> * single -> 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 * single -> 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.

参数定义允许的s参数的格式,以便分析操作成功。 styleThe 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:

参数可以NegativeInfinitySymbol包含PositiveInfinitySymbolNaNSymbol指示的区域性的。provider sThe 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.CurrencyNegativePatternprovider参数IFormatProvider.GetFormat方法返回NumberFormatInfo.CurrencyPositivePatternNumberFormatInfo对象的或属性定义。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.AllowCurrencySymbolThe currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
signsign 一个可选的符号。An optional sign. s s style NumberStyles.AllowTrailingSign如果包含style 标志,则符号可以出现在的开头,如果包含标志,则它可以NumberStyles.AllowLeadingSign出现在的末尾。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.AllowParenthesesParentheses 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.AllowThousandsThe 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.AllowDecimalPointThe 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 styleNumberStyles.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.

备注

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

只包含数字的字符串 (对应NumberStyles.None于样式) 始终会成功分析。A string with digits only (which corresponds to the NumberStyles.None style) always parses successfully. 其余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标志如何影响可能出现在中s的元素。The 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 开头sws元素。The ws element at the beginning of s.
AllowTrailingWhite 末尾s处的ws元素。The ws element at the end of s.
AllowLeadingSign 开头ssign元素。The sign element at the beginning of s.
AllowTrailingSign 末尾ssign元素。The 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 在开头或结尾s处的 ws 元素,在的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 、、千位分隔符(,) 和小数点(.)元素。 ws signThe ws, sign, thousands separator (,), and decimal point (.) elements.
Any 除之外s的所有样式都不能表示十六进制数。All styles, except s cannot represent a hexadecimal number.

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

如果provider snull, 则基于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. 有关分隔符的详细信息,请CurrencyDecimalSeparator参阅NumberDecimalSeparator CurrencyGroupSeparator、、和NumberGroupSeparatorFor more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.

另请参阅

适用于