Single.TryParse 方法

定义

将数字的字符串表示形式转换为它的等效单精度浮点数。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)

将具有指定样式和区域性特定格式的数字的范围表示形式转换为它的等效单精度浮点数。Converts the span 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(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)

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

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.

注解

在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到, PositiveInfinityNegativeInfinity 根据 IEEE 754 规范的要求进行舍入。In .NET Core 3.0 and later, values that are too large to represent are rounded to PositiveInfinity or NegativeInfinity as required by the IEEE 754 specification. 在以前的版本中(包括 .NET Framework),分析过大而无法表示的值导致失败。In prior versions, including .NET Framework, parsing a value that was too large to represent resulted in failure.

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

将具有指定样式和区域性特定格式的数字的范围表示形式转换为它的等效单精度浮点数。Converts the span 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(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);
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

参数

s
ReadOnlySpan<Char>

包含要转换的数字的只读字符范围。A read-only character span that contains the number to convert. 该范围使用由 style 指定的样式来进行解释The span is interpreted using the style specified by style

style
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

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

result
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.

返回

Boolean

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

注解

在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到, PositiveInfinityNegativeInfinity 根据 IEEE 754 规范的要求进行舍入。In .NET Core 3.0 and later, values that are too large to represent are rounded to PositiveInfinity or NegativeInfinity as required by the IEEE 754 specification. 在以前的版本中(包括 .NET Framework),分析过大而无法表示的值导致失败。In prior versions, including .NET Framework, parsing a value that was too large to represent resulted in failure.

适用于

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

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

result
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,或不为有效格式的数字,则转换失败。The conversion fails if the s parameter is null or Empty or is not a number in a valid format. 如果 s 表示一个小于 MinValue 或大于 MaxValue 的数字,则转换在 .NET Framework 和 .NET Core 2.2 及更低版本上也会失败。It also fails on .NET Framework and .NET Core 2.2 and earlier versions if s 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.

返回

Boolean

如果 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

注解

在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到, PositiveInfinityNegativeInfinity 根据 IEEE 754 规范的要求进行舍入。In .NET Core 3.0 and later, values that are too large to represent are rounded to PositiveInfinity or NegativeInfinity as required by the IEEE 754 specification. 在以前的版本中(包括 .NET Framework),分析过大而无法表示的值导致失败。In prior versions, including .NET Framework, parsing a value that was too large to represent resulted in failure.

此重载与方法的不同之处在于, 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表明[整数位数,] 整数位数 [. [小数位数]] [e [sign] 指数-数字] [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-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-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-digits 指定指数的一系列数字字符,范围为0到9。A series of numeric characters ranging from 0 to 9 that specify an exponent.

s参数使用和标志的组合来进行解释 NumberStyles.Float NumberStyles.AllowThousandsThe 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 超出了数据类型的范围 Single ,则该方法将 false 在 .NET Framework 和 .net Core 2.2 及更低版本上返回。If s is out of range of the Single data type, the method returns false on .NET Framework and .NET Core 2.2 and earlier versions. 在 .NET Core 3.0 及更高版本中, Single.NegativeInfinity 如果 s 小于 Single.MinValue 且大于, Single.PositiveInfinity s Single.MaxValue 则返回。On .NET Core 3.0 and later versions, it returns Single.NegativeInfinity if s is less than Single.MinValue and Single.PositiveInfinity if s is greater than Single.MaxValue.

如果在分析操作过程中在参数中遇到分隔符 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 CurrencyGroupSeparatorNumberGroupSeparatorFor more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.

另请参阅

适用于

TryParse(ReadOnlySpan<Char>, Single)

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

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

参数

s
ReadOnlySpan<Char>

>一个字符范围,它包含要转换的数字的字符串表示形式。>A character span that contains the string representation of the number to convert.

result
Single

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

返回

Boolean

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

注解

在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到, PositiveInfinityNegativeInfinity 根据 IEEE 754 规范的要求进行舍入。In .NET Core 3.0 and later, values that are too large to represent are rounded to PositiveInfinity or NegativeInfinity as required by the IEEE 754 specification. 在以前的版本中(包括 .NET Framework),分析过大而无法表示的值导致失败。In prior versions, including .NET Framework, parsing a value that was too large to represent resulted in failure.

适用于

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

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

style
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

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

result
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,或者如果 style 不是 NumberStyles 枚举常量的有效组合,则转换失败。The conversion fails if the s parameter is null or Empty, is not in a format compliant with style, or if style is not a valid combination of NumberStyles enumeration constants. 如果 s 表示一个小于 MinValue 或大于 MaxValue 的数字,则转换在 .NET Framework 或 .NET Core 2.2 及更低版本上也会失败。It also fails on .NET Framework or .NET Core 2.2 and earlier versions if s 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.

返回

Boolean

如果 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'.

注解

在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到, PositiveInfinityNegativeInfinity 根据 IEEE 754 规范的要求进行舍入。In .NET Core 3.0 and later, values that are too large to represent are rounded to PositiveInfinity or NegativeInfinity as required by the IEEE 754 specification. 在以前的版本中(包括 .NET Framework),分析过大而无法表示的值导致失败。In prior versions, including .NET Framework, parsing a value that was too large to represent resulted in failure.

此重载与方法的不同之处在于, 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. 它必须是枚举中的位标志的组合 NumberStylesIt must be a combination of bit flags from the NumberStyles enumeration. NumberStyles不支持以下成员:The following NumberStyles members are not supported:

s参数可以包含 PositiveInfinitySymbol NegativeInfinitySymbol NaNSymbol 由指示的区域性的 providerThe s parameter can contain PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol for the culture indicated by provider. 此外,根据的值 styles 参数可能包括以下元素:In addition, depending on the value of style, the s parameter may include the following elements:

ws[$]表明[整数位数,] 整数位数 [. 小数] [e [sign] 指数-数字] [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.AllowLeadingWhiteWhite space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag. s如果包含标志,则它可以出现在的结尾 style NumberStyles.AllowTrailingWhiteIt can appear at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
$ 区域性特定的货币符号。A culture-specific currency symbol. 其在字符串中的位置由 NumberFormatInfo.CurrencyNegativePattern NumberFormatInfo.CurrencyPositivePattern NumberFormatInfo 参数方法返回的对象的或属性定义 IFormatProvider.GetFormat providerIts 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 style NumberStyles.AllowLeadingSign ,如果包含标志,则它可以出现在的末尾 s style NumberStyles.AllowTrailingSignThe 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-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-digits 一系列从0到9之间的数字,用于指定数字的小数部分。A series of digits ranging from 0 to 9 that specify the fractional part of the number. s如果 style 包含标志,则小数位数可以出现在中 NumberStyles.AllowDecimalPointFractional 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如果 style 包含标志,则参数可以表示指数表示法中的数字 NumberStyles.AllowExponentThe s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.
指数位数exponential-digits 一系列从0到9之间的数字,用于指定指数。A series of digits ranging from 0 to 9 that specify an exponent.

备注

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

只包含数字的字符串 (与 NumberStyles.None 样式) 始终在类型范围内分析成功 SingleA string with digits only (which corresponds to the NumberStyles.None style) always parses successfully if it is in the range of the Single type. 其余 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-digits E exponential-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 开头的 sign 元素 sThe sign element at the beginning of s.
AllowTrailingSign 末尾的 sign 元素 sThe sign element at the end of s.
AllowParentheses 用括号括起数值的 符号 元素。The sign element in the form of parentheses enclosing the numeric value.
AllowThousands 元素。The , 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 表示类型的对象 NumberFormatInfoWhen 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 对象,该对象提供有关参数格式的信息 sThe 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:

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

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

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

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

如果 s 超出了数据类型的范围 Single ,则该方法将 OverflowException 在 .NET Framework 和 .net Core 2.2 及更低版本上引发。If s is out of range of the Single data type, the method throws an OverflowException on .NET Framework and .NET Core 2.2 and earlier versions. 在 .NET Core 3.0 及更高版本中, Single.NegativeInfinity 如果 s 小于 Single.MinValue 且大于, Single.PositiveInfinity s Single.MaxValue 则返回。On .NET Core 3.0 and later versions, it returns Single.NegativeInfinity if s is less than Single.MinValue and Single.PositiveInfinity if s is greater than Single.MaxValue.

如果在分析操作过程中在参数中遇到分隔符 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 CurrencyGroupSeparatorNumberGroupSeparatorFor more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.

另请参阅

适用于