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. 换句话说,它成功地分析 nnnExx、nnnE+xx 和 nnnE-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. 可与 AllowHexSpecifier 组合的标志只有 AllowLeadingWhiteAllowTrailingWhiteThe only flags that can be combined with AllowHexSpecifier are AllowLeadingWhite and AllowTrailingWhite. NumberStyles 枚举包括由这三个标识组成的复合样式 HexNumberThe 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) 方法针对其返回 true 的字符的子集。Note 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) 方法针对其返回 true 的字符的子集。Note 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. 本主题中的 "成员" 表提供每个单独标志的说明, 并指示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.

任意Any 货币Currency FloatFloat 整数Integer 数字Number 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
Numberstyles.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)

适用于

另请参阅