Decimal.Parse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將數字的字串表示,轉換為其相等的 Decimal。
多載
Parse(String) |
將數字的字串表示,轉換為其相等的 Decimal。 |
Parse(ReadOnlySpan<Byte>, IFormatProvider) |
將 UTF-8 字元的範圍剖析為值。 |
Parse(ReadOnlySpan<Char>, IFormatProvider) |
將字元範圍剖析為值。 |
Parse(String, NumberStyles) |
將指定樣式中數字的字串表示轉換為其相等的 Decimal。 |
Parse(String, IFormatProvider) |
使用指定的特定文化特性格式資訊,將數字的字串表示轉換為其對等的 Decimal。 |
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) |
將 UTF-8 字元的範圍剖析為值。 |
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
使用指定樣式和特定文化特性格式,將數字的範圍表示轉換為其對等的 Decimal。 |
Parse(String, NumberStyles, IFormatProvider) |
使用指定的樣式和特定文化特性格式,將數字的字串表示轉換為其對等的 Decimal。 |
Parse(String)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
將數字的字串表示,轉換為其相等的 Decimal。
public:
static System::Decimal Parse(System::String ^ s);
public static decimal Parse (string s);
static member Parse : string -> decimal
Public Shared Function Parse (s As String) As Decimal
參數
- s
- String
要轉換的數字的字串表示。
傳回
與 s
中包含的數字相等的值。
例外狀況
s
為 null
。
s
的格式不正確。
範例
下列程式碼範例會 Parse(String) 使用 方法來剖析值的字串表示 Decimal 。
string value;
decimal number;
// Parse an integer with thousands separators.
value = "16,523,421";
number = Decimal.Parse(value);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
// '16,523,421' converted to 16523421.
// Parse a floating point value with thousands separators
value = "25,162.1378";
number = Decimal.Parse(value);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
// '25,162.1378' converted to 25162.1378.
// Parse a floating point number with US currency symbol.
value = "$16,321,421.75";
try
{
number = Decimal.Parse(value);
Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
// Unable to parse '$16,321,421.75'.
// Parse a number in exponential notation
value = "1.62345e-02";
try
{
number = Decimal.Parse(value);
Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
// Unable to parse '1.62345e-02'.
// Parse an integer with thousands separators.
let value = "16,523,421"
let number = Decimal.Parse value
printfn $"'{value}' converted to {number}."
// Displays:
// '16,523,421' converted to 16523421.
// Parse a floating point value with thousands separators
let value = "25,162.1378"
let number = Decimal.Parse value
printfn $"'{value}' converted to {number}."
// Displays:
// '25,162.1378' converted to 25162.1378.
// Parse a floating point number with US currency symbol.
let value = "$16,321,421.75"
try
let number = Decimal.Parse value
printfn $"'{value}' converted to {number}."
with :? FormatException ->
printfn $"Unable to parse '{value}'."
// Displays:
// Unable to parse '$16,321,421.75'.
// Parse a number in exponential notation
let value = "1.62345e-02"
try
let number = Decimal.Parse value
printfn $"'{value}' converted to {number}."
with :? FormatException ->
printfn $"Unable to parse '{value}'."
// Displays:
// Unable to parse '1.62345e-02'.
Dim value As String
Dim number As Decimal
' Parse an integer with thousands separators.
value = "16,523,421"
number = Decimal.Parse(value)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' '16,523,421' converted to 16523421.
' Parse a floating point value with thousands separators
value = "25,162.1378"
number = Decimal.Parse(value)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' '25,162.1378' converted to 25162.1378.
' Parse a floating point number with US currency symbol.
value = "$16,321,421.75"
Try
number = Decimal.Parse(value)
Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
' Unable to parse '$16,321,421.75'.
' Parse a number in exponential notation
value = "1.62345e-02"
Try
number = Decimal.Parse(value)
Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
' Unable to parse '1.62345e-02'.
備註
參數 s
包含一些表單:
[ws][sign][digits,]digits[.fractional-digits][ws]
在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。
元素 | 描述 |
---|---|
ws | 選擇性空白字元。 |
簽署 | 選擇性符號。 |
數字 | 介於 0 到 9 的數位序列。 |
, | 特定文化特性的千位分隔符號符號。 |
. | 特定文化特性的小數點符號。 |
fractional-digits | 介於 0 到 9 的數位序列。 |
參數 s
是使用 NumberStyles.Number 樣式來解譯。 這表示允許空白字元和千位分隔符號,但貨幣符號則不允許。 若要明確定義專案 (,例如貨幣符號、千位分隔符號和空白字元,) 可以存在於 中 s
,請使用 Decimal.Parse(String, NumberStyles) 或 Decimal.Parse(String, NumberStyles, IFormatProvider) 方法。
參數 s
是使用初始化為目前系統文化特性所初始化的格式 NumberFormatInfo 資訊進行剖析。 如需詳細資訊,請參閱CurrentInfo。 若要使用其他文化特性的格式資訊剖析字串,請使用 Decimal.Parse(String, IFormatProvider) 或 Decimal.Parse(String, NumberStyles, IFormatProvider) 方法。
如有必要,的值 s
會使用四捨五入到最接近的四捨五入。
Decimal有 29 位數的有效位數。 如果 s
代表數位超過 29 位數,但具有小數部分,且位於 和 MinValue 的範圍內 MaxValue ,則數位會四捨五入,而不是截斷為 29 位數,使用四捨五入為最接近的數位。
如果在剖析作業期間遇到參數中的 s
分隔符號,且適用的貨幣或數位十進位和群組分隔符號相同,則剖析作業會假設分隔符號是小數分隔符號,而不是群組分隔符號。 如需分隔符號的詳細資訊,請參閱 CurrencyDecimalSeparator 、 NumberDecimalSeparatorCurrencyGroupSeparator 、 和 NumberGroupSeparator 。
另請參閱
適用於
Parse(ReadOnlySpan<Byte>, IFormatProvider)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
將 UTF-8 字元的範圍剖析為值。
public:
static System::Decimal Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<System::Decimal>::Parse;
public static decimal Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> decimal
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As Decimal
參數
- utf8Text
- ReadOnlySpan<Byte>
要剖析的 UTF-8 字元範圍。
- provider
- IFormatProvider
提供關於 utf8Text
之特定文化特性格式資訊的物件。
傳回
剖析 utf8Text
的結果。
實作
適用於
Parse(ReadOnlySpan<Char>, IFormatProvider)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
將字元範圍剖析為值。
public:
static System::Decimal Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<System::Decimal>::Parse;
public static decimal Parse (ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> decimal
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Decimal
參數
- s
- ReadOnlySpan<Char>
要剖析的字元範圍。
- provider
- IFormatProvider
提供關於 s
之特定文化特性格式資訊的物件。
傳回
剖析 s
的結果。
實作
適用於
Parse(String, NumberStyles)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
將指定樣式中數字的字串表示轉換為其相等的 Decimal。
public:
static System::Decimal Parse(System::String ^ s, System::Globalization::NumberStyles style);
public static decimal Parse (string s, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> decimal
Public Shared Function Parse (s As String, style As NumberStyles) As Decimal
參數
- s
- String
要轉換的數字的字串表示。
- style
- NumberStyles
NumberStyles 值的位元組合,指出可出現在 s
中的樣式項目。 一般會指定的值是 Number。
傳回
Decimal 數字,等於包含在 s
中的數字,如同 style
所指定。
例外狀況
s
為 null
。
s
的格式不正確。
範例
下列程式碼範例會 Parse(String, NumberStyles) 使用 方法來剖析使用 en-US 文化特性來剖析值的字串表示 Decimal 。
string value;
decimal number;
NumberStyles style;
// Parse string with a floating point value using NumberStyles.None.
value = "8694.12";
style = NumberStyles.None;
try
{
number = Decimal.Parse(value, style);
Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
// Unable to parse '8694.12'.
// Parse string with a floating point value and allow decimal point.
style = NumberStyles.AllowDecimalPoint;
number = Decimal.Parse(value, style);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
// '8694.12' converted to 8694.12.
// Parse string with negative value in parentheses
value = "(1,789.34)";
style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands |
NumberStyles.AllowParentheses;
number = Decimal.Parse(value, style);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
// '(1,789.34)' converted to -1789.34.
// Parse string using Number style
value = " -17,623.49 ";
style = NumberStyles.Number;
number = Decimal.Parse(value, style);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
// ' -17,623.49 ' converted to -17623.49.
// Parse string with a floating point value using NumberStyles.None.
let value = "8694.12"
let style = NumberStyles.None
try
let number = Decimal.Parse(value, style)
printfn $"'{value}' converted to {number}."
with :? FormatException ->
printfn $"Unable to parse '{value}'."
// Displays:
// Unable to parse '8694.12'.
// Parse string with a floating point value and allow decimal point.
let style = NumberStyles.AllowDecimalPoint
let number = Decimal.Parse(value, style)
printfn $"'{value}' converted to {number}."
// Displays:
// '8694.12' converted to 8694.12.
// Parse string with negative value in parentheses
let value = "(1,789.34)"
let style =
NumberStyles.AllowDecimalPoint |||
NumberStyles.AllowThousands |||
NumberStyles.AllowParentheses
let number = Decimal.Parse(value, style)
printfn $"'{value}' converted to {number}."
// Displays:
// '(1,789.34)' converted to -1789.34.
// Parse string using Number style
let value = " -17,623.49 "
let style = NumberStyles.Number
let number = Decimal.Parse(value, style)
printfn $"'{value}' converted to {number}."
// Displays:
// ' -17,623.49 ' converted to -17623.49.
Dim value As String
Dim number As Decimal
Dim style As NumberStyles
' Parse string with a floating point value using NumberStyles.None.
value = "8694.12"
style = NumberStyles.None
Try
number = Decimal.Parse(value, style)
Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
' Unable to parse '8694.12'.
' Parse string with a floating point value and allow decimal point.
style = NumberStyles.AllowDecimalPoint
number = Decimal.Parse(value, style)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' '8694.12' converted to 8694.12.
' Parse string with negative value in parentheses
value = "(1,789.34)"
style = NumberStyles.AllowDecimalPoint Or NumberStyles.AllowThousands Or _
NumberStyles.AllowParentheses
number = Decimal.Parse(value, style)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' '(1,789.34)' converted to -1789.34.
' Parse string using Number style
value = " -17,623.49 "
style = NumberStyles.Number
number = Decimal.Parse(value, style)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' ' -17,623.49 ' converted to -17623.49.
備註
參數 style
會定義樣式專案 (,例如千位分隔符號、空白字元和貨幣符號,) 參數中 s
允許的樣式專案,讓剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。 不支援下列 NumberStyles 成員:
根據 的值 style
, s
參數可能包含下列元素:
[ws][$][sign][digits,]digits[.fractional-digits][e[sign]digits][ws]
在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。
元素 | 描述 |
---|---|
ws | 選擇性空白字元。 如果 style 包含 旗標, NumberStyles.AllowLeadingWhite 則空白字元可以出現在 的 s 開頭,如果 style 包含 NumberStyles.AllowTrailingWhite 旗標,則會出現在 結尾 s 。 |
$ | 特定文化特性的貨幣符號。 字串中的位置是由目前文化特性的 和 NumberFormatInfo.CurrencyPositivePattern 屬性所 NumberFormatInfo.CurrencyNegativePattern 定義。 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,則目前文化特性的貨幣符號可以出現在 s 中。 |
簽署 | 選擇性符號。 如果 style 包含 旗標, NumberStyles.AllowLeadingSign 則符號可以出現在 的 s 開頭,如果 style 包含 NumberStyles.AllowTrailingSign 旗標,則它可能會出現在 結尾 s 。 如果 style 包含 NumberStyles.AllowParentheses 旗標,可以使用 s 括弧來表示負值。 |
數字 | 介於 0 到 9 的數位序列。 |
, | 特定文化特性的千位分隔符號符號。 如果 style 包含 NumberStyles.AllowThousands 旗標,則目前文化特性的千位分隔符號可以出現在 中 s 。 |
. | 特定文化特性的小數點符號。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則目前文化特性的小數點符號可以出現在 中 s 。 |
fractional-digits | 介於 0 到 9 的數位序列。 只有包含 NumberStyles.AllowDecimalPoint 旗標時 style ,小數位數才會出現在 中 s 。 |
e | 'e' 或 'E' 字元,表示值是以指數標記法表示。 如果 style 包含 旗標,參數 s 可以表示指數標記法的數位 NumberStyles.AllowExponent 。 |
注意
不論引數的值 style
為何,剖析作業都會忽略 中 s
任何終止的 NUL (U+0000) 字元。
只有數位的字串 (對應至 None 樣式) 在類型範圍內 Decimal 時一律會成功剖析。 其餘 NumberStyles 成員控制項元素可能存在,但不需要出現在輸入字串中。 下表指出個別 NumberStyles 成員如何影響 中 s
可能存在的專案。
NumberStyles 值 | 除了數位之外,也允許的專案 |
---|---|
None | 僅限 digits 元素。 |
AllowDecimalPoint | 和小數位數元素。 |
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 所有樣式都不能代表十六進位數。 |
參數 s
會使用針對目前系統文化特性初始化的 物件中的 NumberFormatInfo 格式化資訊進行剖析。 如需詳細資訊,請參閱CurrentInfo。
Decimal有 29 位數的有效位數。 如果 s
代表數位超過 29 位數,但具有小數部分,且位於 和 MinValue 的範圍內 MaxValue ,則數位會四捨五入,而不是截斷為 29 位數,使用四捨五入為最接近的數位。
如果在剖析作業期間于 參數中 s
遇到分隔符號, styles
包括 NumberStyles.AllowThousands 和 NumberStyles.AllowDecimalPoint 值,且適用的貨幣或數位十進位和群組分隔符號相同,剖析作業會假設分隔符號是小數分隔符號,而不是群組分隔符號。 如需分隔符號的詳細資訊,請參閱 CurrencyDecimalSeparator 、 NumberDecimalSeparatorCurrencyGroupSeparator 、 和 NumberGroupSeparator 。
另請參閱
適用於
Parse(String, IFormatProvider)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
使用指定的特定文化特性格式資訊,將數字的字串表示轉換為其對等的 Decimal。
public:
static System::Decimal Parse(System::String ^ s, IFormatProvider ^ provider);
public:
static System::Decimal Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<System::Decimal>::Parse;
public static decimal Parse (string s, IFormatProvider provider);
public static decimal Parse (string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> decimal
Public Shared Function Parse (s As String, provider As IFormatProvider) As Decimal
參數
- s
- String
要轉換的數字的字串表示。
- provider
- IFormatProvider
IFormatProvider,提供關於 s
的特定文化特性剖析資訊。
傳回
Decimal 數字,等於包含在 s
中的數字,如同 provider
所指定。
實作
例外狀況
s
為 null
。
s
的格式不正確。
範例
下列範例是 Web 表單的按鈕 click 事件處理常式。 它會使用 屬性傳回的 HttpRequest.UserLanguages 陣列來判斷使用者的地區設定。 然後,它會具現化 CultureInfo 對應至該地區設定的物件。 NumberFormatInfo屬於該 CultureInfo 物件的 物件接著會傳遞至 方法, Parse(String, IFormatProvider) 以將使用者的輸入轉換為 Decimal 值。
protected void OkToDecimal_Click(object sender, EventArgs e)
{
string locale;
decimal number;
CultureInfo culture;
// Return if string is empty
if (String.IsNullOrEmpty(this.inputNumber.Text))
return;
// Get locale of web request to determine possible format of number
if (Request.UserLanguages.Length == 0)
return;
locale = Request.UserLanguages[0];
if (String.IsNullOrEmpty(locale))
return;
// Instantiate CultureInfo object for the user's locale
culture = new CultureInfo(locale);
// Convert user input from a string to a number
try
{
number = Decimal.Parse(this.inputNumber.Text, culture.NumberFormat);
}
catch (FormatException)
{
return;
}
catch (Exception)
{
return;
}
// Output number to label on web form
this.outputNumber.Text = "Number is " + number.ToString();
}
Protected Sub OkToDecimal_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToDecimal.Click
Dim locale As String
Dim culture As CultureInfo
Dim number As Decimal
' Return if string is empty
If String.IsNullOrEmpty(Me.inputNumber.Text) Then Exit Sub
' Get locale of web request to determine possible format of number
If Request.UserLanguages.Length = 0 Then Exit Sub
locale = Request.UserLanguages(0)
If String.IsNullOrEmpty(locale) Then Exit Sub
' Instantiate CultureInfo object for the user's locale
culture = New CultureInfo(locale)
' Convert user input from a string to a number
Try
number = Decimal.Parse(Me.inputNumber.Text, culture.NumberFormat)
Catch ex As FormatException
Exit Sub
Catch ex As Exception
Exit Sub
End Try
' Output number to label on web form
Me.outputNumber.Text = "Number is " & number.ToString()
End Sub
備註
此方法的 Parse(String, IFormatProvider) 這個多載通常用來轉換文字,這些文字可以透過各種方式格式化為 Decimal 值。 例如,它可以用來將使用者輸入的文字轉換成 HTML 文字方塊,轉換為數值。
參數 s
包含一些格式:
[ws][sign][digits,]digits[.fractional-digits][ws]
在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。
元素 | 描述 |
---|---|
ws | 選擇性空白字元。 |
簽署 | 選擇性符號。 |
數字 | 介於 0 到 9 的數位序列。 |
, | 特定文化特性的千位分隔符號符號。 |
. | 特定文化特性的小數點符號。 |
fractional-digits | 介於 0 到 9 的數位序列。 |
參數 s
會使用 NumberStyles.Number 樣式來解譯。 這表示允許空白字元和千位分隔符號,但貨幣符號則不允許。 若要明確定義專案 (,例如貨幣符號、千位分隔符號和空白字元) , s
Decimal.Parse(String, NumberStyles, IFormatProvider) 請使用 方法。
參數 provider
是實作 IFormatProvider ,例如 NumberFormatInfo 或 CultureInfo 物件。 參數 provider
提供剖析中使用的文化特性特定資訊。 如果 provider
是 null
,則會使用執行緒目前的文化特性。
物件 Decimal 具有 29 位數的有效位數。 如果 s
代表數位超過 29 位數,但具有小數部分,且位於 和 MinValue 的範圍內 MaxValue ,則數位會四捨五入,而不是截斷為 29 位數,使用四捨五入為最接近的數位。
如果在剖析作業期間于 參數中 s
遇到分隔符號,且適用的貨幣或數位十進位和群組分隔符號相同,則剖析作業會假設分隔符號是小數分隔符號,而不是群組分隔符號。 如需分隔符號的詳細資訊,請參閱 CurrencyDecimalSeparator 、 NumberDecimalSeparatorCurrencyGroupSeparator 、 和 NumberGroupSeparator 。
另請參閱
適用於
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
將 UTF-8 字元的範圍剖析為值。
public static decimal Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Number, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> decimal
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Number, Optional provider As IFormatProvider = Nothing) As Decimal
參數
- utf8Text
- ReadOnlySpan<Byte>
要剖析的 UTF-8 字元範圍。
- style
- NumberStyles
數位樣式的位元組合,可以存在於 中 utf8Text
。
- provider
- IFormatProvider
提供關於 utf8Text
之特定文化特性格式資訊的物件。
傳回
剖析 utf8Text
的結果。
實作
適用於
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
使用指定樣式和特定文化特性格式,將數字的範圍表示轉換為其對等的 Decimal。
public static decimal Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Number, IFormatProvider? provider = default);
public static decimal Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Number, IFormatProvider provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> decimal
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Number, Optional provider As IFormatProvider = Nothing) As Decimal
參數
- s
- ReadOnlySpan<Char>
該範圍包含代表所要轉換數字的字元。
- style
- NumberStyles
NumberStyles 值的位元組合,指出可出現在 s
中的樣式項目。 一般會指定的值是 Number。
- provider
- IFormatProvider
IFormatProvider 物件,提供關於 s
格式的特定文化特性資訊。
傳回
Decimal 數字,等於包含在 s
中的數字,如同 style
和 provider
所指定。
實作
適用於
Parse(String, NumberStyles, IFormatProvider)
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
- 來源:
- Decimal.cs
使用指定的樣式和特定文化特性格式,將數字的字串表示轉換為其對等的 Decimal。
public:
static System::Decimal Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
static System::Decimal Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<System::Decimal>::Parse;
public static decimal Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static decimal Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> decimal
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Decimal
參數
- s
- String
要轉換的數字的字串表示。
- style
- NumberStyles
NumberStyles 值的位元組合,指出可出現在 s
中的樣式項目。 一般會指定的值是 Number。
- provider
- IFormatProvider
IFormatProvider 物件,提供關於 s
格式的特定文化特性資訊。
傳回
Decimal 數字,等於包含在 s
中的數字,如同 style
和 provider
所指定。
實作
例外狀況
s
的格式不正確。
s
為 null
。
範例
下列範例會使用各種 style
和 provider
參數來剖析值的字串表示 Decimal 。
string value;
decimal number;
NumberStyles style;
CultureInfo provider;
// Parse string using " " as the thousands separator
// and "," as the decimal separator for fr-FR culture.
value = "892 694,12";
style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands;
provider = new CultureInfo("fr-FR");
number = Decimal.Parse(value, style, provider);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
// '892 694,12' converted to 892694.12.
try
{
number = Decimal.Parse(value, style, CultureInfo.InvariantCulture);
Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
// Unable to parse '892 694,12'.
// Parse string using "$" as the currency symbol for en-GB and
// en-US cultures.
value = "$6,032.51";
style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
provider = new CultureInfo("en-GB");
try
{
number = Decimal.Parse(value, style, provider);
Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
// Unable to parse '$6,032.51'.
provider = new CultureInfo("en-US");
number = Decimal.Parse(value, style, provider);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
// '$6,032.51' converted to 6032.51.
// Parse string using " " as the thousands separator
// and "," as the decimal separator for fr-FR culture.
let value = "892 694,12"
let style = NumberStyles.AllowDecimalPoint ||| NumberStyles.AllowThousands
let provider = CultureInfo "fr-FR"
let number = Decimal.Parse(value, style, provider)
printfn $"'{value}' converted to {number}."
// Displays:
// '892 694,12' converted to 892694.12.
try
let number = Decimal.Parse(value, style, CultureInfo.InvariantCulture)
printfn $"'{value}' converted to {number}."
with :? FormatException ->
printfn $"Unable to parse '{value}'."
// Displays:
// Unable to parse '892 694,12'.
// Parse string using "$" as the currency symbol for en-GB and
// en-US cultures.
let value = "$6,032.51"
let style = NumberStyles.Number ||| NumberStyles.AllowCurrencySymbol
let provider = CultureInfo "en-GB"
try
let number = Decimal.Parse(value, style, provider)
printfn $"'{value}' converted to {number}."
with :? FormatException ->
printfn $"Unable to parse '{value}'."
// Displays:
// Unable to parse '$6,032.51'.
let provider = CultureInfo "en-US"
let number = Decimal.Parse(value, style, provider)
printfn $"'{value}' converted to {number}."
// Displays:
// '$6,032.51' converted to 6032.51.
Dim value As String
Dim number As Decimal
Dim style As NumberStyles
Dim provider As CultureInfo
' Parse string using " " as the thousands separator
' and "," as the decimal separator for fr-FR culture.
value = "892 694,12"
style = NumberStyles.AllowDecimalPoint Or NumberStyles.AllowThousands
provider = New CultureInfo("fr-FR")
number = Decimal.Parse(value, style, provider)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' '892 694,12' converted to 892694.12.
Try
number = Decimal.Parse(value, style, CultureInfo.InvariantCulture)
Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
' Unable to parse '892 694,12'.
' Parse string using "$" as the currency symbol for en-GB and
' en-US cultures.
value = "$6,032.51"
style = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
provider = New CultureInfo("en-GB")
Try
number = Decimal.Parse(value, style, provider)
Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
' Unable to parse '$6,032.51'.
provider = New CultureInfo("en-US")
number = Decimal.Parse(value, style, provider)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' '$6,032.51' converted to 6032.51.
備註
參數 style
會定義參數的允許格式 s
,讓剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。 不支援下列 NumberStyles 成員:
根據 的值 style
, s
參數可能包含下列元素:
[ws][$][sign][digits,]digits[.fractional-digits][e[sign]digits][ws]
在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。
元素 | 描述 |
---|---|
$ | 特定文化特性的貨幣符號。 字串中的位置是由 CurrencyNegativePattern 參數之 方法 provider 所 GetFormat 傳回之 物件的 和 CurrencyPositivePattern 屬性 NumberFormatInfo 所定義。 如果 style 包含 旗標, NumberStyles.AllowCurrencySymbol 則可以在 中 s 顯示貨幣符號。 |
ws | 選擇性空白字元。 如果 包含 旗標,則空白字元可以出現在 的 s 開頭,如果包含 NumberStyles.AllowTrailingWhite 旗標,則會出現在 結尾 style s 。 NumberStyles.AllowLeadingWhitestyle |
簽署 | 選擇性符號。 如果 style 包含 旗標,則符號可以出現在 的 s 開頭,如果包含 NumberStyles.AllowTrailingSign 旗標,則會出現在 的 style s NumberStyles.AllowLeadingSign 結尾。 如果包含 NumberStyles.AllowParentheses 旗標,則可以在 中使用 s 括弧來表示負值 style 。 |
數字 | 範圍從 0 到 9 的數位序列。 |
, | 特定文化特性的千位分隔符號符號。 如果包含 旗標,則 所 provider 定義文化特性的千位分隔符號會出現在 中 s 。 NumberStyles.AllowThousandsstyle |
. | 特定文化特性的小數點符號。 如果包含 旗標,則 provider 所定義文化特性的小數點符號可能會出現在 中 s 。 NumberStyles.AllowDecimalPointstyle |
fractional-digits | 範圍從 0 到 9 的數位序列。 只有包含 NumberStyles.AllowDecimalPoint 旗標時 style ,小數位數才會出現在 中 s 。 |
e | 'e' 或 'E' 字元,表示該值是以指數標記法表示。 如果 style 包含 NumberStyles.AllowExponent 旗標,參數 s 可以代表指數標記法的數位。 |
注意
中任何終止的 NUL (U+0000) 字元 s
都會被剖析作業忽略,不論引數的值 style
為何。
只有數位的字串 (對應至 None 樣式) 一律會在類型範圍內 Decimal 成功剖析。 其餘 NumberStyles 成員控制項專案可能存在,但不需要出現在輸入字串中。 下表指出個別 NumberStyles 成員如何影響 中 s
可能存在的專案。
NumberStyles 值 | 除了數位之外,還允許的專案 |
---|---|
None | 僅限 digits 元素。 |
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
,則會使用執行緒目前的文化特性。
Decimal物件具有 29 位數的有效位數。 如果 s
代表數位超過 29 位數,但具有小數部分,而且在 和 MinValue 的範圍內 MaxValue ,則數位會四捨五入,而不是截斷為 29 位數,使用四捨五入為最接近的數位。
如果在剖析作業期間于 參數中 s
遇到分隔符號,且適用的貨幣或數位十進位和群組分隔符號相同,則剖析作業會假設分隔符號是小數分隔符號,而不是群組分隔符號。 如需分隔符號的詳細資訊,請參閱 CurrencyDecimalSeparator 、 NumberDecimalSeparatorCurrencyGroupSeparator 和 NumberGroupSeparator 。
另請參閱
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應