Double.TryParse 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将数字的字符串表示形式转换为它的等效双精度浮点数。 一个指示转换是否成功的返回值。
重载
TryParse(ReadOnlySpan<Char>, IFormatProvider, Double) |
尝试将字符范围解析为值。 |
TryParse(ReadOnlySpan<Char>, Double) |
将指定样式和区域性特定格式的数字的范围表示形式转换为它的等效双精度浮点数。 一个指示转换是否成功的返回值。 |
TryParse(String, Double) |
将数字的字符串表示形式转换为它的等效双精度浮点数。 一个指示转换是否成功的返回值。 |
TryParse(ReadOnlySpan<Byte>, IFormatProvider, Double) |
尝试将 UTF-8 字符范围解析为值。 |
TryParse(String, IFormatProvider, Double) |
尝试将字符串分析为值。 |
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Double) |
尝试将 UTF-8 字符范围解析为值。 |
TryParse(ReadOnlySpan<Byte>, Double) |
尝试将包含数字字符串表示形式的 UTF-8 字符范围转换为等效的双精度浮点数。 |
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Double) |
将字符范围(其中包含指定样式和区域性特定格式的数字的字符串表示形式)转换为它的等效双精度浮点数。 一个指示转换是否成功的返回值。 |
TryParse(String, NumberStyles, IFormatProvider, Double) |
将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效双精度浮点数。 一个指示转换是否成功的返回值。 |
注解
在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到 PositiveInfinity IEEE 754 规范要求的 或 NegativeInfinity 。 在早期版本中(包括 .NET Framework)中,分析太大而无法表示的值会导致失败。
TryParse(ReadOnlySpan<Char>, IFormatProvider, Double)
- Source:
- Double.cs
- Source:
- Double.cs
- Source:
- Double.cs
尝试将字符范围解析为值。
public:
static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] double % result) = ISpanParsable<double>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out double result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * double -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As Double) As Boolean
参数
- s
- ReadOnlySpan<Char>
要分析的字符范围。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
- result
- Double
此方法返回时,包含成功分析 s
的结果或失败时的未定义值。
返回
true
如果 s
已成功分析,则为 ;否则为 false
。
适用于
TryParse(ReadOnlySpan<Char>, Double)
- Source:
- Double.cs
- Source:
- Double.cs
- Source:
- Double.cs
将指定样式和区域性特定格式的数字的范围表示形式转换为它的等效双精度浮点数。 一个指示转换是否成功的返回值。
public:
static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] double % result);
public static bool TryParse (ReadOnlySpan<char> s, out double result);
static member TryParse : ReadOnlySpan<char> * double -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As Double) As Boolean
参数
- s
- ReadOnlySpan<Char>
包含要转换的数字的字符串表示形式的字符范围。
- result
- Double
当此方法返回时,如果转换成功,则包含与 s
参数中所包含的数值或符号等效的双精度浮点数;如果转换失败,则包含零。 如果 s
参数为 null
或空,或其格式不符合 style
,则转换失败。 如果 style
不是 NumberStyles 枚举常量的有效组合,则转换也会失败。 如果 s
是小于 Double.MinValue 的有效数字, result
则 为 NegativeInfinity。 如果 s
是大于 Double.MaxValue 的有效数字, result
则 为 PositiveInfinity。 此参数未经初始化即进行传递;最初在 result
中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 s
;否则为 false
。
注解
在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到 PositiveInfinity IEEE 754 规范要求的 或 NegativeInfinity 。 在早期版本中(包括 .NET Framework)中,分析太大而无法表示的值会导致失败。
适用于
TryParse(String, Double)
- Source:
- Double.cs
- Source:
- Double.cs
- Source:
- Double.cs
将数字的字符串表示形式转换为它的等效双精度浮点数。 一个指示转换是否成功的返回值。
public:
static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] double % result);
public static bool TryParse (string s, out double result);
public static bool TryParse (string? s, out double result);
static member TryParse : string * double -> bool
Public Shared Function TryParse (s As String, ByRef result As Double) As Boolean
参数
- s
- String
包含要转换的数字的字符串。
- result
- Double
当此方法返回时,如果转换成功,则包含与 s
参数等效的双精度浮点数;如果转换失败,则包含零。 如果 s
参数为 null
或 Empty,或不为有效格式的数字,则转换失败。 如果 s
表示的数字小于 Double.MinValue 或大于 Double.MaxValue ,则它在 .NET Framework 和 .NET Core 2.2 及更早版本上也会失败。 此参数未经初始化即进行传递;最初在 result
中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 s
;否则为 false
。
示例
以下示例使用 TryParse(String, Double) 方法将数值的字符串表示形式转换为 Double 值。 它假定 en-US 是当前区域性。
using System;
public class Example
{
public static void Main()
{
string[] values = { "1,643.57", "$1,643.57", "-1.643e6",
"-168934617882109132", "123AE6",
null, String.Empty, "ABCDEF" };
double number;
foreach (var value in values) {
if (Double.TryParse(value, out number))
Console.WriteLine("'{0}' --> {1}", value, number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
}
}
}
// The example displays the following output:
// '1,643.57' --> 1643.57
// Unable to parse '$1,643.57'.
// '-1.643e6' --> -1643000
// '-168934617882109132' --> -1.68934617882109E+17
// Unable to parse '123AE6'.
// Unable to parse ''.
// Unable to parse ''.
// Unable to parse 'ABCDEF'.
open System
let values =
[| "1,643.57"; "$1,643.57"; "-1.643e6"
"-168934617882109132"; "123AE6"
null; String.Empty; "ABCDEF" |]
for value in values do
match Double.TryParse value with
| true, number ->
printfn $"'{value}' --> {number}"
| _ ->
printfn $"Unable to parse '{value}'."
// The example displays the following output:
// '1,643.57' --> 1643.57
// Unable to parse '$1,643.57'.
// '-1.643e6' --> -1643000
// '-168934617882109132' --> -1.68934617882109E+17
// Unable to parse '123AE6'.
// Unable to parse ''.
// Unable to parse ''.
// Unable to parse 'ABCDEF'.
Module Example
Public Sub Main()
Dim values() As String = { "1,643.57", "$1,643.57", "-1.643e6",
"-168934617882109132", "123AE6",
Nothing, String.Empty, "ABCDEF" }
Dim number As Double
For Each value In values
If Double.TryParse(value, number) Then
Console.WriteLine("'{0}' --> {1}", value, number)
Else
Console.WriteLine("Unable to parse '{0}'.", value)
End If
Next
End Sub
End Module
' The example displays the following output:
' '1,643.57' --> 1643.57
' Unable to parse '$1,643.57'.
' '-1.643e6' --> -1643000
' '-168934617882109132' --> -1.68934617882109E+17
' Unable to parse '123AE6'.
' Unable to parse ''.
' Unable to parse ''.
' Unable to parse 'ABCDEF'.
注解
在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到 PositiveInfinity IEEE 754 规范要求的 或 NegativeInfinity 。 在早期版本中(包括 .NET Framework)中,分析太大而无法表示的值会导致失败。
此重载不同于 Double.Parse(String) 方法,返回一个布尔值,该值指示分析操作是否成功,而不是返回已分析的数值。 它无需使用异常处理来测试 FormatException 无效且无法成功分析的 事件 s
。
参数 s
可以包含当前区域性的 NumberFormatInfo.PositiveInfinitySymbol、 NumberFormatInfo.NegativeInfinitySymbol、、 NumberFormatInfo.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
,请使用 Double.TryParse(String, NumberStyles, IFormatProvider, Double) 方法重载。
参数 s
是使用针对当前系统区域性初始化的 对象中的 NumberFormatInfo 格式设置信息进行分析的。 有关详细信息,请参阅 NumberFormatInfo.CurrentInfo。 若要使用其他指定区域性的格式设置信息分析字符串,请使用 Double.TryParse(String, NumberStyles, IFormatProvider, Double) 方法重载。
通常,如果向方法传递 Double.TryParse 通过调用 Double.ToString 方法创建的字符串,则返回原始 Double 值。 但是,由于精度损失,这些值可能不相等。 此外,尝试分析 或 Double.MaxValue 的Double.MinValue字符串表示形式无法往返。 在 .NET Framework 和 .NET Core 2.2 及早期版本上,它会引发 OverflowException。 在 .NET Core 3.0 及更高版本中,如果尝试分析 或Double.PositiveInfinity尝试分析 MaxValueMinValue ,则返回 Double.NegativeInfinity 。 下面的示例进行了这方面的演示。
using System;
public class Example
{
public static void Main()
{
string value;
double number;
value = Double.MinValue.ToString();
if (Double.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("{0} is outside the range of a Double.",
value);
value = Double.MaxValue.ToString();
if (Double.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("{0} is outside the range of a Double.",
value);
}
}
// The example displays the following output:
// -1.79769313486232E+308 is outside the range of the Double type.
// 1.79769313486232E+308 is outside the range of the Double type.
open System
[<EntryPoint>]
let main _ =
let value = string Double.MinValue
match Double.TryParse value with
| true, number ->
printfn $"{number}"
| _ ->
printfn $"{value} is outside the range of a Double."
let value = string Double.MaxValue
match Double.TryParse value with
| true, number ->
printfn $"{number}"
| _ ->
printfn $"{value} is outside the range of a Double."
0
// The example displays the following output:
// -1.79769313486232E+308 is outside the range of the Double type.
// 1.79769313486232E+308 is outside the range of the Double type.
Module Example
Public Sub Main()
Dim value As String
Dim number As Double
value = Double.MinValue.ToString()
If Double.TryParse(value, number) Then
Console.WriteLine(number)
Else
Console.WriteLine("{0} is outside the range of a Double.", _
value)
End If
value = Double.MaxValue.ToString()
If Double.TryParse(value, number) Then
Console.WriteLine(number)
Else
Console.WriteLine("{0} is outside the range of a Double.", _
value)
End If
End Sub
End Module
' The example displays the following output:
' -1.79769313486232E+308 is outside the range of the Double type.
' 1.79769313486232E+308 is outside the range of the Double type.
在 .NET Framework 和 .NET Core 2.2 及更早版本上,如果 s
超过数据类型的范围 Double ,该方法 TryParse(String, Double) 将 OverflowException引发 。
在 .NET Core 3.0 及更高版本上,当 数据类型范围Double外时s
,不会引发异常。 在大多数情况下, TryParse(String, Double) 方法计算 或 Double.NegativeInfinity的结果Double.PositiveInfinity。 但是,有一小部分值被认为更接近 的最大值或最小值 Double ,而不是正无穷大或负无穷大。 在这些情况下, 方法计算 或 Double.MinValue的结果Double.MaxValue。
如果在分析操作期间在 参数中 s
遇到分隔符,并且小数分隔符和组分隔符相同,则分析操作假定分隔符是小数分隔符而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparator和 NumberGroupSeparator。
另请参阅
- Parse(String)
- ToString()
- 分析 .NET 中的数字字符串
- 示例:.NET Core WinForms 格式设置实用工具 (C#)
- 示例:.NET Core WinForms 格式设置实用工具 (Visual Basic)
适用于
TryParse(ReadOnlySpan<Byte>, IFormatProvider, Double)
- Source:
- Double.cs
- Source:
- Double.cs
尝试将 UTF-8 字符范围解析为值。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] double % result) = IUtf8SpanParsable<double>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out double result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * double -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As Double) As Boolean
参数
- utf8Text
- ReadOnlySpan<Byte>
要分析的 UTF-8 字符的跨度。
- provider
- IFormatProvider
一个对象,提供有关 utf8Text
的区域性特定格式设置信息。
- result
- Double
返回时,包含成功分析 utf8Text
的结果或失败时的未定义值。
返回
true
如果 utf8Text
已成功分析,则为 ;否则为 false
。
适用于
TryParse(String, IFormatProvider, Double)
- Source:
- Double.cs
- Source:
- Double.cs
- Source:
- Double.cs
尝试将字符串解析为值。
public:
static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] double % result) = IParsable<double>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out double result);
static member TryParse : string * IFormatProvider * double -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As Double) As Boolean
参数
- s
- String
要分析的字符串。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
- result
- Double
此方法返回时,包含成功分析 s
的结果或失败时的未定义值。
返回
true
如果 s
已成功分析,则为 ;否则为 false
。
适用于
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Double)
- Source:
- Double.cs
- Source:
- Double.cs
尝试将 UTF-8 字符范围解析为值。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] double % result) = System::Numerics::INumberBase<double>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out double result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * double -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As Double) As Boolean
参数
- utf8Text
- ReadOnlySpan<Byte>
要分析的 UTF-8 字符的跨度。
- style
- NumberStyles
可以存在于 中的 utf8Text
数字样式的按位组合。
- provider
- IFormatProvider
一个对象,提供有关 utf8Text
的区域性特定格式设置信息。
- result
- Double
返回时,包含成功分析 utf8Text
的结果或失败时的未定义值。
返回
true
如果 utf8Text
已成功分析,则为 ;否则为 false
。
适用于
TryParse(ReadOnlySpan<Byte>, Double)
- Source:
- Double.cs
- Source:
- Double.cs
尝试将包含数字字符串表示形式的 UTF-8 字符范围转换为等效的双精度浮点数。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] double % result);
public static bool TryParse (ReadOnlySpan<byte> utf8Text, out double result);
static member TryParse : ReadOnlySpan<byte> * double -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As Double) As Boolean
参数
- utf8Text
- ReadOnlySpan<Byte>
包含要转换的数字的只读 UTF-8 字符范围。
- result
- Double
此方法返回时,如果转换成功,则包含等效于 中的 utf8Text
数值或符号的双精度浮点数;如果转换失败,则包含零。 如果 utf8Text
为 Empty 或 未采用有效格式,则转换失败。 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 utf8Text
;否则为 false
。
适用于
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Double)
- Source:
- Double.cs
- Source:
- Double.cs
- Source:
- Double.cs
将字符范围(其中包含指定样式和区域性特定格式的数字的字符串表示形式)转换为它的等效双精度浮点数。 一个指示转换是否成功的返回值。
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] double % result);
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] double % result) = System::Numerics::INumberBase<double>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out double result);
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out double result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * double -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As Double) As Boolean
参数
- s
- ReadOnlySpan<Char>
包含要转换的数字的只读字符范围。
- style
- NumberStyles
NumberStyles 值的按位组合,指示 s
的允许格式。 一个用来指定的典型值为 Float 与 AllowThousands 的组合。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
- result
- Double
当此方法返回时,如果转换成功,则包含与 s
中所包含的数值或符号等效的双精度浮点数。 如果转换失败,则包含零。 如果 s
参数为 null
、空字符范围、格式不符合 style
的数字,则转换失败。 如果 s
是小于 Double.MinValue 的有效数字, result
则 为 NegativeInfinity。 如果 s
是大于 Double.MaxValue 的有效数字, result
则 为 PositiveInfinity。 此参数未经初始化即进行传递;最初在 result
中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 s
;否则为 false
。
注解
在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到 PositiveInfinity IEEE NegativeInfinity 754 规范的要求。 在早期版本中(包括 .NET Framework)中,分析太大而无法表示的值会导致失败。
适用于
TryParse(String, NumberStyles, IFormatProvider, Double)
- Source:
- Double.cs
- Source:
- Double.cs
- Source:
- Double.cs
将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效双精度浮点数。 一个指示转换是否成功的返回值。
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] double % result);
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] double % result) = System::Numerics::INumberBase<double>::TryParse;
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out double result);
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out double result);
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * double -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As Double) As Boolean
参数
- s
- String
包含要转换的数字的字符串。
- style
- NumberStyles
NumberStyles 值的按位组合,指示 s
的允许格式。 一个用来指定的典型值为 Float 与 AllowThousands 的组合。
- provider
- IFormatProvider
一个 IFormatProvider,它提供有关 s
的区域性特定格式设置信息。
- result
- Double
当此方法返回时,如果转换成功,则包含与 s
中所包含的数值或符号等效的双精度浮点数;如果转换失败,则包含零。 如果 s
参数为 null
、Empty,或格式不符合 style
,或 style
不是 NumberStyles 枚举常量的有效组合,则转换失败。 如果 s
表示的数字小于 SByte.MinValue 或大于 SByte.MaxValue ,则它在 .NET Framework 或 .NET Core 2.2 及更早版本上也会失败。 此参数未经初始化即进行传递;最初在 result
中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 s
;否则为 false
。
例外
示例
以下示例演示如何使用 Double.TryParse(String, NumberStyles, IFormatProvider, Double) 方法分析具有特定样式并使用特定区域性约定设置格式的数字的字符串表示形式。
string value;
NumberStyles style;
CultureInfo culture;
double number;
// Parse currency value using en-GB culture.
value = "£1,097.63";
style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
culture = CultureInfo.CreateSpecificCulture("en-GB");
if (Double.TryParse(value, style, culture, out number))
Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
Console.WriteLine("Unable to convert '{0}'.", value);
// Displays:
// Converted '£1,097.63' to 1097.63.
value = "1345,978";
style = NumberStyles.AllowDecimalPoint;
culture = CultureInfo.CreateSpecificCulture("fr-FR");
if (Double.TryParse(value, style, culture, out number))
Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
Console.WriteLine("Unable to convert '{0}'.", value);
// Displays:
// Converted '1345,978' to 1345.978.
value = "1.345,978";
style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands;
culture = CultureInfo.CreateSpecificCulture("es-ES");
if (Double.TryParse(value, style, culture, out number))
Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
Console.WriteLine("Unable to convert '{0}'.", value);
// Displays:
// Converted '1.345,978' to 1345.978.
value = "1 345,978";
if (Double.TryParse(value, style, culture, out number))
Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
Console.WriteLine("Unable to convert '{0}'.", value);
// Displays:
// Unable to convert '1 345,978'.
// Parse currency value using en-GB culture.
let value = "£1,097.63"
let style = NumberStyles.Number ||| NumberStyles.AllowCurrencySymbol
let culture = CultureInfo.CreateSpecificCulture "en-GB"
match Double.TryParse(value, style, culture) with
| true, number ->
printfn $"Converted '{value}' to {number}."
| _ ->
printfn $"Unable to convert '{value}'."
// Displays:
// Converted '£1,097.63' to 1097.63.
let value = "1345,978"
let style = NumberStyles.AllowDecimalPoint
let culture = CultureInfo.CreateSpecificCulture "fr-FR"
match Double.TryParse(value, style, culture) with
| true, number ->
printfn $"Converted '{value}' to {number}."
| _ ->
printfn $"Unable to convert '{value}'."
// Displays:
// Converted '1345,978' to 1345.978.
let value = "1.345,978"
let style = NumberStyles.AllowDecimalPoint ||| NumberStyles.AllowThousands
let culture = CultureInfo.CreateSpecificCulture("es-ES")
match Double.TryParse(value, style, culture) with
| true, number ->
printfn $"Converted '{value}' to {number}."
| _ ->
printfn $"Unable to convert '{value}'."
// Displays:
// Converted '1.345,978' to 1345.978.
let value = "1 345,978"
match Double.TryParse(value, style, culture) with
| true, number ->
printfn $"Converted '{value}' to {number}."
| _ ->
printfn $"Unable to convert '{value}'."
// Displays:
// Unable to convert '1 345,978'.
Dim value As String
Dim style As NumberStyles
Dim culture As CultureInfo
Dim number As Double
' Parse currency value using en-GB culture.
value = "£1,097.63"
style = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
culture = CultureInfo.CreateSpecificCulture("en-GB")
If Double.TryParse(value, style, culture, number) Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
Console.WriteLine("Unable to convert '{0}'.", value)
End If
' Displays:
' Converted '£1,097.63' to 1097.63.
value = "1345,978"
style = NumberStyles.AllowDecimalPoint
culture = CultureInfo.CreateSpecificCulture("fr-FR")
If Double.TryParse(value, style, culture, number) Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
Console.WriteLine("Unable to convert '{0}'.", value)
End If
' Displays:
' Converted '1345,978' to 1345.978.
value = "1.345,978"
style = NumberStyles.AllowDecimalPoint Or NumberStyles.AllowThousands
culture = CultureInfo.CreateSpecificCulture("es-ES")
If Double.TryParse(value, style, culture, number) Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
Console.WriteLine("Unable to convert '{0}'.", value)
End If
' Displays:
' Converted '1.345,978' to 1345.978.
value = "1 345,978"
If Double.TryParse(value, style, culture, number) Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
Console.WriteLine("Unable to convert '{0}'.", value)
End If
' Displays:
' Unable to convert '1 345,978'.
注解
在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到 PositiveInfinity IEEE NegativeInfinity 754 规范的要求。 在早期版本中(包括 .NET Framework)中,分析太大而无法表示的值会导致失败。
方法 TryParse 类似于 Parse(String, NumberStyles, IFormatProvider) 方法,但此方法在转换失败时不会引发异常。 如果转换成功,则返回值为 true
, result
参数设置为转换结果。 如果转换失败,则返回值为 false
, result
参数设置为零。 这样就无需在无效且无法成功分析的情况下s
使用异常处理来测试 FormatException 。
参数 style
定义参数的允许格式, s
以便分析操作成功。 它必须是枚举中的位标志 NumberStyles 的组合。 不支持以下 NumberStyles 成员:
对于 s
指示provider
的区域性, 参数可以包含 NumberFormatInfo.PositiveInfinitySymbol、 NumberFormatInfo.NegativeInfinitySymbol或 NumberFormatInfo.NaNSymbol 。 此外,根据 的值 style
, s
参数可能包括以下元素:
[ws][$][sign][integral-digits,]integral-digits[.fractional-digits][e[sign]exponential-digits][ws]
方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 如果 style 包含 标志,s NumberStyles.AllowLeadingWhite则 开头会显示空格。 如果style 包含 NumberStyles.AllowTrailingWhite 标志,则它可以显示在 的末尾s 。 |
$ | 区域性特定的货币符号。 它在字符串中的位置由 NumberFormatInfo.CurrencyNegativePattern 参数的 方法provider 返回IFormatProvider.GetFormat的 NumberFormatInfo 对象的 或 NumberFormatInfo.CurrencyPositivePattern 属性定义。 如果style 包含 标志,NumberStyles.AllowCurrencySymbol则可以显示s 货币符号。 |
sign | 可选符号。 如果style 包含 标志,则符号可以出现在 的s 开头,如果style 包含 NumberStyles.AllowTrailingSign 标志,则它可以显示在 s NumberStyles.AllowLeadingSign 末尾。 如果style 包含 NumberStyles.AllowParentheses 标志,则可以在 中使用s 括号来指示负值。 |
integral-digits | 一系列数字,范围从 0 到 9,指定数字的整型部分。 如果有小数位数,则整型数字可能不存在。 |
, | 区域性特定的千位分隔符。 如果包含 标志,则当前区域性的千位分隔符可以出现在 中s NumberStyles.AllowThousands。style |
. | 区域性特定的小数点符号。 如果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 样式) 在类型范围内 Double 时始终成功分析。 其余 System.Globalization.NumberStyles 成员控制可能为 但不需要存在于输入字符串中的元素。 下表指示各个 NumberStyles 标志如何影响 中可能存在 s
的元素。
NumberStyles 值 | 除了数字外,还允许 在 中 |
---|---|
None | 仅限 整型数字 元素。 |
AllowDecimalPoint | 和小数位数元素。 |
AllowExponent | 参数 s 还可以使用指数表示法。 此标志本身支持 整数数字E指数数字形式的值;若要使用正数或负号和小数点符号等元素成功分析指数表示法中的字符串,还需要其他标志。 |
AllowLeadingWhite | 开头的 s ws 元素。 |
AllowTrailingWhite | 位于 末尾的 s ws 元素。 |
AllowLeadingSign | 开头的s 符号元素。 |
AllowTrailingSign | 末尾的s 符号元素。 |
AllowParentheses | 用括号括住数值的 符号 元素。 |
AllowThousands | 、 元素。 |
AllowCurrencySymbol | $ 元素。 |
Currency | 全部。 参数 s 不能表示十六进制数或指数表示法中的数字。 |
Float | 位于 开头或末尾的 s ws元素,符号位于 的s 开头,符号为 。 参数 s 还可以使用指数表示法。 |
Number | ws 、sign 、千分隔符 (、) 和小数点 (.) 元素。 |
Any | 所有样式(除外 s )都不能表示十六进制数。 |
参数 provider
是实现 IFormatProvider ,如 NumberFormatInfo 或 CultureInfo 对象。 参数 provider
提供分析中使用的特定于区域性的信息。 如果 provider
无法 null
获取 或 NumberFormatInfo 对象,则使用当前区域性的格式信息。
如果 s
参数为 null
或不是数值、 provider
参数不生成 NumberFormatInfo 对象或 style
参数不是枚举中的位标志 NumberStyles 的组合,则转换失败。
通常,如果向方法传递 Double.TryParse 通过调用 Double.ToString 方法创建的字符串,则返回原始 Double 值。 但是,由于精度损失,这些值可能不相等。 此外,尝试分析 或 Double.MaxValue 的Double.MinValue字符串表示形式无法往返。 在 .NET Framework 和 .NET Core 2.2 及早期版本上,它会引发 OverflowException。 在 .NET Core 3.0 及更高版本中,如果尝试分析 或Double.PositiveInfinity尝试分析 MaxValueMinValue ,则返回 Double.NegativeInfinity 。 下面的示例进行了这方面的演示。
using System;
public class Example
{
public static void Main()
{
string value;
double number;
value = Double.MinValue.ToString();
if (Double.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("{0} is outside the range of a Double.",
value);
value = Double.MaxValue.ToString();
if (Double.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("{0} is outside the range of a Double.",
value);
}
}
// The example displays the following output:
// -1.79769313486232E+308 is outside the range of the Double type.
// 1.79769313486232E+308 is outside the range of the Double type.
open System
[<EntryPoint>]
let main _ =
let value = string Double.MinValue
match Double.TryParse value with
| true, number ->
printfn $"{number}"
| _ ->
printfn $"{value} is outside the range of a Double."
let value = string Double.MaxValue
match Double.TryParse value with
| true, number ->
printfn $"{number}"
| _ ->
printfn $"{value} is outside the range of a Double."
0
// The example displays the following output:
// -1.79769313486232E+308 is outside the range of the Double type.
// 1.79769313486232E+308 is outside the range of the Double type.
Module Example
Public Sub Main()
Dim value As String
Dim number As Double
value = Double.MinValue.ToString()
If Double.TryParse(value, number) Then
Console.WriteLine(number)
Else
Console.WriteLine("{0} is outside the range of a Double.", _
value)
End If
value = Double.MaxValue.ToString()
If Double.TryParse(value, number) Then
Console.WriteLine(number)
Else
Console.WriteLine("{0} is outside the range of a Double.", _
value)
End If
End Sub
End Module
' The example displays the following output:
' -1.79769313486232E+308 is outside the range of the Double type.
' 1.79769313486232E+308 is outside the range of the Double type.
在 .NET Framework 和 .NET Core 2.2 及更早版本上,如果 s
超过数据类型的范围 Double ,该方法 Double.TryParse(String, NumberStyles, IFormatProvider, Double) 将 OverflowException引发 。
在 .NET Core 3.0 及更高版本上,当 数据类型范围Double外时s
,不会引发异常。 在大多数情况下, Double.TryParse(String, NumberStyles, IFormatProvider, Double) 方法计算 或 Double.NegativeInfinity的结果Double.PositiveInfinity。 但是,有一小部分值被认为更接近 的最大值或最小值 Double ,而不是正无穷大或负无穷大。 在这些情况下, 方法计算 或 Double.MinValue的结果Double.MaxValue。
如果在分析操作期间在 参数中 s
遇到分隔符,并且适用的货币或数字十进制分隔符和组分隔符相同,则分析操作假定该分隔符是小数分隔符而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparator和 NumberGroupSeparator。
另请参阅
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈