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)
- 來源:
- 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
參數為 null
或 Empty,或者不是有效格式的數字,則轉換會失敗。 如果 代表小於Double.MinValue 或大於 Double.MaxValue的數位,.NET Framework s
和 .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.PositiveInfinitySymbol 、 NumberFormatInfo.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 或 Double.PositiveInfinity 嘗試剖析 MaxValue ,則會傳回 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 、 NumberDecimalSeparatorCurrencyGroupSeparator 和 NumberGroupSeparator 。
另請參閱
- Parse(String)
- ToString()
- 在 .NET 中剖析數值字串
- Sample: .NET Core WinForms Formatting Utility (C#) (範例:.NET Core WinForms 格式化公用程式 (C#))
- Sample: .NET Core WinForms Formatting Utility (Visual Basic) (範例:.NET Core WinForms 格式化公用程式 (Visual Basic))
適用於
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
數值或符號相等的雙精確度浮點數。 如果 utf8Text
為 Empty 或 不是有效格式,轉換就會失敗。 此參數會以未初始化的狀態來傳遞,並會覆寫任何原本在結果中提供的值。
傳回
如果 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
的允許格式。 所要指定一般值為 Float 和 AllowThousands 的組合。
- 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
的允許格式。 所要指定一般值為 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
中原始提供的任何值都將遭到覆寫。
傳回
如果 s
轉換成功,則為 true
,否則為 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 和更新版本中,無法表示的值會四捨五入或 PositiveInfinityNegativeInfinity IEEE 754 規格所需的值。 在舊版中,包括.NET Framework,剖析太大而無法表示的值會導致失敗。
方法 TryParse 就像 Parse(String, NumberStyles, IFormatProvider) 方法一樣,但這個方法不會在轉換失敗時擲回例外狀況。 如果轉換成功,則傳回值為 true
,而 result
參數會設定為轉換的結果。 如果轉換失敗,則傳回值為 false
,而 result
參數會設定為零。 這樣就不需要使用例外狀況處理來測試 FormatException 事件 s
中無效且無法成功剖析的 。
參數 style
會定義參數的允許格式 s
,讓剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。 不支援下列 NumberStyles 成員:
參數 s
可以包含 NumberFormatInfo.PositiveInfinitySymbol 、 NumberFormatInfo.NegativeInfinitySymbol 或 NumberFormatInfo.NaNSymbol 表示的文化特性 provider
。 此外,根據 的值 style
, s
參數可能包含下列元素:
[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 屬性 NumberFormatInfo 所 IFormatProvider.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 | .和fractional-digits元素。 |
AllowExponent | 參數 s 也可以使用指數標記法。 這個旗標本身支援整數 數位E指數數位形式的值;需要額外的旗標,才能使用正數或負號和小數點符號等元素,成功剖析指數標記法中的字串。 |
AllowLeadingWhite | 開頭的 s ws元素。 |
AllowTrailingWhite | 結尾處的 s ws專案。 |
AllowLeadingSign | 開頭的 s 符號專案。 |
AllowTrailingSign | 結尾的 s 符號專案。 |
AllowParentheses | 以括弧括住數值形式的 sign 元素。 |
AllowThousands | 、專案。 |
AllowCurrencySymbol | $ 項目。 |
Currency | 全部。 參數 s 不能代表十六進位數或指數標記法中的數位。 |
Float | 開頭或結尾的 s ws元素,在開頭 s 和 的符號。 參數 s 也可以使用指數標記法。 |
Number | ws 、、 sign 千位分隔符號 (、) 和小數點 (。) 元素。 |
Any | 除了 以外的 s 所有樣式都不能代表十六進位數位。 |
參數 provider
是 實 IFormatProvider 作,例如 NumberFormatInfo 或 CultureInfo 物件。 參數 provider
提供剖析中使用的特定文化特性資訊。 如果 無法取得 或 provider
null
NumberFormatInfo 物件,則會使用目前文化特性的格式資訊。
如果 s
參數不是 null
數值、參數不會產生 NumberFormatInfo 物件, provider
或 style
參數不是列舉 NumberStyles 中的位旗標組合,轉換就會失敗。
一般而言,如果您傳遞 Double.TryParse 方法以呼叫 方法所建立的 Double.ToString 字串,則會傳回原始 Double 值。 不過,由於遺失精確度,值可能不相等。 此外,嘗試剖析 或 Double.MaxValue 的字串表示 Double.MinValue 無法來回。 在 .NET Framework 和 .NET Core 2.2 和舊版上,它會擲回 OverflowException 。 在 .NET Core 3.0 和更新版本上,如果您嘗試剖析 MinValue 或 Double.PositiveInfinity 嘗試剖析 MaxValue ,則會傳回 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 、 NumberDecimalSeparatorCurrencyGroupSeparator 和 NumberGroupSeparator 。
另請參閱
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應