共用方式為


Decimal.Parse 方法

定義

將數字的字串表示轉換為其 Decimal 等價物。

多載

名稱 Description
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.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數字。

例外狀況

snull

s 格式不正確。

s 代表小於 Decimal.MinValue 或大於 Decimal.MaxValue 的數字。

範例

以下程式碼範例使用此 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 的數位序列。
, 特定文化特性的千位分隔符符號。
. 特定文化特性的小數點符號。
小數數字 範圍從 0 到 9 的數位序列。

s參數是用樣NumberStyles.Number式來解釋的。 這表示允許空格符和千位分隔符,但貨幣符號則不允許。 要明確定義 中可能存在 s的元素(如貨幣符號、數千分隔符和空白),可以使用 或 Decimal.Parse(String, NumberStyles) 方法 Decimal.Parse(String, NumberStyles, IFormatProvider)

s參數會利用格式資訊解析,該格式資訊NumberFormatInfo已初始化為當前系統文化。 如需詳細資訊,請參閱CurrentInfo。 若要解析字串,使用其他文化的格式資訊,請使用 Decimal.Parse(String, IFormatProvider) or Decimal.Parse(String, NumberStyles, IFormatProvider) 方法。

如有需要,會用四捨五入的方式將 的 s 值四捨五入到最接近。

A Decimal 有 29 位數的精確度。 若 s 代表一個數字,其數字多於 29 位數,且其分數部分位於 和 MaxValueMinValue範圍內,則該數字會四捨五入而非截斷,並以四捨五入至近的 29 位數。

若在解析操作中遇到參數分 s 隔符,且適用的貨幣或數字分隔符與群組分隔符相同,則解析操作假設分隔符為十進位分隔符而非群分隔符。 欲了解更多分離子資訊,請參見 CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparatorNumberGroupSeparator及 。

另請參閱

適用於

Parse(ReadOnlySpan<Byte>, IFormatProvider)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
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
來源:
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.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與 中sstyle所包含的數字相等價的數。

例外狀況

snull

style 不是一個 NumberStyles 數值。

-或-

style 是 的 AllowHexSpecifier 價值。

s 格式不正確。

s代表小於 Decimal.MinValue 或大於 Decimal.MaxValue 的數字

範例

以下程式碼範例使用該 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 選擇性的空格符。 如果包含該旗幟,空白可以出現在 的s開頭,如果包含該NumberStyles.AllowTrailingWhite旗,則可以出現在 的結尾stylesNumberStyles.AllowLeadingWhitestyle
$ 特定文化特性的貨幣符號。 它在字串中的位置由NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern當前文化的性質所定義。 當前s文化的貨幣符號可以出現style在包含NumberStyles.AllowCurrencySymbol國旗的情況下。
簽署 選擇性符號。 如果包含NumberStyles.AllowLeadingSign旗幟,標誌可以出現在 的styles開頭,如果包含NumberStyles.AllowTrailingSign該旗幟,則可以出現在 的結尾sstyle。 若s包含旗標,style則可使用NumberStyles.AllowParentheses括號表示負值。
數字 範圍從 0 到 9 的數位序列。
, 特定文化特性的千位分隔符符號。 現今文化的千分隔符可以出現s在包含styleNumberStyles.AllowThousands旗幟時。
. 特定文化特性的小數點符號。 當前文化的小數點符號可以出現sstyle在包含NumberStyles.AllowDecimalPoint國旗的情況下。
小數數字 範圍從 0 到 9 的數位序列。 只有當 s 包含該旗幟時style,小數數字才能出現NumberStyles.AllowDecimalPoint在 中。
e 'e' 或 'E' 字元,表示值是以指數表示法表示。 若s包含style旗標,參數NumberStyles.AllowExponent可用指數符號表示數字。

注意

任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。

只有數字的字串(對應 None 於樣式)若在該類型範圍內 Decimal ,則能成功解析。 剩餘 NumberStyles 成員控制輸入字串中可能存在但不必須存在的元素。 下表顯示個別 NumberStyles 成員如何影響可能存在於 s中的元素。

NumberStyles 值 除了數位以外,允許的 元素
None 只有 數字 元素。
AllowDecimalPoint 以及 以及 小數數字 元素。
AllowExponent 參數 s 也可以使用指數符號。 此旗標支援數字 E位數的形式;需要額外旗標才能成功解析包含正負符號及小數點符號等元素的字串。
AllowLeadingWhite 開頭sw 元素。
AllowTrailingWhite 末尾s 元素。
AllowLeadingSign 符號 元素位於 的開頭 s
AllowTrailingSign 符號 元素位於 的結尾 s
AllowParentheses 符號 元素以 括號形式包含數值。
AllowThousands 這個元素
AllowCurrencySymbol 元素 $
Currency 都。 該 s 參數無法代表十六進位數或指數符號中的數字。
Float 符號位於 s開頭或結尾,符號位於 s的開頭,以及.符號。 參數 s 也可以使用指數符號。
Number 這些元素、 wssign,. 、 以及 元素。
Any 除了 s 不能代表十六進位數字外,所有樣式都無法代表。

s參數是利用為目前系統文化初始化的物件中的格式資訊NumberFormatInfo進行解析。 如需詳細資訊,請參閱CurrentInfo

A Decimal 有 29 位數的精確度。 若 s 代表一個數字,其數字多於 29 位數,且其分數部分位於 和 MaxValueMinValue範圍內,則該數字會四捨五入而非截斷,並以四捨五入至近的 29 位數。

若在解析操作中參數中遇到 s 分隔符,包含 stylesNumberStyles.AllowThousandsNumberStyles.AllowDecimalPoint 值,且適用的貨幣或數字分隔符(十進位與群分隔符)相同,則解析操作假設分隔符為十進位分隔符而非群分隔符。 欲了解更多分離子資訊,請參見 CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparatorNumberGroupSeparator及 。

另請參閱

適用於

Parse(String, IFormatProvider)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
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與 中sprovider所包含的數字相等價的數。

實作

例外狀況

snull

s 格式不正確。

s 代表小於 Decimal.MinValue 或大於 Decimal.MaxValue 的數字。

範例

下列範例是 Web 窗體的按鈕點選事件處理程式。 它利用屬性 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 的數位序列。
, 特定文化特性的千位分隔符符號。
. 特定文化特性的小數點符號。
小數數字 範圍從 0 到 9 的數位序列。

s參數是透過 NumberStyles.Number style 來解釋的。 這表示允許空格符和千位分隔符,但貨幣符號則不允許。 要明確定義 中可能存在 s的元素(如貨幣符號、數千分隔符及空白),請使用該 Decimal.Parse(String, NumberStyles, IFormatProvider) 方法。

參數 provider 是一個 IFormatProvider 實作,例如 a NumberFormatInfoCultureInfo 物件。 參數 provider 提供解析中所需的特定文化資訊。 若 provider 為 , null則使用執行緒當前文化。

一個 Decimal 物體有29位數的精確度。 若 s 代表一個數字,其數字多於 29 位數,且其分數部分位於 和 MaxValueMinValue範圍內,則該數字會四捨五入而非截斷,並以四捨五入至近的 29 位數。

若在解析操作中遇到參數分 s 隔符,且適用的貨幣或數字分隔符相同,則解析操作假設分隔符為十進位分隔符而非群分隔符。 欲了解更多分離子資訊,請參見 CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparatorNumberGroupSeparator及 。

另請參閱

適用於

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
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.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等價於由 和 providers包含style的數值。

實作

適用於

Parse(String, NumberStyles, IFormatProvider)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
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等價於由 和 providers包含style的數值。

實作

例外狀況

s 格式不正確。

s 代表小於 Decimal.MinValue 或大於 Decimal.MaxValue 的數字。

snull

style 不是一個 NumberStyles 數值。

-或-

style 是 的 AllowHexSpecifier 價值。

範例

以下範例使用多種 styleprovider 參數來解析數值的 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]

方括弧 ([ 和 ]) 中的元素是選擇性的。 下表描述每個元素。

元素 描述
$ 特定文化特性的貨幣符號。 它在字串中的位置由CurrencyNegativePatternCurrencyPositivePatternNumberFormatInfo參數方法provider回傳GetFormat的物件屬性所定義。 貨幣s符號可出現style在包含NumberStyles.AllowCurrencySymbol旗幟時。
ws 選擇性的空格符。 如果包含該旗幟,空白可以出現在 的s開頭,如果包含該NumberStyles.AllowTrailingWhite旗,則可以出現在 的結尾stylesNumberStyles.AllowLeadingWhitestyle
簽署 選擇性符號。 如果包含NumberStyles.AllowLeadingSign旗幟,標誌可以出現在 的styles開頭,如果包含NumberStyles.AllowTrailingSign該旗幟,則可以出現在 的結尾sstyle。 若s包含旗標,style則可使用NumberStyles.AllowParentheses括號表示負值。
數字 範圍從 0 到 9 的數位序列。
, 特定文化特性的千位分隔符符號。 文化的provider千分隔符若包含NumberStyles.AllowThousands旗幟,則可出現sstyle
. 特定文化特性的小數點符號。 provider若包含NumberStyles.AllowDecimalPoint旗幟,則可出現在 中sstyle
小數數字 範圍從 0 到 9 的數位序列。 只有當 s 包含該旗幟時style,小數數字才能出現NumberStyles.AllowDecimalPoint在 中。
e 'e' 或 'E' 字元,表示值是以指數表示法表示。 若s包含style旗標,參數NumberStyles.AllowExponent可用指數符號表示數字。

注意

任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。

只有數字的字串(對應 None 於樣式)若在該類型範圍內 Decimal ,則能成功解析。 剩餘 NumberStyles 成員控制輸入字串中可能存在但不必須存在的元素。 下表顯示個別 NumberStyles 成員如何影響可能存在於 s中的元素。

NumberStyles 值 除了數位以外,允許的 元素
None 只有 數字 元素。
AllowDecimalPoint 以及 以及 小數數字 元素。
AllowExponent 參數 s 也可以使用指數符號。 此旗標支援數字 E位數的形式;需要額外旗標才能成功解析包含正負符號及小數點符號等元素的字串。
AllowLeadingWhite 開頭sw 元素。
AllowTrailingWhite 末尾s 元素。
AllowLeadingSign 符號 元素位於 的開頭 s
AllowTrailingSign 符號 元素位於 的結尾 s
AllowParentheses 符號 元素以 括號形式包含數值。
AllowThousands 這個元素
AllowCurrencySymbol 元素 $
Currency 都。 該 s 參數無法代表十六進位數或指數符號中的數字。
Float ws 元素位於s開頭或結 尾,符號 位於 s,以及 符號。 參數 s 也可以使用指數符號。
Number 元素、wssign、和
Any 除了 s 不能代表十六進位數字外,所有樣式都無法代表。

參數 provider 是一個 IFormatProvider 實作,例如 a NumberFormatInfoCultureInfo 物件。 參數 provider 提供解析中所需的特定文化資訊。 若 provider 為 , null則使用執行緒當前文化。

一個 Decimal 物體有29位數的精確度。 若 s 代表一個數字,其數字多於 29 位數,且其分數部分位於 和 MaxValueMinValue範圍內,則該數字會四捨五入而非截斷,並以四捨五入至近的 29 位數。

若在解析操作中遇到參數分 s 隔符,且適用的貨幣或數字分隔符相同,則解析操作假設分隔符為十進位分隔符而非群分隔符。 欲了解更多分離子資訊,請參見 CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparatorNumberGroupSeparator及 。

另請參閱

適用於