NumberStyles 枚举

确定数字字符串参数中允许的样式,这些参数被传递到数字基类型类的 Parse 方法。

此枚举有一个 FlagsAttribute 属性,允许其成员值按位组合。

**命名空间:**System.Globalization
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<FlagsAttribute> _
<ComVisibleAttribute(True)> _
Public Enumeration NumberStyles
用法
Dim instance As NumberStyles
[SerializableAttribute] 
[FlagsAttribute] 
[ComVisibleAttribute(true)] 
public enum NumberStyles
[SerializableAttribute] 
[FlagsAttribute] 
[ComVisibleAttribute(true)] 
public enum class NumberStyles
/** @attribute SerializableAttribute() */ 
/** @attribute FlagsAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public enum NumberStyles
SerializableAttribute 
FlagsAttribute 
ComVisibleAttribute(true) 
public enum NumberStyles

成员

  成员名称 说明
由 .NET Compact Framework 支持 AllowCurrencySymbol 指示如果数字字符串包含货币符号则将其作为货币分析;否则,将其作为数字分析。有效的货币符号由 NumberFormatInfoCurrencySymbol 属性确定。 
由 .NET Compact Framework 支持 AllowDecimalPoint 指示数字字符串可以具有小数点。有效的小数点字符由 NumberFormatInfoNumberDecimalSeparatorCurrencyDecimalSeparator 属性确定。 
由 .NET Compact Framework 支持 AllowExponent 指示数字字符串用于指数符号中。 
由 .NET Compact Framework 支持 AllowHexSpecifier 指示数值字符串表示一个十六进制值。有效的十六进制值包括数字 0-9 和十六进制数字 A-F 与 a-f。十六进制值的左侧可以用零填充。使用此样式分析的字符串不允许以“0x”为前缀。 
由 .NET Compact Framework 支持 AllowLeadingSign 指示数字字符串可以具有前导符号。有效的前导符号字符是由 NumberFormatInfoPositiveSignNegativeSign 属性确定的。 
由 .NET Compact Framework 支持 AllowLeadingWhite 指示在分析期间必须忽略前导空白字符。有效的空白字符具有 Unicode 值 U+0009、U+000A、U+000B、U+000C、U+000D 和 U+0020。 
由 .NET Compact Framework 支持 AllowParentheses 指示数字字符串可以具有一对将数字括起来的括号。 
由 .NET Compact Framework 支持 AllowThousands 指示数字字符串可以具有组分隔符;例如,将百位与千位分隔开来。有效的组分隔符字符是由 NumberFormatInfoNumberGroupSeparatorCurrencyGroupSeparator 属性确定的,并且每组中的位数是由 NumberFormatInfoNumberGroupSizesCurrencyGroupSizes 属性确定的。 
由 .NET Compact Framework 支持 AllowTrailingSign 指示数字字符串可以具有结尾符号。有效的结尾符号字符是由 NumberFormatInfoPositiveSignNegativeSign 属性确定的。 
由 .NET Compact Framework 支持 AllowTrailingWhite 指示分析期间必须忽略结尾空白字符。有效的空白字符具有 Unicode 值 U+0009、U+000A、U+000B、U+000C、U+000D 和 U+0020。 
由 .NET Compact Framework 支持 Any 指示使用 AllowHexSpecifier 以外的所有样式。这是复合数字样式。 
由 .NET Compact Framework 支持 Currency 指示使用 AllowExponent 和 AllowHexSpecifier 以外的所有样式。这是复合数字样式。 
由 .NET Compact Framework 支持 Float 指示使用 AllowLeadingWhite、AllowTrailingWhite、AllowLeadingSign、AllowDecimalPoint 和 AllowExponent 样式。这是复合数字样式。 
由 .NET Compact Framework 支持 HexNumber 指示使用 AllowLeadingWhite、AllowTrailingWhite 和 AllowHexSpecifier 样式。这是复合数字样式。 
由 .NET Compact Framework 支持 Integer 指示使用 AllowLeadingWhite、AllowTrailingWhite 和 AllowLeadingSign 样式。这是复合数字样式。 
由 .NET Compact Framework 支持 None 指示不允许使用任何位样式。 
由 .NET Compact Framework 支持 Number 指示使用 AllowLeadingWhite、AllowTrailingWhite、AllowLeadingSign、AllowTrailingSign、AllowDecimalPoint 和 AllowThousands 样式。这是复合数字样式。 

备注

要用于货币符号、千分号、小数点指示符和前导符号的符号由 NumberFormatInfo 指定。

AllowCurrencySymbolAllowDecimalPoint 值确定 Parse 方法是将数值类型的实例视作货币还是视作数字,并因此确定是使用关联的 NumberFormatInfo 的货币属性还是数字属性。例如,如果指定 AllowCurrencySymbol,那么 Parse 方法使用货币属性,例如 CurrencyDecimalSeparatorCurrencyGroupSeparator。但是,如果指定 AllowDecimalPoint,则该方法使用数字属性,例如 NumberDecimalSeparatorNumberGroupSeparator

NumberStyles 的属性是通过对字段标志使用按位“或”运算设置的。最高有效位 (MSB) 是最左边的位。

在下表中,各行是具有单独字段标志的数字样式,各列是组合单独字段标志的复合数字样式。

在复合数字样式下,“1”表示复合数字样式包括该行中的单独数字样式;“0”表示复合数字样式不包括该行中的单独数字样式。

复合数字样式的十六进制表示形式在该表的最后一行列出。

任意

Currency

Float

Integer

数字

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)

AllowExponent 样式用于数字字符串中,其格式为以下格式之一:

[-] m. dddddd E+ xx

[-] m. dddddd E- xx

[-] m. dddddd e+ xx

[-] m. dddddd e- xx

一个或多个非零数字 ( m ) 位于小数分隔符(“.”)之前。减号(“-”)可以位于 m 之前。执行转换的 Type 确定该字符串中小数的位数 ( dddddd ) 以及 xx 和 m 的最大值和最小值。指数 (+ / - xx ) 由加号或减号组成,后跟至少一位。

示例

此示例显示如何使用各种 NumberStyles 标志将字符串分析成一个 32 位整数。

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

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

System.Globalization 命名空间
NumberFormatInfo 类