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
- 繼承
- 屬性
欄位
AllowCurrencySymbol | 256 | 指出數字字串是否可以包含貨幣符號。 有效貨幣符號是由 CurrencySymbol 屬性來決定。 |
AllowDecimalPoint | 32 | 指示數值字串可以有小數點。 如果 NumberStyles 值包含 AllowCurrencySymbol 旗標,且剖析的值包含貨幣符號,則十進位分隔符號字元由CurrencyDecimalSeparator 屬性決定。 否則,小數分隔符號字元是由 NumberDecimalSeparator 屬性所決定。 |
AllowExponent | 128 | 指示數值字串可以採用指數標記法。 AllowExponent 旗標允許剖析的字串包含以 "E" 或 "e" 字元開頭,後面跟著選擇性正負號和整數的指數。 換句話說,它能成功剖析 nnnExx、nnnE+xx 和 nnnE-xx 形式的字串。 它不允許有效數字或尾數中的小數分隔符號或正負號。為了要讓字串中的這些項目得以剖析,請使用 AllowDecimalPoint 和 AllowLeadingSign 旗標,或是使用包含這些個別旗標的複合樣式。 |
AllowHexSpecifier | 512 | 指示數值字串會表示十六進位值。 有效十六進位值包括數值位數 0-9 和十六進位位數 A-F 和 a-f, 使用這個樣式剖析的字串不能在前面加上 "0x" 或 &h"。 使用 AllowHexSpecifier 樣式剖析的字串一定會解譯為十六進位值。 其他可合併的旗標僅限 AllowHexSpecifier、AllowLeadingWhite 和 AllowTrailingWhite。 NumberStyles 列舉包含 HexNumber 複合樣式,此樣式是由這三個旗標所組成。 |
AllowLeadingSign | 4 | 指示數值字串可以有前置正負號。 有效前置正負號字元是由 PositiveSign 和 NegativeSign 屬性所決定。 |
AllowLeadingWhite | 1 | 表示前置空白字元可以出現在剖析的字串中。 有效泛空白字元有 Unicode 值 U+0009、U+000A、U+000B、U+000C、U+000D 和 U+0020。 請注意,這是字元的子集,IsWhiteSpace(Char) 方法會針對此子集傳回 |
AllowParentheses | 16 | 指示數值字串可以有一對括住數字的括弧。 括號表示要剖析的字串代表負數。 |
AllowThousands | 64 | 表示數值字串可以有群組分隔符號 (例如,隔開百位與千位的符號)。 NumberStyles如果值包含 AllowCurrencySymbol 旗標,且要剖析的字串包含貨幣符號,則有效的群組分隔符號是由 CurrencyGroupSeparator 屬性決定,而每個群組中的位數則由 CurrencyGroupSizes 屬性決定。 否則,有效群組分隔符號字元是由 NumberGroupSeparator 屬性所決定,而每個群組中的位數則由 NumberGroupSizes 屬性所決定。 |
AllowTrailingSign | 8 | 指示數值字串可以有尾端正負號。 有效尾端正負號字元由 PositiveSign 和 NegativeSign 屬性所決定。 |
AllowTrailingWhite | 2 | 表示尾端空白字元可以出現在剖析的字串中。 有效泛空白字元有 Unicode 值 U+0009、U+000A、U+000B、U+000C、U+000D 和 U+0020。 請注意,這是字元的子集,IsWhiteSpace(Char) 方法會針對此子集傳回 |
Any | 511 | 指示使用除了 AllowHexSpecifier 之外的所有樣式。 這是複合的數字樣式。 |
Currency | 383 | 指示使用除了 AllowExponent 和 AllowHexSpecifier 之外的所有樣式。 這是複合的數字樣式。 |
Float | 167 | 指示使用 AllowLeadingWhite、AllowTrailingWhite、AllowLeadingSign、AllowDecimalPoint 和 AllowExponent 樣式。 這是複合的數字樣式。 |
HexNumber | 515 | 指示使用 AllowLeadingWhite、AllowTrailingWhite 和 AllowHexSpecifier 樣式。 這是複合的數字樣式。 |
Integer | 7 | 指示使用 AllowLeadingWhite、AllowTrailingWhite 和 AllowLeadingSign 樣式。 這是複合的數字樣式。 |
None | 0 | 表示剖析的字串中不可出現樣式元素,例如前置或尾端空白、千分位分隔符號或十分位分隔符號。 要剖析的字串必須只能由整數位數和小數位數組成。 |
Number | 111 | 指示使用 AllowLeadingWhite、AllowTrailingWhite、AllowLeadingSign、AllowTrailingSign、AllowDecimalPoint 和 AllowThousands 樣式。 這是複合的數字樣式。 |
範例
此範例示範如何使用各種 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
使用的個別欄位旗標是 AllowLeadingWhite
和 AllowTrailingWhite
。 列舉 NumberStyles 包含複合數位樣式 , HexNumber
其中包含這三個旗標。
(符號,例如貨幣符號、群組分隔符號、小數分隔符號,以及可顯示在要剖析之字串中的正和負號) 是由隱含或明確傳遞至 Parse
方法的物件成員 System.Globalization.NumberFormatInfo 所定義。 本主題中的成員資料表提供每個個別旗標的描述,並指出其與 NumberFormatInfo 屬性的關聯性。
下表列出複合編號樣式,並指出它們所包含的個別欄位旗標。 儲存格中的 「1」 表示複合編號樣式包含該資料列中的個別數位樣式。 「0」 表示複合編號樣式不包含個別的數位樣式。
任意 | 貨幣 | Float | 整數 | 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) |