Double.TryParse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將數位的字串表示轉換為其對等的雙精確度浮點數。 傳回值表示轉換成功或失敗。
多載
| 名稱 | Description |
|---|---|
| TryParse(String, IFormatProvider, Double) |
嘗試將字串剖析成值。 |
| TryParse(ReadOnlySpan<Char>, Double) |
將指定樣式和特定文化特性格式的數位範圍表示轉換為其對等的雙精確度浮點數。 傳回值表示轉換成功或失敗。 |
| TryParse(String, Double) |
將數位的字串表示轉換為其對等的雙精確度浮點數。 傳回值表示轉換成功或失敗。 |
| TryParse(ReadOnlySpan<Byte>, IFormatProvider, Double) |
嘗試將UTF-8字元的範圍剖析為值。 |
| TryParse(ReadOnlySpan<Char>, 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 或 NegativeInfinity 依照 IEEE 754 規範的要求進行。 在舊版中,包括 .NET Framework,剖析的值太大而無法表示導致失敗。
TryParse(String, IFormatProvider, Double)
- 來源:
- Double.cs
- 來源:
- Double.cs
- 來源:
- 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<Char>, Double)
- 來源:
- Double.cs
- 來源:
- Double.cs
- 來源:
- 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 空,轉換失敗。 若 s 是小於 Double.MinValue 的有效數值,result則 。NegativeInfinity 若 s 是大於 Double.MaxValue 的有效數值,result則 。PositiveInfinity 此參數以未初始化的方式傳遞;原本輸入 result 的任何值都會被覆寫。
傳回
true若s成功轉換;否則,。 false
備註
在 .NET Core 3.0 及之後版本中,過大無法表示的數值會被四捨五入到 PositiveInfinity 或 NegativeInfinity 依照 IEEE 754 規範的要求進行。 在舊版中,包括 .NET Framework,剖析的值太大而無法表示導致失敗。
適用於
TryParse(String, Double)
- 來源:
- Double.cs
- 來源:
- Double.cs
- 來源:
- 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或不是數字,轉換即失敗。 在 .NET Framework 上,若 s 表示的數字小於 Double.MinValue 或大於 Double.MaxValue 也會失敗。 此參數以未初始化的方式傳遞;原本輸入 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 或 NegativeInfinity 依照 IEEE 754 規範的要求進行。 在舊版中,包括 .NET Framework,剖析的值太大而無法表示導致失敗。
此過載與方法 Double.Parse(String) 不同,回傳一個布林值以指示解析操作是否成功,而非回傳解析後的數值。 它消除了在無法成功解析的 event FormatException 時,使用例外處理來測試 a s 的必要。
參數s可包含當前文化的 NumberFormatInfo.PositiveInfinitySymbol、 或NumberFormatInfo.NegativeInfinitySymbolNumberFormatInfo.NaNSymbol符號。 此字串比較在 .NET Core 3.0 及以後版本中不區分大小寫,但在先前版本(包括 .NET Framework)中則不區分大小寫。 參數 s 也可以是如下形式的字串:
[ws][sign][integral-digits,]integral-digits[.[fractional-digits][e[sign]exponential-digits][ws]
方括弧中的元素是選擇性的。 下表描述每個元素。
| 元素 | 描述 |
|---|---|
| ws | 一系列空格符。 |
| 簽署 | 負號或正負號符號。 |
| 整數數字 | 一系列數值字元,範圍從 0 到 9,指定數位的整數部分。 如果有小數位數,整數位數就可能不存在。 |
| , | 特定文化特性的群組分隔符符號。 |
| . | 特定文化特性的小數點符號。 |
| 小數數字 | 一系列數值字元,範圍從 0 到 9,指定數位的小數部分。 |
| E | 表示指數型(科學)表示法的大寫或小寫字元 『e』。 |
| 指數位數 | 一系列數值字元,範圍從 0 到 9,指定指數。 |
欲了解更多數字格式資訊,請參閱 格式類型。
s參數是透過結合 NumberStyles.Float 和 NumberStyles.AllowThousands 標誌來解釋的。 這表示允許空格符和千位分隔符,但貨幣符號則不允許。 要明確定義 中可能存在 s的元素(如貨幣符號、數千個分隔符和空白),可以使用 Double.TryParse(String, NumberStyles, IFormatProvider, Double) 方法過載。
s參數是利用為當前系統文化初始化的物件格式資訊NumberFormatInfo進行解析。 如需詳細資訊,請參閱NumberFormatInfo.CurrentInfo。 若要解析字串,使用其他指定文化的格式資訊,請使用 Double.TryParse(String, NumberStyles, IFormatProvider, Double) 方法超載。
通常,如果你傳 Double.TryParse 給方法一個由呼叫 Double.ToString 該方法所產生的字串, Double 原始值就會被回傳。 然而,由於精度損失,這些數值可能不相等。 此外,嘗試解析 或 Double.MinValueDouble.MaxValue 的字串表示無法進行往返。 在 .NET Framework 上,它會拋出一個 OverflowException. 在 .NET Core 3.0 及更新版本中,嘗試MinValue解析 或嘗試解析 時MaxValueDouble.PositiveInfinity,會返回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 中,如果 s 超出資料型別範圍 Double ,該 TryParse(String, Double) 方法會拋出一個 OverflowException。
在 .NET Core 3.0 及更新版本中,當 s 超出資料型別範圍 Double 時,不會拋出例外。 在大多數情況下,該TryParse(String, Double)方法計算出或Double.PositiveInfinityDouble.NegativeInfinity的結果。 然而,有一小組被認為更接近 的最大 Double 值或最小值,而非正無窮或負無限大。 在這些情況下,方法計算出或Double.MaxValueDouble.MinValue的結果。
若在解析操作中遇到參數分 s 隔符,且十進位與群分隔符相同,解析操作假設分隔符為十進位分隔符而非群分隔符。 欲了解更多分離子資訊,請參見 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparatorNumberGroupSeparator及 。
另請參閱
- Parse(String)
- ToString()
- .NET 中的解析數值字串
- 範例:.NET Core WinForms 格式化公用程式 (C#)
- 範例:.NET Core WinForms 格式化公用程式 (Visual Basic)
適用於
TryParse(ReadOnlySpan<Byte>, IFormatProvider, Double)
- 來源:
- Double.cs
- 來源:
- Double.cs
- 來源:
- 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(ReadOnlySpan<Char>, IFormatProvider, Double)
- 來源:
- Double.cs
- 來源:
- Double.cs
- 來源:
- 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<Byte>, NumberStyles, IFormatProvider, Double)
- 來源:
- Double.cs
- 來源:
- Double.cs
- 來源:
- 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
- 來源:
- 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有效或不正確,轉換即失敗。 這個參數會未初始化傳遞;將覆寫原本在結果中提供的任何值。
傳回
true若utf8Text成功轉換;否則,。 false
適用於
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Double)
- 來源:
- Double.cs
- 來源:
- Double.cs
- 來源:
- Double.cs
- 來源:
- Double.cs
- 來源:
- Double.cs
將包含指定樣式和特定文化特性格式之數位的字串表示的字元範圍轉換為其對等的雙精確度浮點數。 傳回值表示轉換成功或失敗。
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, [Runtime::InteropServices::Out] double % result);
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 或 NegativeInfinity 依照 IEEE 754 規範的要求進行。 在舊版中,包括 .NET Framework,剖析的值太大而無法表示導致失敗。
適用於
TryParse(String, NumberStyles, IFormatProvider, Double)
- 來源:
- Double.cs
- 來源:
- Double.cs
- 來源:
- 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。 典型的指定值是 Float 與 AllowThousands結合。
- provider
- IFormatProvider
提供IFormatProvider針對特定文化的格式資訊。s
- result
- Double
當此方法回傳時,包含一個雙精度浮點數,等同於 中 s所含的數值或符號(若轉換成功),若轉換失敗則為零。 若s參數格式nullEmpty不符合 style,或 不符合 ,或 style 不是列舉常數的有效組合NumberStyles,則轉換失敗。 在 .NET Framework 上,若 s 代表的數字小於 SByte.MinValue 或大於 SByte.MaxValue 也會失敗。 此參數以未初始化的方式傳遞;原本輸入 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 或 NegativeInfinity 依照 IEEE 754 規範的要求進行。 在舊版中,包括 .NET Framework,剖析的值太大而無法表示導致失敗。
此 TryParse 方法與 方法 Parse(String, NumberStyles, IFormatProvider) 相同,但若轉換失敗,該方法不會拋出例外。 若轉換成功,回傳值為 ,trueresult參數則設定為轉換結果。 若轉換失敗,回傳值為 , false 參數 result 設為零。 這消除了在無法成功解析且無效的事件FormatException時,使用例外處理來測試 a s 的需求。
參數 style 定義了該參數允許的格式 s ,以確保解析操作成功。 它必須是列舉中多個位元旗 NumberStyles 標的組合。 以下 NumberStyles 成員不獲支援:
s參數可包含 NumberFormatInfo.PositiveInfinitySymbol、 NumberFormatInfo.NegativeInfinitySymbol,或NumberFormatInfo.NaNSymbol由 表示provider的文化符號。 此字串比較在 .NET Core 3.0 及以後版本中不區分大小寫,但在先前版本(包括 .NET Framework)中則不區分大小寫。 此外,根據 的styles值,參數可能包含以下元素:
[ws][$][sign][integral-digits,]integral-digits[.fractional-digits][e[sign]exponential-digits][ws]
方括弧 ([ 和 ]) 中的元素是選擇性的。 下表描述每個元素。
| 元素 | 描述 |
|---|---|
| ws | 選擇性的空格符。 如果包含s旗幟,styleNumberStyles.AllowLeadingWhite開頭可能會出現空白。 它可能出現在包含旗幟的最後sstyle。NumberStyles.AllowTrailingWhite |
| $ | 特定文化特性的貨幣符號。 它在字串中的位置由NumberFormatInfo.CurrencyNegativePattern參數方法NumberFormatInfo.CurrencyPositivePattern回傳的NumberFormatInfo物件的 or IFormatProvider.GetFormat 屬性provider所定義。 貨幣s符號可出現style在包含NumberStyles.AllowCurrencySymbol旗幟時。 |
| 簽署 | 選擇性符號。 如果包含s旗幟,標誌可以出現在 的styleNumberStyles.AllowLeadingSign開頭,如果包含s該旗幟,則可以出現在 的結尾styleNumberStyles.AllowTrailingSign。 若s包含旗標,style則可使用NumberStyles.AllowParentheses括號表示負值。 |
| 整數數字 | 一系列數位,範圍從 0 到 9,指定數位的整數部分。 如果有小數位數,整數位數就可能不存在。 |
| , | 特定文化特性的千位分隔符符號。 當前s文化的千分符號若包含style國旗,則可出現NumberStyles.AllowThousands。 |
| . | 特定文化特性的小數點符號。 當前文化的小數點符號可以出現sstyle在包含NumberStyles.AllowDecimalPoint國旗的情況下。 |
| 小數數字 | 一系列數位,範圍從 0 到 9,指定數位的小數部分。 若s包含style旗幟,則可出現NumberStyles.AllowDecimalPoint小數數字。 |
| e | e 或 E 字元表示 , s 可以用指數符號表示一個數字。 如果風格包含旗幟,s參數NumberStyles.AllowExponent可以用指數符號表示數字。 |
| 指數位數 | 指定指數的一系列數位範圍從 0 到 9。 |
注意
任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。
只有數字的字串(對應 NumberStyles.None 於樣式)若在該類型範圍內 Double ,則能成功解析。 剩餘 System.Globalization.NumberStyles 成員控制輸入字串中可能存在但不必須存在的元素。 下表說明個別 NumberStyles 旗標如何影響可能存在於 s中的元素。
| NumberStyles 值 | 除了數位以外,允許的 元素 |
|---|---|
| None | 只有 整數數字 元素。 |
| AllowDecimalPoint | 以及 。 以及 小數數字 元素。 |
| AllowExponent | 參數 s 也可以使用指數符號。 此旗標本身支援整 數數字E 指數位數的數值;為了成功解析指數符號中的字串,如正負符號及小數點符號,則需要額外的旗標。 |
| AllowLeadingWhite | 開頭的 s 元素。 |
| AllowTrailingWhite | 末尾的 s 元素。 |
| AllowLeadingSign | 符號 元素位於 的開頭 s。 |
| AllowTrailingSign | 符號 元素位於 的結尾 s。 |
| AllowParentheses | 符號 元素以 括號形式包含數值。 |
| AllowThousands | 這個元素。 |
| AllowCurrencySymbol | 元素 $ 。 |
| Currency | 都。 該 s 參數無法代表十六進位數或指數符號中的數字。 |
| Float | ws 元素位於 的 s開頭或結 尾,符號 位於 s,以及 。 符號。 參數 s 也可以使用指數符號。 |
| Number | 、wssign、千分隔符(,)和小數點(.)元素。 |
| Any | 除了 s 不能代表十六進位數字外,所有樣式都無法代表。 |
參數 provider 是一個 IFormatProvider 實作,例如 a NumberFormatInfo 或 CultureInfo 物件。 參數 provider 提供解析中所需的特定文化資訊。 若 providernullNumberFormatInfo 無法取得物件或物件,則使用當前文化的格式資訊。
若 s 參數為 null 數值或非數值、 provider 參數未產生 NumberFormatInfo 物件,或 style 參數非列舉中位元旗 NumberStyles 標的組合,則轉換失敗。
通常,如果你傳 Double.TryParse 給方法一個由呼叫 Double.ToString 該方法所產生的字串, Double 原始值就會被回傳。 然而,由於精度損失,這些數值可能不相等。 此外,嘗試解析 或 Double.MinValueDouble.MaxValue 的字串表示無法進行往返。 在 .NET Framework 上,它會拋出一個 OverflowException. 在 .NET Core 3.0 及更新版本中,嘗試MinValue解析 或嘗試解析 時MaxValueDouble.PositiveInfinity,會返回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 中,如果 s 超出資料型別範圍 Double ,該 Double.TryParse(String, NumberStyles, IFormatProvider, Double) 方法會拋出一個 OverflowException。
在 .NET Core 3.0 及更新版本中,當 s 超出資料型別範圍 Double 時,不會拋出例外。 在大多數情況下,該Double.TryParse(String, NumberStyles, IFormatProvider, Double)方法計算出或Double.PositiveInfinityDouble.NegativeInfinity的結果。 然而,有一小組被認為更接近 的最大 Double 值或最小值,而非正無窮或負無限大。 在這些情況下,方法計算出或Double.MaxValueDouble.MinValue的結果。
若在解析操作中遇到參數分 s 隔符,且適用的貨幣或數字分隔符相同,則解析操作假設分隔符為十進位分隔符而非群分隔符。 欲了解更多分離子資訊,請參見 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparatorNumberGroupSeparator及 。