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 样式分析的字符串将始终解释为二进制值。 可与 AllowBinarySpecifier 组合的标志只有 AllowLeadingWhiteAllowTrailingWhiteNumberStyles 枚举包括由这三个标识组成的复合样式 BinaryNumber

AllowCurrencySymbol 256

指示数字字符串可包含货币符号。 有效的货币符号由 CurrencySymbol 属性确定。

AllowDecimalPoint 32

指示数字字符串可以具有小数点。 如果 NumberStyles 值包括 AllowCurrencySymbol 标志,分析的字符串包括货币符号,则小数分隔符字符由 CurrencyDecimalSeparator 属性确定。 否则,小数分隔符由 NumberDecimalSeparator 属性确定。

AllowExponent 128

指示数字字符串用于指数符号中。 该 AllowExponent 标志允许分析的字符串包含以“E”或“e”字符开始并且后接可选的正号或负号以及整数的指数。 换句话说,它成功地分析 nnnExx、nnnE+xx 和 nnnE-xx 格式的字符串。 它不允许有效位数或尾数中有小数分隔符或小数符号;若要允许分析字符串中的这些元素,请使用 AllowDecimalPointAllowLeadingSign 标志,或使用包含各个标志的组合样式。

AllowHexSpecifier 512

指示数值字符串表示一个十六进制值。 有效的十六进制值包括数字 0-9 和十六进制数字 A-F 和 a-f。 使用此样式分析的字符串不能以“0x”或“&h”为前缀。 使用 AllowHexSpecifier 样式分析的字符串总是被解释为一个十六进制值。 可与 AllowHexSpecifier 组合的标志只有 AllowLeadingWhiteAllowTrailingWhiteNumberStyles 枚举包括由这三个标识组成的复合样式 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 的单个字段标志是 AllowLeadingWhiteAllowTrailingWhiteNumberStyles枚举包括一个复合数字样式 ,HexNumber它由所有三个标志组成。

可出现在要分析的字符串中的符号 (,例如货币符号、组分隔符、小数点分隔符以及正号和负号) ,由隐式或显式Parse传递给 方法的对象成员System.Globalization.NumberFormatInfo定义。 本主题中的成员表提供了每个单独标志的说明,并指示其与 NumberFormatInfo 属性的关系。

下表列出了复合数字样式,并指示它们包括哪些单独的字段标志。 单元格中的“1”表示复合数字样式包括该行中的单个数字样式。 “0”表示复合数字样式不包括单个数字样式。

任意 货币 Float Integer 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)

适用于

另请参阅