Single.TryParse 方法

定义

将数字的字符串表示形式转换为它的等效单精度浮点数。 一个指示转换是否成功的返回值。

重载

TryParse(ReadOnlySpan<Char>, IFormatProvider, Single)

尝试将字符范围解析为值。

TryParse(ReadOnlySpan<Char>, Single)

将字符范围中的数字的字符串表示形式转换为它的等效单精度浮点数。 一个指示转换是否成功的返回值。

TryParse(String, Single)

将数字的字符串表示形式转换为它的等效单精度浮点数。 一个指示转换是否成功的返回值。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Single)

尝试将 UTF-8 字符范围解析为值。

TryParse(String, IFormatProvider, Single)

尝试将字符串分析为值。

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

尝试将 UTF-8 字符范围解析为值。

TryParse(ReadOnlySpan<Byte>, Single)

尝试将包含数字字符串表示形式的 UTF-8 字符范围转换为其等效的单精度浮点数。

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

将具有指定样式和区域性特定格式的数字的范围表示形式转换为它的等效单精度浮点数。 一个指示转换是否成功的返回值。

TryParse(String, NumberStyles, IFormatProvider, Single)

将具有指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效单精度浮点数。 一个指示转换是否成功的返回值。

注解

在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到 PositiveInfinity IEEE NegativeInfinity 754 规范的要求。 在以前的版本中(包括.NET Framework)中,分析太大而无法表示的值会导致失败。

TryParse(ReadOnlySpan<Char>, IFormatProvider, Single)

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

尝试将字符范围解析为值。

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

参数

s
ReadOnlySpan<Char>

要分析的字符范围。

provider
IFormatProvider

一个对象,提供有关 s 的区域性特定格式设置信息。

result
Single

此方法返回时,包含成功分析 s的结果或失败时的未定义值。

返回

true 如果 s 已成功分析,则为 ;否则为 false

适用于

TryParse(ReadOnlySpan<Char>, Single)

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

将字符范围中的数字的字符串表示形式转换为它的等效单精度浮点数。 一个指示转换是否成功的返回值。

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>

>一个字符范围,它包含要转换的数字的字符串表示形式。

result
Single

当此方法返回时,如果转换成功,则包含与 s 参数等效的单精度浮点数;如果转换失败,则包含零。 如果 s 参数为 null 或为空,或不为有效格式的数字,则转换失败。 如果 s 是小于 Single.MinValue 的有效数字, result 则 为 NegativeInfinity。 如果 s 是大于 Single.MaxValue 的有效数字, result 则 为 PositiveInfinity。 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。

返回

如果 true 成功转换,则为 s;否则为 false

注解

在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到 PositiveInfinity IEEE 754 规范要求的 或 NegativeInfinity 。 在以前的版本中(包括 .NET Framework)中,分析太大而无法表示的值会导致失败。

适用于

TryParse(String, Single)

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

将数字的字符串表示形式转换为它的等效单精度浮点数。 一个指示转换是否成功的返回值。

public:
 static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] float % result);
public static bool TryParse (string s, 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

表示要转换的数字的字符串。

result
Single

当此方法返回时,如果转换成功,则包含与 s 所包含的数值或符号等效的单精度浮点数字;如果转换失败,则包含零。 如果 s 参数为 nullEmpty,或不为有效格式的数字,则转换失败。 如果 s 表示的数字小于 Single.MinValue 或大于 Single.MaxValue,则它在 .NET Framework 和 .NET Core 2.2 及更早版本上也会失败。 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。

返回

如果 true 成功转换,则为 s;否则为 false

示例

以下示例使用 TryParse(String, Single) 方法将数值的字符串表示形式转换为 Single 值。 它假定 en-US 是当前区域性。

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.689346E+17
// Parse a floating-point value with a thousands separator.
let value = "1,643.57"
match Single.TryParse value with
| true, number ->
    printfn $"{number}"
| _ ->
    printfn $"Unable to parse '{value}'."

// Parse a floating-point value with a currency symbol and a
// thousands separator.
let value = "$1,643.57"
match Single.TryParse value with
| true, number ->
    printfn $"{number}"
| _ ->
    printfn $"Unable to parse '{value}'."

// Parse value in exponential notation.
let value = "-1.643e6"
match Single.TryParse value with
| true, number ->
    printfn $"{number}"
| _ ->
    printfn $"Unable to parse '{value}'."

// Parse a negative integer value.
let value = "-168934617882109132"
match Single.TryParse value with
| true, number ->
    printfn $"{number}"
| _ ->
    printfn $"Unable to parse '{value}'."
// The example displays the following output:
//       1643.57
//       Unable to parse '$1,643.57'.
//       -164300
//       -1.689346E+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.689346E+17

注解

在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到 PositiveInfinity IEEE 754 规范要求的 或 NegativeInfinity 。 在以前的版本中(包括 .NET Framework)中,分析太大而无法表示的值会导致失败。

此重载不同于 Single.Parse(String) 方法,返回一个布尔值,该值指示分析操作是否成功,而不是返回已分析的数值。 它无需使用异常处理来测试 FormatException 无效且无法成功分析的 事件 s

参数 s 可以包含 PositiveInfinitySymbolNegativeInfinitySymbol、、 NaNSymbol (字符串比较) 区分大小写,也可以包含以下形式的字符串:

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

括号中的元素是可选的。 下表对每个元素进行了描述。

元素 说明
ws 一系列空白字符。
sign 负号或正号符号。
integral-digits 一系列从 0 到 9 的数字字符,用于指定数字的整数部分。 如果有小数位数,则整数可能不存在。
, 区域性特定的组分隔符。
. 区域性特定的小数点符号。
fractional-digits 一系列从 0 到 9 的数字字符,用于指定数字的小数部分。
E 一个大写或小写字符“e”,指示指数 (科学) 表示法。
exponential-digits 一系列数字字符,范围从 0 到 9,用于指定指数。

参数s使用 和 NumberStyles.AllowThousands 标志的组合NumberStyles.Float进行解释。 这意味着允许使用空格和数千个分隔符,但不允许使用货币符号。 若要显式定义 (元素,如货币符号、千位分隔符和空白) 中可能存在 s,请使用 TryParse(String, NumberStyles, IFormatProvider, Single) 方法重载。

参数 s 是使用针对当前系统区域性初始化的 对象中的 NumberFormatInfo 格式设置信息进行分析的。 有关详细信息,请参阅 NumberFormatInfo.CurrentInfo。 若要使用其他指定区域性的格式设置信息分析字符串,请使用 TryParse(String, NumberStyles, IFormatProvider, Single) 方法重载。

通常,如果向方法传递 Single.TryParse 通过调用 Single.ToString 方法创建的字符串,则返回原始 Single 值。 但是,由于精度损失,这些值可能不相等。

如果 s 的数据类型范围Single外,该方法在 .NET Framework 和 .NET Core 2.2 及更早版本上返回 false 。 在 .NET Core 3.0 及更高版本上,如果 s 小于 Single.MinValueSingle.PositiveInfinity如果 s 大于 Single.MaxValue,则返回 Single.NegativeInfinity

如果在分析操作期间在 参数中 s 遇到分隔符,并且适用的货币或数字十进制分隔符和组分隔符相同,则分析操作假定该分隔符是小数分隔符而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparatorNumberGroupSeparator

另请参阅

适用于

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Single)

Source:
Single.cs
Source:
Single.cs

尝试将 UTF-8 字符范围解析为值。

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] float % result) = IUtf8SpanParsable<float>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out float result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * single -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As Single) As Boolean

参数

utf8Text
ReadOnlySpan<Byte>

要分析的 UTF-8 字符范围。

provider
IFormatProvider

一个对象,提供有关 utf8Text 的区域性特定格式设置信息。

result
Single

返回时,包含成功分析 utf8Text 的结果或失败时未定义的值。

返回

true 如果 utf8Text 已成功分析,则为 ;否则为 false

适用于

TryParse(String, IFormatProvider, Single)

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

尝试将字符串分析为值。

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

参数

s
String

要分析的字符串。

provider
IFormatProvider

一个对象,提供有关 s 的区域性特定格式设置信息。

result
Single

此方法返回时,包含成功分析 s 的结果或失败时的未定义值。

返回

true 如果 s 已成功分析,则为 ;否则为 false

适用于

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

Source:
Single.cs
Source:
Single.cs

尝试将 UTF-8 字符范围解析为值。

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] float % result) = System::Numerics::INumberBase<float>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out float result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * single -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As Single) As Boolean

参数

utf8Text
ReadOnlySpan<Byte>

要分析的 UTF-8 字符范围。

style
NumberStyles

可以存在于 中的 utf8Text数字样式的按位组合。

provider
IFormatProvider

一个对象,提供有关 utf8Text 的区域性特定格式设置信息。

result
Single

返回时,包含成功分析 utf8Text 的结果或失败时未定义的值。

返回

true 如果 utf8Text 已成功分析,则为 ;否则为 false

适用于

TryParse(ReadOnlySpan<Byte>, Single)

Source:
Single.cs
Source:
Single.cs

尝试将包含数字字符串表示形式的 UTF-8 字符范围转换为其等效的单精度浮点数。

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

参数

utf8Text
ReadOnlySpan<Byte>

包含要转换的数字的只读 UTF-8 字符范围。

result
Single

此方法返回时,如果转换成功,则包含与 中包含的 utf8Text 数值或符号等效的单精度浮点数;如果转换失败,则包含零。 如果 utf8TextEmpty 或 格式无效,则转换失败。 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。

返回

如果 true 成功转换,则为 utf8Text;否则为 false

适用于

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

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

将具有指定样式和区域性特定格式的数字的范围表示形式转换为它的等效单精度浮点数。 一个指示转换是否成功的返回值。

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, [Runtime::InteropServices::Out] float % result) = System::Numerics::INumberBase<float>::TryParse;
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>

包含要转换的数字的只读字符范围。 该范围使用由 style 指定的样式来进行解释。

style
NumberStyles

枚举值的一个按位组合,指示 s 所允许的格式。 一个用来指定的典型值为 FloatAllowThousands 的组合。

provider
IFormatProvider

一个对象,提供有关 s 的区域性特定格式设置信息。

result
Single

当此方法返回时,如果转换成功,则包含与 s 所包含的数值或符号等效的单精度浮点数字;如果转换失败,则包含零。 如果s参数为 nullEmpty,格式不符合 style,表示小于 Single.MinValue 或大于 Single.MaxValue 的数字,或者如果 style 不是枚举常量的有效组合NumberStyles,则转换失败。 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。

返回

如果 true 成功转换,则为 s;否则为 false

注解

在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到 PositiveInfinity IEEE 754 规范要求的 或 NegativeInfinity 。 在以前的版本中(包括 .NET Framework)中,分析太大而无法表示的值会导致失败。

适用于

TryParse(String, NumberStyles, IFormatProvider, Single)

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

将具有指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效单精度浮点数。 一个指示转换是否成功的返回值。

public:
 static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] float % result);
public:
 static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] float % result) = System::Numerics::INumberBase<float>::TryParse;
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, 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

表示要转换的数字的字符串。

style
NumberStyles

枚举值的一个按位组合,指示 s 所允许的格式。 一个用来指定的典型值为 FloatAllowThousands 的组合。

provider
IFormatProvider

一个对象,提供有关 s 的区域性特定格式设置信息。

result
Single

当此方法返回时,如果转换成功,则包含与 s 所包含的数值或符号等效的单精度浮点数字;如果转换失败,则包含零。 如果 s 参数为 nullEmpty,格式不符合 style,或者如果 style 不是 NumberStyles 枚举常量的有效组合,则转换失败。 如果 s 表示的数字小于 Single.MinValue 或大于 Single.MaxValue,则它在 .NET Framework 或 .NET Core 2.2 及更早版本上也会失败。 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。

返回

如果 true 成功转换,则为 s;否则为 false

例外

style 不是 NumberStyles 值。

styleAllowHexSpecifier 值。

示例

下面的示例演示如何使用 Single.TryParse(String, NumberStyles, IFormatProvider, Single) 方法分析具有特定样式并使用特定区域性的约定设置格式的数字的字符串表示形式。

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'.
// Parse currency value using en-GB culture.
let value = "£1,097.63"
let style = System.Globalization.NumberStyles.Number ||| System.Globalization.NumberStyles.AllowCurrencySymbol
let culture = System.Globalization.CultureInfo.CreateSpecificCulture "en-GB"
match Single.TryParse(value, style, culture) with
| true, number ->
    printfn $"Converted '{value}' to {number}."
| _ ->
    printfn $"Unable to convert '{value}'."

let value = "1345,978"
let style = System.Globalization.NumberStyles.AllowDecimalPoint
let culture = System.Globalization.CultureInfo.CreateSpecificCulture "fr-FR"
match Single.TryParse(value, style, culture) with
| true, number ->
    printfn $"Converted '{value}' to {number}."
| _ ->
    printfn $"Unable to convert '{value}'."

let value = "1.345,978"
let style = System.Globalization.NumberStyles.AllowDecimalPoint ||| System.Globalization.NumberStyles.AllowThousands
let culture = System.Globalization.CultureInfo.CreateSpecificCulture "es-ES"
match Single.TryParse(value, style, culture) with
| true, number ->
    printfn $"Converted '{value}' to {number}."
| _ ->
    printfn $"Unable to convert '{value}'."

let value = "1 345,978"
match Single.TryParse(value, style, culture) with
| true, number ->
    printfn $"Converted '{value}' to {number}."
| _ ->
    printfn $"Unable to convert '{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 及更高版本中,太大而无法表示的值将舍入到 PositiveInfinity IEEE 754 规范要求的 或 NegativeInfinity 。 在以前的版本中(包括 .NET Framework)中,分析太大而无法表示的值会导致失败。

此重载不同于 Parse(String, NumberStyles, IFormatProvider) 方法,返回一个布尔值,该值指示分析操作是否成功,而不是返回已分析的数值。 它无需使用异常处理来测试 FormatException 无效且无法成功分析的 事件 s

参数 style 定义允许的参数格式, s 以便分析操作成功。 它必须是 枚举中的位标志 NumberStyles 的组合。 不支持以下 NumberStyles 成员:

对于 s 由 指示的区域性, 参数可以包含 PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbolprovider 此外,根据 的值 styles 参数可能包括以下元素:

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

方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。

元素 说明
ws 可选空格。 如果style包含 NumberStyles.AllowLeadingWhite 标志,s则 开头会出现空格。 如果style包含 标志,NumberStyles.AllowTrailingWhite则它可以显示在 的末尾s
$ 区域性特定的货币符号。 它在字符串中的位置由 NumberFormatInfo.CurrencyNegativePattern 参数的 方法provider返回IFormatProvider.GetFormatNumberFormatInfo 对象的 或 NumberFormatInfo.CurrencyPositivePattern 属性定义。 如果style包含 标志,NumberStyles.AllowCurrencySymbol则可以在 中s显示货币符号。
sign 可选符号。 如果包含 标志,则符号可以出现在 的s开头,如果style包含 NumberStyles.AllowTrailingSign 标志,则它可显示在 的末尾sNumberStyles.AllowLeadingSignstyle 如果style包含 标志,NumberStyles.AllowParentheses则可以在 中使用s括号来指示负值。
integral-digits 一系列数字,范围从 0 到 9,指定数字的整数部分。 如果有小数位数,则整数可能不存在。
, 区域性特定的千位分隔符。 如果style包含 NumberStyles.AllowThousands 标志,则当前区域性的千位分隔符可以出现在 中s
. 区域性特定的小数点符号。 如果style包含 NumberStyles.AllowDecimalPoint 标志,则当前区域性的小数点符号可以出现在 中s
fractional-digits 一系列数字,范围从 0 到 9,指定数字的小数部分。 如果style包含 标志,NumberStyles.AllowDecimalPoint则可以在 中s显示小数位数。
e e 或 E 字符,指示 s 可以使用指数表示法表示数字。 如果样式包含 标志,则 s 参数可以表示指数表示法中的 NumberStyles.AllowExponent 数字。
exponential-digits 指定指数的一系列数字,范围为 0 到 9。

注意

分析操作将忽略中 s 任何 (U+0000) 字符的终止 NUL,而不考虑 参数的值 style

仅包含数字的字符串 (对应于 NumberStyles.None 样式) 如果位于类型范围内 Single ,则始终会成功分析。 其余 System.Globalization.NumberStyles 成员控制可能为 但不需要存在于输入字符串中的元素。 下表指示各个 NumberStyles 标志如何影响 中可能存在 s的元素。

NumberStyles 值 除了数字外,还允许 在 中
None 仅限 整型数字 元素。
AllowDecimalPoint 小数位数元素。
AllowExponent 参数 s 还可以使用指数表示法。 此标志本身支持 整数数字E指数数字形式的值;若要使用正数或负号和小数点符号等元素成功分析指数表示法中的字符串,还需要其他标志。
AllowLeadingWhite 开头的 sws 元素。
AllowTrailingWhite 位于 末尾的 sws 元素。
AllowLeadingSign 开头的s符号元素。
AllowTrailingSign 末尾的s符号元素。
AllowParentheses 用括号括住数值的 符号 元素。
AllowThousands 元素。
AllowCurrencySymbol $ 元素。
Currency 全部。 参数 s 不能表示十六进制数或指数表示法中的数字。
Float ws 元素位于 开头或末尾s符号位于 的s开头,符号为 参数 s 还可以使用指数表示法。
Number wssign、千位分隔符 (、) 和小数点 (.) 元素。
Any 除 之外 s 的所有样式都不能表示十六进制数。

provider参数是一个IFormatProvider实现,其GetFormat方法返回一个NumberFormatInfo对象,该对象提供特定于区域性的格式设置信息。 TryParse(String, NumberStyles, IFormatProvider, Single)调用 方法时,它会调用provider参数的 GetFormat 方法,并为其传递一个Type表示类型的 NumberFormatInfo 对象。 然后, GetFormat 方法返回 对象, NumberFormatInfo 该对象提供有关 参数格式 s 的信息。 有三种方法可以使用 provider 参数向分析操作提供自定义格式设置信息:

如果 providernull,则根据NumberFormatInfo当前区域性的对象解释 的格式s

如果 s 已超过数据类型的范围Single,则方法在 .NET Framework 和 .NET Core 2.2 及更早版本上引发 OverflowException 。 在 .NET Core 3.0 及更高版本中,如果 s 小于 Single.MinValueSingle.PositiveInfinitys 大于 Single.MaxValue,则返回 Single.NegativeInfinity

如果在分析操作期间参数中 s 遇到分隔符,并且适用的货币或数字小数分隔符和组分隔符相同,则分析操作假定分隔符是小数点分隔符,而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparatorNumberGroupSeparator

另请参阅

适用于