Decimal.Parse 方法

定義

將數位的字串表示轉換為其相等的 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)

使用指定的樣式和文化特性特定格式,將數位的span表示轉換為其相等的 Decimal

Parse(String, NumberStyles, IFormatProvider)

使用指定的樣式和文化特性特定格式,將數位的字串表示轉換成其相等的 Decimal

Parse(String)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

將數位的字串表示轉換為其相等的 Decimal

C#
public static decimal Parse (string s);

參數

s
String

要轉換之數位的字串表示。

傳回

相當於 s中包含的數位。

例外狀況

s 格式不正確。

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

範例

下列程式代碼範例會使用 Parse(String) 方法來剖析 Decimal 值的字串表示。

C#
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'.

備註

參數 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)Decimal.Parse(String, NumberStyles, IFormatProvider) 方法。

如有必要,s 的值會使用四捨五入到最接近的四捨五入。

Decimal 有29位數的有效位數。 如果 s 代表數字超過 29 位數,但具有小數部分,且在 MaxValueMinValue的範圍內,則數位會四捨五入,而不是截斷為 29 位數,使用四捨五入到最接近。

如果在剖析作業期間,s 參數中遇到分隔符,且適用的貨幣或數位十進位和群組分隔符相同,剖析作業會假設分隔符是小數分隔符,而不是群組分隔符。 如需分隔符的詳細資訊,請參閱 CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparatorNumberGroupSeparator

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Parse(ReadOnlySpan<Byte>, IFormatProvider)

來源:
Decimal.cs
來源:
Decimal.cs

將UTF-8字元的範圍剖析為值。

C#
public static decimal Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);

參數

utf8Text
ReadOnlySpan<Byte>

要剖析的UTF-8字元範圍。

provider
IFormatProvider

物件,提供與 utf8Text相關的特定文化特性格式資訊。

傳回

剖析 utf8Text的結果。

實作

適用於

.NET 9 和 .NET 8
產品 版本
.NET 8, 9

Parse(ReadOnlySpan<Char>, IFormatProvider)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

將字元範圍剖析為值。

C#
public static decimal Parse (ReadOnlySpan<char> s, IFormatProvider? provider);

參數

s
ReadOnlySpan<Char>

要剖析的字元範圍。

provider
IFormatProvider

物件,提供與 s相關的特定文化特性格式資訊。

傳回

剖析 s的結果。

實作

適用於

.NET 9 和其他版本
產品 版本
.NET 7, 8, 9

Parse(String, NumberStyles)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

將指定樣式中數位的字串表示轉換成其相等的 Decimal

C#
public static decimal Parse (string s, System.Globalization.NumberStyles style);

參數

s
String

要轉換之數位的字串表示。

style
NumberStyles

NumberStyles 值的位元組合,表示 s中可以存在的樣式專案。 要指定的一般值是 Number

傳回

Decimal 數位,相當於 style所指定之 s 中包含的數位。

例外狀況

style 不是 NumberStyles 值。

-或-

styleAllowHexSpecifier 值。

s 格式不正確。

範例

下列程式代碼範例會使用 Parse(String, NumberStyles) 方法來使用 en-US 文化特性剖析 Decimal 值的字串表示。

C#
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.

備註

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.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern 屬性所定義。 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,則目前的文化特性貨幣符號可能會出現在 s 中。
簽署 選擇性符號。 如果 style 包含 NumberStyles.AllowLeadingSign 旗標,則符號可能會出現在 s 開頭,如果 style 包含 NumberStyles.AllowTrailingSign 旗標,則它可能會出現在 s 結尾。 括弧可用於 s,如果 style 包含 NumberStyles.AllowParentheses 旗標,則表示負值。
位數 範圍從 0 到 9 的數位序列。
特定文化特性的千位分隔符符號。 如果 style 包含 NumberStyles.AllowThousands 旗標,則目前文化特性的千位分隔符可以出現在 s 中。
特定文化特性的小數點符號。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則目前文化特性的小數點符號可能會出現在 s 中。
小數位數 範圍從 0 到 9 的數位序列。 只有 style 包含 NumberStyles.AllowDecimalPoint 旗標時,小數位數才會出現在 s 中。
e 'e' 或 'E' 字元,表示值是以指數表示法表示。 如果 style 包含 NumberStyles.AllowExponent 旗標,s 參數就可以以指數表示法來表示數位。

備註

不論 style 自變數的值為何,剖析作業都會忽略 s 中任何終止的 NUL (U+0000) 字元。

只有數位的字串(對應至 None 樣式),如果字串位於 Decimal 類型的範圍,則一律會成功剖析。 其餘 NumberStyles 成員控制元素,這些元素可能不是必須存在於輸入字串中。 下表指出個別 NumberStyles 成員如何影響 s中可能出現的專案。

NumberStyles 值 除了數位以外,允許的 元素
None 位數 元素。
AllowDecimalPoint 小數位數 元素。
AllowExponent s 參數也可以使用指數表示法。 此旗標支援E數位格式 值;需要額外的旗標,才能成功剖析具有正負號和小數點符號等元素的字串。
AllowLeadingWhite s開頭的 ws ws 專案。
AllowTrailingWhite s結尾的 ws 專案。
AllowLeadingSign s開頭的 符號 專案。
AllowTrailingSign s結尾處的 符號 專案。
AllowParentheses 以括弧括住數值的括弧形式,符號 專案。
AllowThousands 專案。
AllowCurrencySymbol $ 專案。
Currency 都。 s 參數不能代表十六進位數或指數表示法的數位。
Float s開頭或結尾的 ws 專案,符號s開頭,以及 . 符號。 s 參數也可以使用指數表示法。
Number wssign,. 元素。
Any 除了 s 以外的所有樣式都不能代表十六進位數。

s 參數是使用針對目前系統文化特性初始化之 NumberFormatInfo 物件中的格式資訊來剖析。 如需詳細資訊,請參閱 CurrentInfo

Decimal 有29位數的有效位數。 如果 s 代表數字超過 29 位數,但具有小數部分,且在 MaxValueMinValue的範圍內,則數位會四捨五入,而不是截斷為 29 位數,使用四捨五入到最接近。

如果在剖析作業期間 s 參數中遇到分隔符,styles 包含 NumberStyles.AllowThousandsNumberStyles.AllowDecimalPoint 值,而且適用的貨幣或數位小數和群組分隔符相同,剖析作業會假設分隔符是小數分隔符,而不是群組分隔符。 如需分隔符的詳細資訊,請參閱 CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparatorNumberGroupSeparator

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Parse(String, IFormatProvider)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

使用指定的特定文化特性格式資訊,將數位的字串表示轉換為其相等 Decimal

C#
public static decimal Parse (string s, IFormatProvider provider);
C#
public static decimal Parse (string s, IFormatProvider? provider);

參數

s
String

要轉換之數位的字串表示。

provider
IFormatProvider

IFormatProvider,提供 s的特定文化特性剖析資訊。

傳回

Decimal 數位,相當於 provider所指定之 s 中包含的數位。

實作

例外狀況

s 格式不正確。

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

範例

下列範例是 Web 窗體的按鈕點選事件處理程式。 它會使用 HttpRequest.UserLanguages 屬性所傳回的數位來判斷使用者的地區設定。 然後,它會具現化對應至該地區設定的 CultureInfo 物件。 屬於該 CultureInfo 物件的 NumberFormatInfo 對象接著會傳遞至 Parse(String, IFormatProvider) 方法,將使用者的輸入轉換成 Decimal 值。

C#
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();
}

備註

這個 Parse(String, IFormatProvider) 方法的多載通常用來將各種格式的文字轉換成 Decimal 值。 例如,它可以用來將使用者輸入的文字轉換成 HTML 文字框,轉換為數值。

s 參數包含一些表單:

[ws][sign][digits,]digits[.fractional-digits][ws]

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

元素 描述
ws 選擇性的空格符。
簽署 選擇性符號。
位數 範圍從 0 到 9 的數位序列。
特定文化特性的千位分隔符符號。
特定文化特性的小數點符號。
小數位數 範圍從 0 到 9 的數位序列。

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

provider 參數是 IFormatProvider 實作,例如 NumberFormatInfoCultureInfo 物件。 provider 參數會提供剖析中使用的文化特性特定資訊。 如果 providernull,則會使用線程目前的文化特性。

Decimal 物件具有29位數的有效位數。 如果 s 代表數字超過 29 位數,但具有小數部分,且在 MaxValueMinValue的範圍內,則數位會四捨五入,而不是截斷為 29 位數,使用四捨五入到最接近。

如果在剖析作業期間 s 參數中遇到分隔符,而且適用的貨幣或數位十進位和群組分隔符相同,剖析作業會假設分隔符是小數分隔符,而不是群組分隔符。 如需分隔符的詳細資訊,請參閱 CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparatorNumberGroupSeparator

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

來源:
Decimal.cs
來源:
Decimal.cs

將UTF-8字元的範圍剖析為值。

C#
public static decimal Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Number, IFormatProvider? provider = default);

參數

utf8Text
ReadOnlySpan<Byte>

要剖析的UTF-8字元範圍。

style
NumberStyles

數字樣式的位元組合,可以存在於 utf8Text中。

provider
IFormatProvider

物件,提供與 utf8Text相關的特定文化特性格式資訊。

傳回

剖析 utf8Text的結果。

實作

適用於

.NET 9 和 .NET 8
產品 版本
.NET 8, 9

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

使用指定的樣式和文化特性特定格式,將數位的span表示轉換為其相等的 Decimal

C#
public static decimal Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Number, IFormatProvider? provider = default);
C#
public static decimal Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Number, IFormatProvider provider = default);

參數

s
ReadOnlySpan<Char>

範圍,包含表示要轉換之數位的字元。

style
NumberStyles

NumberStyles 值的位元組合,表示 s中可以存在的樣式專案。 要指定的一般值是 Number

provider
IFormatProvider

IFormatProvider 物件,提供 s格式的文化特性特定資訊。

傳回

styleprovider所指定之 s 中包含的數位相等的 Decimal 數位。

實作

適用於

.NET 9 和其他版本
產品 版本
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Parse(String, NumberStyles, IFormatProvider)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

使用指定的樣式和文化特性特定格式,將數位的字串表示轉換成其相等的 Decimal

C#
public static decimal Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
C#
public static decimal Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);

參數

s
String

要轉換之數位的字串表示。

style
NumberStyles

NumberStyles 值的位元組合,表示 s中可以存在的樣式專案。 要指定的一般值是 Number

provider
IFormatProvider

IFormatProvider 物件,提供 s格式的文化特性特定資訊。

傳回

styleprovider所指定之 s 中包含的數位相等的 Decimal 數位。

實作

例外狀況

s 格式不正確。

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

style 不是 NumberStyles 值。

-或-

styleAllowHexSpecifier 值。

範例

下列範例使用各種 styleprovider 參數來剖析 Decimal 值的字串表示。

C#
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.

備註

style 參數會定義 s 參數允許的格式,讓剖析作業成功。 它必須是來自 NumberStyles 列舉的位旗標組合。 不支援下列 NumberStyles 成員:

根據 style的值,s 參數可能包含下列元素:

[ws][$][sign][digits,]digits[.fractional-digits][e[sign]digits][ws]

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

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

備註

不論 style 自變數的值為何,剖析作業都會忽略 s 中任何終止的 NUL (U+0000) 字元。

只有數位的字串(對應至 None 樣式),如果字串位於 Decimal 類型的範圍,則一律會成功剖析。 其餘 NumberStyles 成員控制元素,這些元素可能不是必須存在於輸入字串中。 下表指出個別 NumberStyles 成員如何影響 s中可能出現的專案。

NumberStyles 值 除了數位以外,允許的 元素
None 位數 元素。
AllowDecimalPoint 小數位數 元素。
AllowExponent s 參數也可以使用指數表示法。 此旗標支援E數位格式 值;需要額外的旗標,才能成功剖析具有正負號和小數點符號等元素的字串。
AllowLeadingWhite s開頭的 ws ws 專案。
AllowTrailingWhite s結尾的 ws 專案。
AllowLeadingSign s開頭的 符號 專案。
AllowTrailingSign s結尾處的 符號 專案。
AllowParentheses 以括弧括住數值的括弧形式,符號 專案。
AllowThousands 專案。
AllowCurrencySymbol $ 專案。
Currency 都。 s 參數不能代表十六進位數或指數表示法的數位。
Float s開頭或結尾的 ws 專案,s開頭的符號,以及 符號。 s 參數也可以使用指數表示法。
Number wssign 專案。
Any 除了 s 以外的所有樣式都不能代表十六進位數。

provider 參數是 IFormatProvider 實作,例如 NumberFormatInfoCultureInfo 物件。 provider 參數會提供剖析中使用的文化特性特定資訊。 如果 providernull,則會使用線程目前的文化特性。

Decimal 物件具有29位數的有效位數。 如果 s 代表數字超過 29 位數,但具有小數部分,且在 MaxValueMinValue的範圍內,則數位會四捨五入,而不是截斷為 29 位數,使用四捨五入到最接近。

如果在剖析作業期間 s 參數中遇到分隔符,而且適用的貨幣或數位十進位和群組分隔符相同,剖析作業會假設分隔符是小數分隔符,而不是群組分隔符。 如需分隔符的詳細資訊,請參閱 CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparatorNumberGroupSeparator

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0