NumberStyles 列舉

定義

決定數值字串引數中允許的樣式,這些引數是傳遞給整數及浮點數值型別的 ParseTryParse 方法。Determines the styles permitted in numeric string arguments that are passed to the Parse and TryParse methods of the integral and floating-point numeric types.

此列舉具有 FlagsAttribute 個屬性允許以位元方式合併其成員值。

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

欄位

AllowCurrencySymbol 256

指出數字字串是否可以包含貨幣符號。Indicates that the numeric string can contain a currency symbol. 有效貨幣符號是由 CurrencySymbol 屬性來決定。Valid currency symbols are determined by the CurrencySymbol property.

AllowDecimalPoint 32

指示數值字串可以有小數點。Indicates that the numeric string can have a decimal point. 如果 NumberStyles 值包含 AllowCurrencySymbol 旗標,且剖析的值包含貨幣符號,則十進位分隔符號字元由CurrencyDecimalSeparator 屬性決定。If the NumberStyles value includes the AllowCurrencySymbol flag and the parsed string includes a currency symbol, the decimal separator character is determined by the CurrencyDecimalSeparator property. 否則,小數分隔符號字元是由 NumberDecimalSeparator 屬性所決定。Otherwise, the decimal separator character is determined by the NumberDecimalSeparator property.

AllowExponent 128

指示數值字串可以採用指數標記法。Indicates that the numeric string can be in exponential notation. AllowExponent 旗標允許剖析的字串包含以 "E" 或 "e" 字元開頭,後面跟著選擇性正負號和整數的指數。The AllowExponent flag allows the parsed string to contain an exponent that begins with the "E" or "e" character and that is followed by an optional positive or negative sign and an integer. 換句話說,它能成功剖析 nnnExxnnnE+xxnnnE-xx 形式的字串。In other words, it successfully parses strings in the form nnnExx, nnnE+xx, and nnnE-xx. 它不允許有效數字或尾數中的小數分隔符號或正負號。為了要讓字串中的這些項目得以剖析,請使用 AllowDecimalPointAllowLeadingSign 旗標,或是使用包含這些個別旗標的複合樣式。It does not allow a decimal separator or sign in the significand or mantissa; to allow these elements in the string to be parsed, use the AllowDecimalPoint and AllowLeadingSign flags, or use a composite style that includes these individual flags.

AllowHexSpecifier 512

指示數值字串會表示十六進位值。Indicates that the numeric string represents a hexadecimal value. 有效十六進位值包括數值位數 0-9 和十六進位位數 A-F 和 a-f,Valid hexadecimal values include the numeric digits 0-9 and the hexadecimal digits A-F and a-f. 使用這個樣式剖析的字串不能在前面加上 "0x" 或 &h"。Strings that are parsed using this style cannot be prefixed with "0x" or "&h". 使用 AllowHexSpecifier 樣式剖析的字串一定會解譯為十六進位值。A string that is parsed with the AllowHexSpecifier style will always be interpreted as a hexadecimal value. 其他可合併的旗標僅限 AllowHexSpecifierAllowLeadingWhiteAllowTrailingWhiteThe only flags that can be combined with AllowHexSpecifier are AllowLeadingWhite and AllowTrailingWhite. NumberStyles 列舉包含 HexNumber 複合樣式,此樣式是由這三個旗標所組成。The NumberStyles enumeration includes a composite style, HexNumber, that consists of these three flags.

AllowLeadingSign 4

指示數值字串可以有前置正負號。Indicates that the numeric string can have a leading sign. 有效前置正負號字元是由 PositiveSignNegativeSign 屬性所決定。Valid leading sign characters are determined by the PositiveSign and NegativeSign properties.

AllowLeadingWhite 1

表示前置空白字元可以出現在剖析的字串中。Indicates that leading white-space characters can be present in the parsed string. 有效泛空白字元有 Unicode 值 U+0009、U+000A、U+000B、U+000C、U+000D 和 U+0020。Valid white-space characters have the Unicode values U+0009, U+000A, U+000B, U+000C, U+000D, and U+0020. 請注意,這是字元的子集,IsWhiteSpace(Char) 方法會針對此子集傳回 trueNote that this is a subset of the characters for which the IsWhiteSpace(Char) method returns true.

AllowParentheses 16

指示數值字串可以有一對括住數字的括弧。Indicates that the numeric string can have one pair of parentheses enclosing the number. 括號表示要剖析的字串代表負數。The parentheses indicate that the string to be parsed represents a negative number.

AllowThousands 64

表示數值字串可以有群組分隔符號 (例如,隔開百位與千位的符號)。Indicates that the numeric string can have group separators, such as symbols that separate hundreds from thousands. 如果 NumberStyles 值包含 AllowCurrencySymbol 旗標,且要剖析的字串包含貨幣符號,則有效群組分隔符號字元由CurrencyGroupSeparator 屬性決定,且每個群組中的位數由 CurrencyGroupSizes 屬性決定。If the NumberStyles value includes the AllowCurrencySymbol flag and the string to be parsed includes a currency symbol, the valid group separator character is determined by the CurrencyGroupSeparator property, and the number of digits in each group is determined by the CurrencyGroupSizes property. 否則,有效群組分隔符號字元是由 NumberGroupSeparator 屬性所決定,而每個群組中的位數則由 NumberGroupSizes 屬性所決定。Otherwise, the valid group separator character is determined by the NumberGroupSeparator property, and the number of digits in each group is determined by the NumberGroupSizes property.

AllowTrailingSign 8

指示數值字串可以有尾端正負號。Indicates that the numeric string can have a trailing sign. 有效尾端正負號字元由 PositiveSignNegativeSign 屬性所決定。Valid trailing sign characters are determined by the PositiveSign and NegativeSign properties.

AllowTrailingWhite 2

表示尾端空白字元可以出現在剖析的字串中。Indicates that trailing white-space characters can be present in the parsed string. 有效泛空白字元有 Unicode 值 U+0009、U+000A、U+000B、U+000C、U+000D 和 U+0020。Valid white-space characters have the Unicode values U+0009, U+000A, U+000B, U+000C, U+000D, and U+0020. 請注意,這是字元的子集,IsWhiteSpace(Char) 方法會針對此子集傳回 trueNote that this is a subset of the characters for which the IsWhiteSpace(Char) method returns true.

Any 511

指示使用除了 AllowHexSpecifier 之外的所有樣式。Indicates that all styles except AllowHexSpecifier are used. 這是複合的數字樣式。This is a composite number style.

Currency 383

指示使用除了 AllowExponentAllowHexSpecifier 之外的所有樣式。Indicates that all styles except AllowExponent and AllowHexSpecifier are used. 這是複合的數字樣式。This is a composite number style.

Float 167

指示使用 AllowLeadingWhiteAllowTrailingWhiteAllowLeadingSignAllowDecimalPointAllowExponent 樣式。Indicates that the AllowLeadingWhite, AllowTrailingWhite, AllowLeadingSign, AllowDecimalPoint, and AllowExponent styles are used. 這是複合的數字樣式。This is a composite number style.

HexNumber 515

指示使用 AllowLeadingWhiteAllowTrailingWhiteAllowHexSpecifier 樣式。Indicates that the AllowLeadingWhite, AllowTrailingWhite, and AllowHexSpecifier styles are used. 這是複合的數字樣式。This is a composite number style.

Integer 7

指示使用 AllowLeadingWhiteAllowTrailingWhiteAllowLeadingSign 樣式。Indicates that the AllowLeadingWhite, AllowTrailingWhite, and AllowLeadingSign styles are used. 這是複合的數字樣式。This is a composite number style.

None 0

表示剖析的字串中不可出現樣式元素,例如前置或尾端空白、千分位分隔符號或十分位分隔符號。Indicates that no style elements, such as leading or trailing white space, thousands separators, or a decimal separator, can be present in the parsed string. 要剖析的字串必須只能由整數位數和小數位數組成。The string to be parsed must consist of integral decimal digits only.

Number 111

指示使用 AllowLeadingWhiteAllowTrailingWhiteAllowLeadingSignAllowTrailingSignAllowDecimalPointAllowThousands 樣式。Indicates that the AllowLeadingWhite, AllowTrailingWhite, AllowLeadingSign, AllowTrailingSign, AllowDecimalPoint, and AllowThousands styles are used. 這是複合的數字樣式。This is a composite number style.

範例

這個範例示範如何使用各種NumberStyles旗標, 將字串剖析為32位的整數。This example shows how to parse a string into a 32-bit integer by using various NumberStyles flags.

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列舉是由兩種列舉值所組成, 用來剖析數值的字串表示:The NumberStyles enumeration consists of two kinds of enumeration values that are used to parse the string representations of numeric values:

  • 個別欄位旗標, 會定義可在剖析的字串中出現的特定樣式元素 (例如空白字元和群組分隔符號)。Individual field flags, which define specific style elements (such as white space and group separators) that can be present in the parsed string.

  • 複合數位樣式, 其中包含多個欄位旗標, 定義可以在剖析的字串中出現的樣式元素。Composite number styles, which consist of multiple field flags that define the style elements that can be present in the parsed string.

除了以外NumberStyles , 列舉中的個別欄位旗標會定義在剖析十進位數的字串表示時所使用的樣式元素。 AllowHexSpecifierExcept for AllowHexSpecifier, the individual field flags in the NumberStyles enumeration define style elements that are used when parsing the string representation of a decimal number. None指出剖析的字串中只可出現數位。None indicates that only digits can be present in the parsed string. 其餘的個別欄位旗標會定義可能是但不一定要出現在十進位數之字串表示中的樣式元素, 以便讓剖析作業成功。The remaining individual field flags define style elements that may be, but do not have to be, present in the string representation of a decimal number for the parse operation to succeed. 相反地, AllowHexSpecifier旗標表示要剖析的字串一律會解讀為十六進位值。In contrast, the AllowHexSpecifier flag indicates that the string to be parsed is always interpreted as a hexadecimal value. 唯一可以搭配使用AllowHexSpecifier的個別欄位旗標是AllowLeadingWhiteAllowTrailingWhiteThe only individual field flags that can be used with AllowHexSpecifier are AllowLeadingWhite and AllowTrailingWhite. 列舉包含複合數位樣式, HexNumber其中包含三個旗標。 NumberStylesThe NumberStyles enumeration includes a composite number style, HexNumber, that consists of all three flags.

可以在要剖析的字串中出現的符號 (例如貨幣符號、群組分隔符號、小數點分隔符號, 以及正和負號) 是由隱含或明確傳遞之System.Globalization.NumberFormatInfo物件的成員所定義。Parse至方法。The symbols (such as the currency symbol, the group separator, the decimal separator, and the positive and negative signs) that can appear in the string to be parsed are defined by the members of the System.Globalization.NumberFormatInfo object that is passed either implicitly or explicitly to the Parse method. 本主題中的 members 資料表會提供每個個別旗標的描述, 並指出NumberFormatInfo其與屬性的關聯性。The members table in this topic provides a description of each individual flag and indicates its relationship to NumberFormatInfo properties.

下表列出複合數位樣式, 並指出它們包含的個別欄位旗標。The following table lists the composite number styles and indicates which individual field flags they include. 資料格中的 "1" 表示複合數位樣式包含該資料列中的個別數位樣式。A "1" in a cell indicates that the composite number style includes the individual number style in that row. "0" 表示複合數位樣式不包含個別的數位樣式。A "0" indicates that the composite number style does not include the individual number style.

AnyAny 貨幣Currency 浮動Float 整數Integer numberNumber HexNumberHexNumber
Numberstyles.allowhexspecifier 除外 (0x0200)AllowHexSpecifier (0x0200) 00 00 00 00 00 11
AllowCurrencySymbol (0x0100)AllowCurrencySymbol (0x0100) 11 11 00 00 00 00
Numberstyles.allowexponent (0x0080)AllowExponent (0x0080) 11 00 11 00 00 00
Numberstyles.allowthousands (0x0040)AllowThousands (0x0040) 11 11 00 00 11 00
AllowDecimalPoint (0x0020)AllowDecimalPoint (0x0020) 11 11 11 00 11 00
AllowParentheses (0x0010)AllowParentheses (0x0010) 11 11 00 00 00 00
AllowTrailingSign (0x0008)AllowTrailingSign (0x0008) 11 11 00 00 11 00
Numberstyles.allowleadingsign (0x0004)AllowLeadingSign (0x0004) 11 11 11 11 11 00
AllowTrailingWhite (0x0002)AllowTrailingWhite (0x0002) 11 11 11 11 11 11
Numberstyles.allowleadingwhite (0x0001)AllowLeadingWhite (0x0001) 11 11 11 11 11 11
(0x1ff)(0x1ff) (0x17f)(0x17f) (0x0a7)(0x0a7) (0x007)(0x007) (0x06f)(0x06f) (0x203)(0x203)

適用於

另請參閱