共用方式為


Double.TryParse 方法

定義

將數字的字串表示轉換為其相等的雙精確度浮點數。 傳回值,該值指出轉換成功或失敗。

多載

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 和更新版本中,無法表示的值會四捨五入或 PositiveInfinityNegativeInfinity IEEE 754 規格所需的值。 在舊版中,包括 .NET Framework,剖析太大而無法表示的值會導致失敗。

TryParse(ReadOnlySpan<Char>, IFormatProvider, Double)

來源:
Double.cs
來源:
Double.cs
來源:
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)

來源:
Double.cs
來源:
Double.cs
來源:
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 中原始提供的任何值都將遭到覆寫。

傳回

如果 s 轉換成功,則為 true,否則為 false

備註

在 .NET Core 3.0 和更新版本中,太大而無法表示的值會四捨五入為 PositiveInfinity IEEE 754 規格所需的或 NegativeInfinity 。 在舊版中,包括 .NET Framework,剖析太大而無法表示導致失敗的值。

適用於

TryParse(String, Double)

來源:
Double.cs
來源:
Double.cs
來源:
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 參數為 nullEmpty,或者不是有效格式的數字,則轉換會失敗。 如果 s 代表小於 Double.MinValue 或大於 Double.MaxValue 的數位,.NET Framework 和 .NET Core 2.2 和更早版本也會失敗。 這個參數未初始化便傳遞,result 中原始提供的任何值都將遭到覆寫。

傳回

如果 s 轉換成功,則為 true,否則為 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) ,方法是傳回布爾值,指出剖析作業是否成功,而不是傳回剖析的數值。 它不需要使用例外狀況處理,在無效且無法成功剖析的 事件s中測試 FormatException

參數 s 可以包含目前文化特性的 NumberFormatInfo.PositiveInfinitySymbolNumberFormatInfo.NegativeInfinitySymbol (NumberFormatInfo.NaNSymbol 字串比較會區分大小寫) 或格式的字串:

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

在方括號中的元素是選擇性的元素。 下表說明每個元素。

元素 描述
ws 一系列空格符。
簽署 負號或正負號符號。
整數數位 一系列數值字元,範圍從 0 到 9,指定數位的整數部分。 如果有小數位數,整數數位可能不存在。
, 特定文化特性的群組分隔符符號。
. 特定文化特性的小數點符號。
fractional-digits 一連串的數值字元,範圍從 0 到 9,指定數位的小數部分。
E 大寫或小寫字元 'e',表示指數 (科學) 表示法。
指數數位 一系列的數值字元,範圍從 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 和更新版本上,如果您嘗試剖析MinValue或嘗試剖析 MaxValue,則會傳回 Double.NegativeInfinityDouble.PositiveInfinity。 下列範例提供說明。

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 和更新版本上,當 超出數據類型的範圍Doubles,不會擲回例外狀況。 在大部分情況下,方法會TryParse(String, Double)計算 或Double.NegativeInfinity的結果Double.PositiveInfinity。 不過,有一組小的值會被視為比正或負無限大更接近最大值或最小值 Double 。 在這些情況下,方法會計算 或Double.MinValue的結果Double.MaxValue

如果在剖析作業期間於 參數中 s 遇到分隔符,而且小數和群組分隔符相同,則剖析作業會假設分隔符是小數分隔符,而不是群組分隔符。 如需分隔符的詳細資訊,請參閱CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparatorNumberGroupSeparator

另請參閱

適用於

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Double)

來源:
Double.cs
來源:
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)

來源:
Double.cs
來源:
Double.cs
來源:
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)

來源:
Double.cs
來源:
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)

來源:
Double.cs
來源:
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 數值或符號相等的雙精確度浮點數。 如果 utf8TextEmpty 或 不是有效格式,轉換就會失敗。 此參數會以未初始化的狀態來傳遞,並會覆寫任何原本在結果中提供的值。

傳回

如果 utf8Text 轉換成功,則為 true,否則為 false

適用於

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

來源:
Double.cs
來源:
Double.cs
來源:
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 的允許格式。 所要指定一般值為 FloatAllowThousands 的組合。

provider
IFormatProvider

物件,其提供關於 s 的特定文化特性格式資訊。

result
Double

當這個方法傳回時,若轉換成功,將會包含相當於 s 所含數值或符號的雙精確度浮點數。 若轉換失敗,則會包含零。 如果 s 參數為 null 或空字元範圍,或者其數字格式不符合 style 規範,則轉換會失敗。 如果 s 是小於 Double.MinValue 的有效數位, result 則 為 NegativeInfinity。 如果 s 是大於 Double.MaxValue 的有效數位, result 則 為 PositiveInfinity。 這個參數未初始化便傳遞,result 中原始提供的任何值都將遭到覆寫。

傳回

如果 s 轉換成功,則為 true,否則為 false

備註

在 .NET Core 3.0 和更新版本中,無法表示的值會四捨五入或 PositiveInfinityNegativeInfinity IEEE 754 規格所需的值。 在舊版中,包括 .NET Framework,剖析太大而無法表示的值會導致失敗。

適用於

TryParse(String, NumberStyles, IFormatProvider, Double)

來源:
Double.cs
來源:
Double.cs
來源:
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 的允許格式。 所要指定一般值為 FloatAllowThousands 的組合。

provider
IFormatProvider

IFormatProvider,提供 s 的相關特定文化特性格式資訊。

result
Double

當此方法傳回時,若轉換成功,包含相當於 s 中包含之數值或符號的雙精確度浮點數;若轉換失敗,則為零。 如果 s 參數是 nullEmpty、不是符合 style 規範的格式,或者 style 不是 NumberStyles 列舉常數的有效組合,則轉換會失敗。 如果s代表小於 SByte.MinValue 或大於 SByte.MaxValue 的數位,它也會在 .NET Framework 或 .NET Core 2.2 和舊版上失敗。 這個參數未初始化便傳遞,result 中原始提供的任何值都將遭到覆寫。

傳回

如果 s 轉換成功,則為 true,否則為 false

例外狀況

style 不是 NumberStyles 值。

-或-

style 包含 AllowHexSpecifier 值。

範例

下列範例示範如何使用 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 和更新版本中,無法表示的值會四捨五入或 PositiveInfinityNegativeInfinity IEEE 754 規格所需的值。 在舊版中,包括 .NET Framework,剖析太大而無法表示的值會導致失敗。

方法 TryParse 就像 Parse(String, NumberStyles, IFormatProvider) 方法一樣,但這個方法不會在轉換失敗時擲回例外狀況。 如果轉換成功,則傳回值為 true ,而 result 參數會設定為轉換的結果。 如果轉換失敗,則傳回值為 false ,而 result 參數會設定為零。 這樣就不需要使用例外狀況處理來測試 FormatException 事件 s 中無效且無法成功剖析的 。

參數 style 會定義參數的允許格式 s ,讓剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。 不支援下列 NumberStyles 成員:

參數 s 可以包含 NumberFormatInfo.PositiveInfinitySymbolNumberFormatInfo.NegativeInfinitySymbolNumberFormatInfo.NaNSymbol 表示的文化特性 provider。 此外,根據的值 styles 參數可能包含下列元素:

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

在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。

元素 描述
ws 選擇性空格符。 如果style包含 NumberStyles.AllowLeadingWhite 旗標,則空格符可以出現在的s開頭。 如果style包含旗標,NumberStyles.AllowTrailingWhite它可能會出現在 結尾s
$ 特定文化特性的貨幣符號。 字串中的位置是由 NumberFormatInfo.CurrencyNegativePattern 參數的 方法provider所傳回之 物件的 或 NumberFormatInfo.CurrencyPositivePattern 屬性NumberFormatInfoIFormatProvider.GetFormat定義。 如果style包含旗標,NumberStyles.AllowCurrencySymbol貨幣符號就可以出現在 中s
簽署 選擇性符號。 如果 style 包含旗標,NumberStyles.AllowLeadingSign則符號可以出現在的s開頭,如果style包含 NumberStyles.AllowTrailingSign 旗標,則它可能會出現在 結尾s。 如果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
指數數位 指定指數的一系列數位範圍從 0 到 9。

注意

不論自變數的值style為何,剖析作業都會忽略 中s任何終止的 NUL (U+0000) 字元。

只有數位的字串 (對應至 NumberStyles.None 樣式) 在類型範圍內 Double 時一律會成功剖析。 其餘 System.Globalization.NumberStyles 成員控件元素可能存在,但不需要出現在輸入字串中。 下表指出個別 NumberStyles 旗標如何影響 中 s可能存在的專案。

NumberStyles 值 除了數位之外,也允許的專案
None 僅限 整數數字元素
AllowDecimalPoint 小數位數元素
AllowExponent 參數 s 也可以使用指數表示法。 這個旗標本身支援 整數數位E指數數位格式的值;需要額外的旗標,才能使用正數或負號和小數點符號等元素,成功剖析指數表示法中的字串。
AllowLeadingWhite 開頭的 sws 元素。
AllowTrailingWhite 結尾處的 sws 元素。
AllowLeadingSign 開頭的s符號專案。
AllowTrailingSign 結尾處的s符號專案。
AllowParentheses 以括弧括住數值形式的 sign 元素。
AllowThousands 元素。
AllowCurrencySymbol $ 項目。
Currency 全部。 參數 s 不能代表十六進位數或指數表示法中的數位。
Float 開頭或結尾的 sws 元素,s開頭和 符號。 參數 s 也可以使用指數表示法。
Number wssign、thousands 分隔符 (、) 和小數點 ()
Any 除了以外的 s 所有樣式都不能代表十六進位數。

參數 provider 是實作 IFormatProvider ,例如 NumberFormatInfoCultureInfo 物件。 參數 provider 提供剖析中使用的文化特性特定資訊。 如果 provider 無法 null 取得 或 NumberFormatInfo 物件,則會使用目前文化特性的格式資訊。

如果s參數不是null數值、參數不會產生NumberFormatInfo物件,providerstyle參數不是列舉NumberStyles中的位旗標組合,則轉換會失敗。

一般而言,如果您傳遞 Double.TryParse 方法所建立 Double.ToString 的字串,則會傳回原始 Double 值。 不過,由於精確度遺失,值可能不相等。 此外,嘗試剖析 或 Double.MaxValue 無法往返的字串表示Double.MinValue。 在 .NET Framework 和 .NET Core 2.2 和舊版上,它會擲回 OverflowException。 在 .NET Core 3.0 和更新版本上,如果您嘗試剖析MinValue或嘗試剖MaxValue析 ,則會傳回 Double.NegativeInfinityDouble.PositiveInfinity。 下列範例提供說明。

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 和更新版本上,當超出數據類型的範圍Doubles,不會擲回例外狀況。 在大部分情況下,Double.TryParse(String, NumberStyles, IFormatProvider, Double)方法會計算 或Double.NegativeInfinity的結果Double.PositiveInfinity。 不過,有一組小的值會被視為比正或負無限大接近最大值或最小值 Double 。 在這些情況下,方法會計算 或Double.MinValue的結果Double.MaxValue

如果在剖析作業期間於 參數中 s 遇到分隔符,且適用的貨幣或數位十進位和群組分隔符相同,則剖析作業會假設分隔符是小數分隔符,而不是群組分隔符。 如需分隔符的詳細資訊,請參閱CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparator、 和 NumberGroupSeparator

另請參閱

適用於