NumberStyles 枚举
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
确定传递给整型和浮点数值类型的 Parse 和 TryParse 方法的数字字符串参数中允许的样式。
此枚举支持其成员值的按位组合。
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
- 继承
- 属性
字段
| 名称 | 值 | 说明 |
|---|---|---|
| None | 0 | 指示分析的字符串中不存在任何样式元素,例如前导空格或尾随空格、千位分隔符或小数分隔符。 要分析的字符串必须仅包含整型十进制数字。 |
| AllowLeadingWhite | 1 | 指示在分析的字符串中可以存在前导空格字符。 有效的空白字符具有 Unicode 值 U+0009、U+000A、U+000B、U+000C、U+000D 和 U+0020。 请注意,这是该方法返回IsWhiteSpace(Char)的 |
| AllowTrailingWhite | 2 | 指示在分析的字符串中可以存在尾随空格字符。 有效的空白字符具有 Unicode 值 U+0009、U+000A、U+000B、U+000C、U+000D 和 U+0020。 请注意,这是该方法返回IsWhiteSpace(Char)的 |
| AllowLeadingSign | 4 | 指示数值字符串可以有前导符号。 有效的前导符号字符由 PositiveSign 属性 NegativeSign 确定。 |
| Integer | 7 | 指示AllowLeadingWhite使用和AllowTrailingWhiteAllowLeadingSign样式。 这是复合数字样式。 |
| AllowTrailingSign | 8 | 指示数值字符串可以有尾随符号。 有效的尾随符号字符由 PositiveSign 属性 NegativeSign 确定。 |
| AllowParentheses | 16 | 指示数值字符串可以有一对括住数字的括号。 括号指示要分析的字符串表示负数。 |
| AllowDecimalPoint | 32 | 指示数值字符串可以具有小数点。 NumberStyles如果值包含AllowCurrencySymbol标志,并且分析的字符串包含货币符号,则小数分隔符由CurrencyDecimalSeparator属性确定。 否则,小数分隔符由 NumberDecimalSeparator 属性确定。 |
| AllowThousands | 64 | 指示数值字符串可以具有组分隔符,例如将数百个与数千个分隔符分开的符号。 NumberStyles如果值包括AllowCurrencySymbol标志和要分析的字符串包括货币符号,则有效的组分隔符由CurrencyGroupSeparator该属性确定,并且每个组中的数字数由CurrencyGroupSizes该属性确定。 否则,有效的组分隔符由 NumberGroupSeparator 属性确定,每个组中的数字数由 NumberGroupSizes 该属性确定。 |
| Number | 111 | 指示AllowLeadingWhite使用了 、AllowTrailingWhite、AllowLeadingSignAllowTrailingSign、 AllowDecimalPoint和AllowThousands样式。 这是复合数字样式。 |
| AllowExponent | 128 | 指示数值字符串可以以指数表示法表示法表示。 该 AllowExponent 标志允许分析的字符串包含以“E”或“e”字符开头的指数,后跟可选的正号或负号和整数。 换句话说,它以 nnn E xx、nnnE+xx 和 nnnE-xx 的形式成功分析字符串。 它不允许小数分隔符或登录符号或 mantissa;如果允许分析字符串中的这些元素,请使用 AllowDecimalPoint 和 AllowLeadingSign 标志,或使用包含这些单独标志的复合样式。 |
| Float | 167 | 指示AllowLeadingWhite使用 、AllowTrailingWhite、AllowLeadingSign和AllowDecimalPointAllowExponent样式。 这是复合数字样式。 |
| AllowCurrencySymbol | 256 | 指示数值字符串可以包含货币符号。 有效的货币符号由 CurrencySymbol 属性确定。 |
| Currency | 383 | 指示除和AllowExponent使用的样式之外AllowHexSpecifier的所有样式。 这是复合数字样式。 |
| Any | 511 | 指示除和AllowHexSpecifier使用的样式之外AllowBinarySpecifier的所有样式。 这是复合数字样式。 |
| AllowHexSpecifier | 512 | 指示数值字符串表示十六进制值。 有效的十六进制值包括数字 0-9 和十六进制数字 A-F 和 a-f。 使用此样式分析的字符串不能以“0x”或“&h”为前缀。 使用样式分析的 AllowHexSpecifier 字符串将始终解释为十六进制值。 唯一可以与 AllowHexSpecifier 它们组合的标志是 AllowLeadingWhite 和 AllowTrailingWhite。 枚举 NumberStyles 包括由这三个标志组成的复合样式 HexNumber。 |
| HexNumber | 515 | 指示AllowLeadingWhite使用和AllowTrailingWhiteAllowHexSpecifier样式。 这是复合数字样式。 |
| HexFloat | 679 | |
| AllowBinarySpecifier | 1024 | 指示数值字符串表示二进制值。 有效的二进制值包括数字 0 和 1。 使用此样式分析的字符串不使用前缀; |
| BinaryNumber | 1027 | 指示AllowLeadingWhite使用和AllowTrailingWhiteAllowBinarySpecifier样式。 这是复合数字样式。 |
示例
此示例演示如何使用各种 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'.
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 的唯一单个字段标志是 AllowLeadingWhite 和 AllowTrailingWhite。 枚举 NumberStyles 包括一个复合数字样式, HexNumber该样式由所有三个标志组成。
要分析的字符串中显示的符号(如货币符号、组分隔符、小数分隔符和正号和负号)由隐式或显式System.Globalization.NumberFormatInfo传递给方法的对象的成员Parse定义。 本主题中的成员表提供了每个单独标志的说明,并指示其与 NumberFormatInfo 属性的关系。
下表列出了复合编号样式,并指示它们包含的单个字段标志。 单元格中的“1”表示复合数字样式包括该行中的单个数字样式。 “0”表示复合数字样式不包括单个数字样式。
| 任意 | 货币 | 漂浮 | Integer | 编号 | HexNumber | BinaryNumber | |
|---|---|---|---|---|---|---|---|
| AllowBinarySpecifier (0x0400) | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| AllowHexSpecifier (0x0200) | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
| AllowCurrencySymbol (0x0100) | 1 | 1 | 0 | 0 | 0 | 0 | 0 |
| AllowExponent (0x0080) | 1 | 0 | 1 | 0 | 0 | 0 | 0 |
| AllowThousands (0x0040) | 1 | 1 | 0 | 0 | 1 | 0 | 0 |
| AllowDecimalPoint (0x0020) | 1 | 1 | 1 | 0 | 1 | 0 | 0 |
| AllowParentheses (0x0010) | 1 | 1 | 0 | 0 | 0 | 0 | 0 |
| AllowTrailingSign (0x0008) | 1 | 1 | 0 | 0 | 1 | 0 | 0 |
| AllowLeadingSign (0x0004) | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
| AllowTrailingWhite (0x0002) | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| AllowLeadingWhite (0x0001) | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| (0x1ff) | (0x17f) | (0x0a7) | (0x007) | (0x06f) | (0x203) | (0x403) |