NumberStyles 列舉

定義

決定數值字串引數中允許的樣式,這些引數是傳遞給整數及浮點數值型別的 ParseTryParse 方法。

此列舉支援其成員值的位元組合。

public enum class NumberStyles
[System.Flags]
public enum NumberStyles
[System.Flags]
[System.Serializable]
public enum NumberStyles
[System.Flags]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum NumberStyles
[<System.Flags>]
type NumberStyles = 
[<System.Flags>]
[<System.Serializable>]
type NumberStyles = 
[<System.Flags>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type NumberStyles = 
Public Enum NumberStyles
繼承
NumberStyles
屬性

欄位

AllowBinarySpecifier 1024

表示數值字串代表二進位值。 有效的二進位值包括數位 0 和 1。 使用此樣式剖析的字串不會採用前置詞; 0b 無法使用。 以樣式剖析的 AllowBinarySpecifier 字串一律會解譯為二進位值。 其他可合併的旗標僅限 AllowBinarySpecifierAllowLeadingWhiteAllowTrailingWhiteNumberStyles 列舉包含 BinaryNumber 複合樣式,此樣式是由這三個旗標所組成。

AllowCurrencySymbol 256

指出數字字串是否可以包含貨幣符號。 有效貨幣符號是由 CurrencySymbol 屬性來決定。

AllowDecimalPoint 32

指示數值字串可以有小數點。 如果 NumberStyles 值包含 AllowCurrencySymbol 旗標,且剖析的值包含貨幣符號,則十進位分隔符號字元由CurrencyDecimalSeparator 屬性決定。 否則,小數分隔符號字元是由 NumberDecimalSeparator 屬性所決定。

AllowExponent 128

指示數值字串可以採用指數標記法。 AllowExponent 旗標允許剖析的字串包含以 "E" 或 "e" 字元開頭,後面跟著選擇性正負號和整數的指數。 換句話說,它能成功剖析 nnnExxnnnE+xxnnnE-xx 形式的字串。 它不允許有效數字或尾數中的小數分隔符號或正負號。為了要讓字串中的這些項目得以剖析,請使用 AllowDecimalPointAllowLeadingSign 旗標,或是使用包含這些個別旗標的複合樣式。

AllowHexSpecifier 512

指示數值字串會表示十六進位值。 有效十六進位值包括數值位數 0-9 和十六進位位數 A-F 和 a-f, 使用此樣式剖析的字串不能前面加上 「0x」 或 「&h」。。 使用 AllowHexSpecifier 樣式剖析的字串一定會解譯為十六進位值。 其他可合併的旗標僅限 AllowHexSpecifierAllowLeadingWhiteAllowTrailingWhiteNumberStyles 列舉包含 HexNumber 複合樣式,此樣式是由這三個旗標所組成。

AllowLeadingSign 4

指示數值字串可以有前置正負號。 有效前置正負號字元是由 PositiveSignNegativeSign 屬性所決定。

AllowLeadingWhite 1

表示前置空白字元可以出現在剖析的字串中。 有效泛空白字元有 Unicode 值 U+0009、U+000A、U+000B、U+000C、U+000D 和 U+0020。 請注意,這是字元的子集,IsWhiteSpace(Char) 方法會針對此子集傳回 true

AllowParentheses 16

指示數值字串可以有一對括住數字的括弧。 括號表示要剖析的字串代表負數。

AllowThousands 64

表示數值字串可以有群組分隔符號 (例如,隔開百位與千位的符號)。 NumberStyles如果值包含 AllowCurrencySymbol 旗標,而要剖析的字串包含貨幣符號,則有效的群組分隔符是由 屬性決定CurrencyGroupSeparator,而每個群組中的位數則由 CurrencyGroupSizes 屬性決定。 否則,有效群組分隔符號字元是由 NumberGroupSeparator 屬性所決定,而每個群組中的位數則由 NumberGroupSizes 屬性所決定。

AllowTrailingSign 8

指示數值字串可以有尾端正負號。 有效尾端正負號字元由 PositiveSignNegativeSign 屬性所決定。

AllowTrailingWhite 2

表示尾端空白字元可以出現在剖析的字串中。 有效泛空白字元有 Unicode 值 U+0009、U+000A、U+000B、U+000C、U+000D 和 U+0020。 請注意,這是字元的子集,IsWhiteSpace(Char) 方法會針對此子集傳回 true

Any 511

指示使用除了 AllowHexSpecifier 之外的所有樣式。 這是複合的數字樣式。

BinaryNumber 1027

指示使用 AllowLeadingWhiteAllowTrailingWhiteAllowBinarySpecifier 樣式。 這是複合的數字樣式。

Currency 383

指示使用除了 AllowExponentAllowHexSpecifier 之外的所有樣式。 這是複合的數字樣式。

Float 167

指示使用 AllowLeadingWhiteAllowTrailingWhiteAllowLeadingSignAllowDecimalPointAllowExponent 樣式。 這是複合的數字樣式。

HexNumber 515

指示使用 AllowLeadingWhiteAllowTrailingWhiteAllowHexSpecifier 樣式。 這是複合的數字樣式。

Integer 7

指示使用 AllowLeadingWhiteAllowTrailingWhiteAllowLeadingSign 樣式。 這是複合的數字樣式。

None 0

表示剖析的字串中不可出現樣式元素,例如前置或尾端空白、千分位分隔符號或十分位分隔符號。 要剖析的字串必須只能由整數位數和小數位數組成。

Number 111

指示使用 AllowLeadingWhiteAllowTrailingWhiteAllowLeadingSignAllowTrailingSignAllowDecimalPointAllowThousands 樣式。 這是複合的數字樣式。

範例

此範例示範如何使用各種 NumberStyles 旗標,將字串剖析成32位整數。

using namespace System;
using namespace System::Text;
using namespace System::Globalization;


int main()
{
    // Parse the string as a hex value and display the
    // value as a decimal.
    String^ numberString = "A";
    int stringValue = Int32::Parse(numberString, NumberStyles::HexNumber);
    Console::WriteLine("{0} in hex = {1} in decimal.",
        numberString, stringValue);

    // Parse the string, allowing a leading sign, and ignoring
    // leading and trailing white spaces.
    numberString = "    -45   ";
    stringValue =Int32::Parse(numberString, NumberStyles::AllowLeadingSign |
        NumberStyles::AllowLeadingWhite | NumberStyles::AllowTrailingWhite);
    Console::WriteLine("'{0}' parsed to an int is '{1}'.",
        numberString, stringValue);

    // Parse the string, allowing parentheses, and ignoring
    // leading and trailing white spaces.
    numberString = "    (37)   ";
    stringValue = Int32::Parse(numberString, NumberStyles::AllowParentheses |
        NumberStyles::AllowLeadingSign | NumberStyles::AllowLeadingWhite |
        NumberStyles::AllowTrailingWhite);

    Console::WriteLine("'{0}' parsed to an int is '{1}'.",
        numberString, stringValue);
}

// This code produces the following output.
//
// A in hex = 10 in decimal.
// '    -45   ' parsed to an int is '-45'.
// '    (37)   ' parsed to an int is '-37'.
using System;
using System.Text;
using System.Globalization;

public sealed class App
{
    static void Main()
    {
        // Parse the string as a hex value and display the value as a decimal.
        String num = "A";
        int val = int.Parse(num, NumberStyles.HexNumber);
        Console.WriteLine("{0} in hex = {1} in decimal.", num, val);

        // Parse the string, allowing a leading sign, and ignoring leading and trailing white spaces.
        num = "    -45   ";
        val = int.Parse(num, NumberStyles.AllowLeadingSign |
            NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite);
        Console.WriteLine("'{0}' parsed to an int is '{1}'.", num, val);

        // Parse the string, allowing parentheses, and ignoring leading and trailing white spaces.
        num = "    (37)   ";
        val = int.Parse(num, NumberStyles.AllowParentheses | NumberStyles.AllowLeadingSign |                         NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite);
        Console.WriteLine("'{0}' parsed to an int is '{1}'.", num, val);
    }
}

// This code produces the following output.
//
// A in hex = 10 in decimal.
// '    -45   ' parsed to an int is '-45'.
// '    (37)   ' parsed to an int is '-37'.
Imports System.Globalization
Imports System.Text

Public Module Example
   Public Sub Main() 
      ' Parse the string as a hex value and display the value as a decimal.
      Dim num As String = "A"
      Dim val As Integer = Int32.Parse(num, NumberStyles.HexNumber)
      Console.WriteLine("{0} in hex = {1} in decimal.", num, val)

      ' Parse the string, allowing a leading sign, and ignoring leading and trailing white spaces.
      num = "    -45   "
      val = Integer.Parse(num, NumberStyles.AllowLeadingSign Or 
                               NumberStyles.AllowLeadingWhite Or 
                               NumberStyles.AllowTrailingWhite)
      Console.WriteLine("'{0}' parsed to an integer is '{1}'.", num, val)

      ' Parse the string, allowing parentheses, and ignoring leading and trailing white spaces.
      num = "    (37)   "
      val = Integer.Parse(num, NumberStyles.AllowParentheses Or 
                               NumberStyles.AllowLeadingSign Or
                               NumberStyles.AllowLeadingWhite Or 
                               NumberStyles.AllowTrailingWhite)
      Console.WriteLine("'{0}' parsed to an integer is '{1}'.", num, val)
   End Sub
End Module
' The example displays the following output:
'       A in hex = 10 in decimal.
'       '    -45   ' parsed to an int is '-45'.
'       '    (37)   ' parsed to an int is '-37'.

備註

列舉 NumberStyles 包含兩種列舉值,用來剖析數值的字串表示:

  • 個別欄位旗標,定義特定樣式專案 (,例如空格符和群組分隔符,) 可以存在於剖析字串中。

  • 複合編號樣式,其中包含多個字段旗標,可定義可存在於剖析字元串中的樣式專案。

AllowHexSpecifier除了 之外,列舉中的NumberStyles個別欄位旗標會定義剖析十進位數位串表示時所使用的樣式專案。 None 表示只有數位可以出現在剖析的字串中。 其餘個別欄位旗標會定義可能但不需要的樣式專案,以小數點數的字串表示呈現,讓剖析作業成功。 相反地, AllowHexSpecifier 旗標表示要剖析的字串一律會解譯為十六進位值。 唯一可以搭配 AllowHexSpecifier 使用的個別欄位旗標為 AllowLeadingWhiteAllowTrailingWhite。 列舉 NumberStyles 包含複合數字樣式 , HexNumber其中包含所有三個旗標。

符號 (例如貨幣符號、群組分隔符、小數分隔符,以及正負號) 可以出現在要剖析之 System.Globalization.NumberFormatInfo 字串中的符號,是由隱含或明確傳遞至 Parse 方法的對象成員所定義。 本主題中的成員數據表提供每個個別旗標的描述,並指出其與 NumberFormatInfo 屬性的關聯性。

下表列出複合編號樣式,並指出其包含的個別欄位旗標。 單元格中的 「1」 表示複合編號樣式包含該數據列中的個別數字樣式。 “0” 表示複合數字樣式不包含個別的數字樣式。

任意 貨幣 Float 整數 Number HexNumber
AllowHexSpecifier (0x0200) 0 0 0 0 0 1
AllowCurrencySymbol (0x0100) 1 1 0 0 0 0
AllowExponent (0x0080) 1 0 1 0 0 0
AllowThousands (0x0040) 1 1 0 0 1 0
AllowDecimalPoint (0x0020) 1 1 1 0 1 0
AllowParentheses (0x0010) 1 1 0 0 0 0
AllowTrailingSign (0x0008) 1 1 0 0 1 0
AllowLeadingSign (0x0004) 1 1 1 1 1 0
AllowTrailingWhite (0x0002) 1 1 1 1 1 1
AllowLeadingWhite (0x0001) 1 1 1 1 1 1
(0x1ff) (0x17f) (0x0a7) (0x007) (0x06f) (0x203)

適用於

另請參閱