Decimal.TryParse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將數字的字串表示,轉換為其相等的 Decimal。 傳回值,該值指出轉換成功或失敗。
多載
TryParse(ReadOnlySpan<Byte>, Decimal) |
嘗試將包含數位字串表示的 UTF-8 字元範圍轉換為其帶正負號十進位的對等專案。 |
TryParse(ReadOnlySpan<Char>, Decimal) |
使用指定樣式和特定文化特性格式,將數字的範圍表示轉換為其對等的 Decimal。 傳回值,該值指出轉換成功或失敗。 |
TryParse(String, Decimal) |
將數字的字串表示,轉換為其相等的 Decimal。 傳回值,該值指出轉換成功或失敗。 |
TryParse(ReadOnlySpan<Byte>, IFormatProvider, Decimal) |
嘗試將 UTF-8 字元的範圍剖析為值。 |
TryParse(ReadOnlySpan<Char>, IFormatProvider, Decimal) |
嘗試將字元範圍剖析成值。 |
TryParse(String, IFormatProvider, Decimal) |
嘗試將字串剖析成值。 |
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Decimal) |
嘗試將 UTF-8 字元的範圍剖析為值。 |
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Decimal) |
使用指定樣式和特定文化特性格式,將數字的範圍表示轉換為其對等的 Decimal。 傳回值,該值指出轉換成功或失敗。 |
TryParse(String, NumberStyles, IFormatProvider, Decimal) |
使用指定的樣式和特定文化特性格式,將數字的字串表示轉換為其對等的 Decimal。 傳回值,該值指出轉換成功或失敗。 |
TryParse(ReadOnlySpan<Byte>, Decimal)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
嘗試將包含數位字串表示的 UTF-8 字元範圍轉換為其帶正負號十進位的對等專案。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] System::Decimal % result);
public static bool TryParse (ReadOnlySpan<byte> utf8Text, out decimal result);
static member TryParse : ReadOnlySpan<byte> * decimal -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As Decimal) As Boolean
參數
- utf8Text
- ReadOnlySpan<Byte>
範圍,包含代表要轉換之數位的 UTF-8 字元。
- result
- Decimal
當這個方法傳回時,會包含與轉換成功時所包含的 utf8Text
數位相等的帶正負號十進位值,如果轉換失敗,則為零。 此參數會以未初始化的狀態來傳遞,並會覆寫任何原本在結果中提供的值。
傳回
如果 utf8Text
轉換成功,則為 true
,否則為 false
。
適用於
TryParse(ReadOnlySpan<Char>, Decimal)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
使用指定樣式和特定文化特性格式,將數字的範圍表示轉換為其對等的 Decimal。 傳回值,該值指出轉換成功或失敗。
public:
static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] System::Decimal % result);
public static bool TryParse (ReadOnlySpan<char> s, out decimal result);
static member TryParse : ReadOnlySpan<char> * decimal -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As Decimal) As Boolean
參數
- s
- ReadOnlySpan<Char>
範圍,其包含代表所要轉換數字的字元。
- result
- Decimal
當此方法傳回時,包含相當於 s
中所包含數值的 Decimal 數,或若轉換失敗,則傳回零。 如果 s
參數為 null
或 Empty ,則轉換失敗,其格式 style
不符合 ,或代表小於Decimal.MinValue 或大於 Decimal.MaxValue的數位。 這個參數未初始化便傳遞,result
中原始提供的任何值都會被覆寫。
傳回
如果 s
轉換成功,則為 true
,否則為 false
。
適用於
TryParse(String, Decimal)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
將數字的字串表示,轉換為其相等的 Decimal。 傳回值,該值指出轉換成功或失敗。
public:
static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] System::Decimal % result);
public static bool TryParse (string s, out decimal result);
public static bool TryParse (string? s, out decimal result);
static member TryParse : string * decimal -> bool
Public Shared Function TryParse (s As String, ByRef result As Decimal) As Boolean
參數
- s
- String
要轉換的數字的字串表示。
- result
- Decimal
當此方法傳回時,包含相當於 s
中所包含數值的 Decimal 數,或若轉換失敗,則傳回零。 如果 s
參數為 null
或 Empty ,不是有效格式的數位,或代表小於Decimal.MinValue 或大於 Decimal.MaxValue的數位,則轉換會失敗。 這個參數未初始化便傳遞,result
中原始提供的任何值都會被覆寫。
傳回
如果 s
轉換成功,則為 true
,否則為 false
。
範例
下列範例會使用 方法, Decimal.TryParse(String, Decimal) 將數值的字串表示轉換成 Decimal 值。 它假設 en-US 是目前的文化特性。
string value;
decimal number;
// Parse a floating-point value with a thousands separator.
value = "1,643.57";
if (Decimal.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
// Parse a floating-point value with a currency symbol and a
// thousands separator.
value = "$1,643.57";
if (Decimal.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
// Parse value in exponential notation.
value = "-1.643e6";
if (Decimal.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
// Parse a negative integer value.
value = "-1689346178821";
if (Decimal.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
// The example displays the following output to the console:
// 1643.57
// Unable to parse '$1,643.57'.
// Unable to parse '-1.643e6'.
// -1689346178821
// Parse a floating-point value with a thousands separator.
let value = "1,643.57"
match Decimal.TryParse value with
| true, number ->
printfn $"{number}"
| _ ->
printfn $"Unable to parse '{value}'."
// Parse a floating-point value with a currency symbol and a
// thousands separator.
let value = "$1,643.57"
match Decimal.TryParse value with
| true, number ->
printfn $"{number}"
| _ ->
printfn $"Unable to parse '{value}'."
// Parse value in exponential notation.
let value = "-1.643e6"
match Decimal.TryParse value with
| true, number ->
printfn $"{number}"
| _ ->
printfn $"Unable to parse '{value}'."
// Parse a negative integer value.
let value = "-1689346178821"
match Decimal.TryParse value with
| true, number ->
printfn $"{number}"
| _ ->
printfn $"Unable to parse '{value}'."
// The example displays the following output to the console:
// 1643.57
// Unable to parse '$1,643.57'.
// Unable to parse '-1.643e6'.
// -1689346178821
Dim value As String
Dim number As Decimal
' Parse a floating-point value with a thousands separator.
value = "1,643.57"
If Decimal.TryParse(value, number) Then
Console.WriteLine(number)
Else
Console.WriteLine("Unable to parse '{0}'.", value)
End If
' Parse a floating-point value with a currency symbol and a
' thousands separator.
value = "$1,643.57"
If Decimal.TryParse(value, number) Then
Console.WriteLine(number)
Else
Console.WriteLine("Unable to parse '{0}'.", value)
End If
' Parse value in exponential notation.
value = "-1.643e6"
If Decimal.TryParse(value, number)
Console.WriteLine(number)
Else
Console.WriteLine("Unable to parse '{0}'.", value)
End If
' Parse a negative integer value.
value = "-1689346178821"
If Decimal.TryParse(value, number)
Console.WriteLine(number)
Else
Console.WriteLine("Unable to parse '{0}'.", value)
End If
' The example displays the following output to the console:
' 1643.57
' Unable to parse '$1,643.57'.
' Unable to parse '-1.643e6'.
' -1689346178821
備註
這個多載與 Decimal.Parse(String) 方法不同,方法是傳回布林值,指出剖析作業是否成功,而不是傳回剖析的數值。 它不需要使用例外狀況處理來測試 FormatException 事件 s
中無效且無法成功剖析的 。
參數 s
包含一些表單:
[ws][sign][digits,]digits[.fractional-digits][ws]
在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。
元素 | 描述 |
---|---|
ws | 選擇性空白字元。 |
簽署 | 選擇性符號。 |
數字 | 介於 0 到 9 的數位序列。 |
, | 特定文化特性的千位分隔符號符號。 |
. | 特定文化特性的小數點符號。 |
fractional-digits | 介於 0 到 9 的數位序列。 |
參數 s
是使用 NumberStyles.Number 樣式來解譯。 這表示允許空白字元和千位分隔符號,但貨幣符號則不允許。 若要明確定義元素 (,例如貨幣符號、千位分隔符號和空白字元,) 可以存在於 中 s
,請使用 Decimal.TryParse(String, NumberStyles, IFormatProvider, Decimal) 方法多載。
參數 s
是使用針對目前系統文化特性初始化之 物件中的 NumberFormatInfo 格式資訊進行剖析。 如需詳細資訊,請參閱CurrentInfo。 若要使用其他指定文化特性的格式資訊剖析字串,請使用 Decimal.TryParse(String, NumberStyles, IFormatProvider, Decimal) 方法多載。
如有必要,的值 s
會使用四捨五入到最接近的四捨五入。
物件 Decimal 具有 29 位數的有效位數。 如果 s
代表數位超過 29 位數,但具有小數部分,且位於 和 MinValue 的範圍內 MaxValue ,則數位會四捨五入,而不是截斷為 29 位數,使用四捨五入為最接近的數位。
如果在剖析作業期間遇到參數中的 s
分隔符號,且適用的貨幣或數位十進位和群組分隔符號相同,則剖析作業會假設分隔符號是小數分隔符號,而不是群組分隔符號。 如需分隔符號的詳細資訊,請參閱 CurrencyDecimalSeparator 、 NumberDecimalSeparatorCurrencyGroupSeparator 、 和 NumberGroupSeparator 。
另請參閱
適用於
TryParse(ReadOnlySpan<Byte>, IFormatProvider, Decimal)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
嘗試將 UTF-8 字元的範圍剖析為值。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Decimal % result) = IUtf8SpanParsable<System::Decimal>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out decimal result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * decimal -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As Decimal) As Boolean
參數
- utf8Text
- ReadOnlySpan<Byte>
要剖析的 UTF-8 字元範圍。
- provider
- IFormatProvider
提供關於 utf8Text
之特定文化特性格式資訊的物件。
- result
- Decimal
傳回時,包含成功剖析 utf8Text
或失敗時未定義值的結果。
傳回
true
如果 utf8Text
已成功剖析,則為 ,否則為 false
。
適用於
TryParse(ReadOnlySpan<Char>, IFormatProvider, Decimal)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
嘗試將字元範圍剖析成值。
public:
static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Decimal % result) = ISpanParsable<System::Decimal>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out decimal result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * decimal -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As Decimal) As Boolean
參數
- s
- ReadOnlySpan<Char>
要剖析的字元範圍。
- provider
- IFormatProvider
提供關於 s
之特定文化特性格式資訊的物件。
- result
- Decimal
當這個方法傳回時,會包含成功剖析 s
的結果,或失敗時未定義的值。
傳回
true
如果 s
已成功剖析,則為 ,否則為 false
。
適用於
TryParse(String, IFormatProvider, Decimal)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
嘗試將字串剖析成值。
public:
static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Decimal % result) = IParsable<System::Decimal>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out decimal result);
static member TryParse : string * IFormatProvider * decimal -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As Decimal) As Boolean
參數
- s
- String
要剖析的字串。
- provider
- IFormatProvider
提供關於 s
之特定文化特性格式資訊的物件。
- result
- Decimal
當這個方法傳回時,包含成功剖析 s
或失敗時未定義值的結果。
傳回
true
如果 s
已成功剖析,則為 ,否則為 false
。
適用於
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Decimal)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
嘗試將 UTF-8 字元的範圍剖析為值。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Decimal % result) = System::Numerics::INumberBase<System::Decimal>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out decimal result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * decimal -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As Decimal) As Boolean
參數
- utf8Text
- ReadOnlySpan<Byte>
要剖析的 UTF-8 字元範圍。
- style
- NumberStyles
數位樣式的位元組合,可以存在於 中 utf8Text
。
- provider
- IFormatProvider
提供關於 utf8Text
之特定文化特性格式資訊的物件。
- result
- Decimal
傳回時,包含成功剖析 utf8Text
或失敗時未定義值的結果。
傳回
true
如果 utf8Text
已成功剖析,則為 ,否則為 false
。
適用於
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Decimal)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
使用指定樣式和特定文化特性格式,將數字的範圍表示轉換為其對等的 Decimal。 傳回值,該值指出轉換成功或失敗。
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Decimal % result);
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Decimal % result) = System::Numerics::INumberBase<System::Decimal>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out decimal result);
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out decimal result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * decimal -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As Decimal) As Boolean
參數
- s
- ReadOnlySpan<Char>
範圍,其包含代表所要轉換數字的字元。
- style
- NumberStyles
列舉值的位元組合,其表示 s
所允許的格式。 一般會指定的值是 Number。
- provider
- IFormatProvider
物件,提供 s
的相關特定文化特性剖析資訊。
- result
- Decimal
當此方法傳回時,包含相當於 s
中所包含數值的 Decimal 數,或若轉換失敗,則傳回零。 如果 s
參數為 null
或 Empty ,則轉換失敗,其格式 style
不符合 ,或代表小於Decimal.MinValue 或大於 Decimal.MaxValue的數位。 這個參數未初始化便傳遞,result
中原始提供的任何值都會被覆寫。
傳回
如果 s
轉換成功,則為 true
,否則為 false
。
適用於
TryParse(String, NumberStyles, IFormatProvider, Decimal)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
使用指定的樣式和特定文化特性格式,將數字的字串表示轉換為其對等的 Decimal。 傳回值,該值指出轉換成功或失敗。
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Decimal % result);
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Decimal % result) = System::Numerics::INumberBase<System::Decimal>::TryParse;
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out decimal result);
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out decimal result);
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * decimal -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As Decimal) As Boolean
參數
- s
- String
要轉換的數字的字串表示。
- style
- NumberStyles
列舉值的位元組合,其表示 s
所允許的格式。 一般會指定的值是 Number。
- provider
- IFormatProvider
物件,提供 s
的相關特定文化特性剖析資訊。
- result
- Decimal
當此方法傳回時,包含相當於 s
中所包含數值的 Decimal 數,或若轉換失敗,則傳回零。 如果 s
參數為 null
或 Empty ,則轉換失敗,其格式 style
不符合 ,或代表小於Decimal.MinValue 或大於 Decimal.MaxValue的數位。 這個參數未初始化便傳遞,result
中原始提供的任何值都會被覆寫。
傳回
如果 s
轉換成功,則為 true
,否則為 false
。
例外狀況
範例
下列範例示範如何使用 TryParse(String, NumberStyles, IFormatProvider, Decimal) 方法來剖析具有特定樣式的數位字串表示,並使用特定文化特性的慣例來格式化。
string value;
NumberStyles style;
CultureInfo culture;
decimal number;
// Parse currency value using en-GB culture.
value = "£1,097.63";
style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
culture = CultureInfo.CreateSpecificCulture("en-GB");
if (Decimal.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 (Decimal.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 (Decimal.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 (Decimal.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 Decimal.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 Decimal.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 Decimal.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 Decimal.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 Decimal
' Parse currency value using en-GB culture.
value = "£1,097.63"
style = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
culture = CultureInfo.CreateSpecificCulture("en-GB")
If Decimal.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 Decimal.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 Decimal.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 Decimal.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'.
備註
這個多載與 Decimal.Parse(String, NumberStyles, IFormatProvider) 方法不同,方法是傳回布林值,指出剖析作業是否成功,而不是傳回剖析的數值。 它不需要使用例外狀況處理來測試 FormatException 事件 s
中無效且無法成功剖析的 。
參數 style
會定義參數的允許格式 s
,讓剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。 不支援下列 NumberStyles 成員:
根據樣式的值, s
參數可能包含下列元素:
[ws][$][sign][digits,]digits[.fractional-digits][e[sign]digits][ws]
在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。
元素 | 描述 |
---|---|
ws | 選擇性空白字元。 如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,則空白字元可以出現在 的 s 開頭。 如果 style 包含 旗標, NumberStyles.AllowTrailingWhite 它可能會出現在 結尾 s 。 |
$ | 特定文化特性的貨幣符號。 字串中的位置是由 NumberFormatInfo.CurrencyNegativePattern 參數的 方法 provider 所傳回之 物件的 或 NumberFormatInfo.CurrencyPositivePattern 屬性 NumberFormatInfo 所 IFormatProvider.GetFormat 定義。 如果 style 包含 旗標, NumberStyles.AllowCurrencySymbol 貨幣符號就可以出現在 中 s 。 |
簽署 | 選擇性符號。 |
數字 | 介於 0 到 9 的數位序列。 |
. | 特定文化特性的小數點符號。 |
fractional-digits | 介於 0 到 9 的數位序列。 |
參數 style
會指定參數的允許格式 s
,而且可以是使用位 OR 運算結合的一或多個 NumberStyles 列舉常數。 如果 style
為 null, s
則會使用 NumberStyles.Number 樣式來解譯。
參數 provider
是 或 CultureInfo 物件之類的 NumberFormatInfo 實作 IFormatProvider 。 參數 provider
提供剖析中使用的文化特性特定資訊。 如果 provider
是 null
,則會使用執行緒目前的文化特性。
物件 Decimal 具有 29 位數的有效位數。 如果 s
代表數位超過 29 位數,但具有小數部分,且位於 和 MinValue 的範圍內 MaxValue ,則數位會四捨五入,而不是截斷為 29 位數,使用四捨五入為最接近的數位。
如果在剖析作業期間于 參數中 s
遇到分隔符號,且適用的貨幣或數位十進位和群組分隔符號相同,則剖析作業會假設分隔符號是小數分隔符號,而不是群組分隔符號。 如需分隔符號的詳細資訊,請參閱 CurrencyDecimalSeparator 、 NumberDecimalSeparatorCurrencyGroupSeparator 、 和 NumberGroupSeparator 。
另請參閱
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應