閱讀英文

共用方式為


Int64.Parse 方法

定義

將數位的字串表示轉換為其相等的64位帶正負號整數。

多載

Parse(String)

將數位的字串表示轉換為其相等的64位帶正負號整數。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

將UTF-8字元的範圍剖析為值。

Parse(ReadOnlySpan<Char>, IFormatProvider)

將字元範圍剖析為值。

Parse(String, NumberStyles)

將指定樣式中數位的字串表示轉換為其相等的64位帶正負號整數。

Parse(String, IFormatProvider)

將指定之特定文化特性格式之數位的字串表示轉換為其相等的64位帶正負號整數。

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

將UTF-8字元的範圍剖析為值。

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

將指定樣式和特定文化特性格式的數位範圍表示轉換為其64位帶正負號的整數對等。

Parse(String, NumberStyles, IFormatProvider)

將指定樣式和特定文化特性格式之數位的字串表示轉換為其64位帶正負號的整數對等。

Parse(String)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs

將數位的字串表示轉換為其相等的64位帶正負號整數。

C#
public static long Parse (string s);

參數

s
String

字串,包含要轉換的數位。

傳回

64 位帶正負號的整數,相當於 s中包含的數位。

例外狀況

s 格式不正確。

s 代表小於 Int64.MinValue 或大於 Int64.MaxValue的數位。

範例

下列範例示範如何使用 Int64.Parse(String) 方法,將字串值轉換成 64 位帶正負號的整數值。 然後,它會顯示產生的長整數值。

C#
using System;

public class ParseInt64
{
   public static void Main()
   {
      Convert("  179042  ");
      Convert(" -2041326 ");
      Convert(" +8091522 ");
      Convert("   1064.0   ");
      Convert("  178.3");
      Convert(String.Empty);
      Convert(((decimal) Int64.MaxValue) + 1.ToString());
   }

   private static void Convert(string value)
   {
      try
      {
         long number = Int64.Parse(value);
         Console.WriteLine("Converted '{0}' to {1}.", value, number);
      }
      catch (FormatException)
      {
         Console.WriteLine("Unable to convert '{0}'.", value);
      }
      catch (OverflowException)
      {
         Console.WriteLine("'{0}' is out of range.", value);
      }
   }
}
// This example displays the following output to the console:
//       Converted '  179042  ' to 179042.
//       Converted ' -2041326 ' to -2041326.
//       Converted ' +8091522 ' to 8091522.
//       Unable to convert '   1064.0   '.
//       Unable to convert '  178.3'.
//       Unable to convert ''.
//       '92233720368547758071' is out of range.

備註

s 參數包含一些表單:

[ws][sign]digits[ws]

方括弧 ([ 和 ]) 中的元素是選擇性的。 下表描述每個元素。

元素 描述
ws 選擇性的空格符。
標誌 選擇性符號。
數位 範圍從 0 到 9 的數位序列。

s 參數是使用 NumberStyles.Integer 樣式來解譯。 除了十進位數之外,只允許前置和尾端空格與前置正負號。 若要明確定義可以存在於 s中的樣式專案,請使用 Int64.Parse(String, NumberStyles)Int64.Parse(String, NumberStyles, IFormatProvider) 方法。

s 參數是使用目前系統文化特性初始化之 NumberFormatInfo 物件中的格式資訊來剖析。 若要使用其他文化特性的格式資訊剖析字串,請使用 Int64.Parse(String, NumberStyles, IFormatProvider) 方法。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Parse(ReadOnlySpan<Byte>, IFormatProvider)

來源:
Int64.cs
來源:
Int64.cs

將UTF-8字元的範圍剖析為值。

C#
public static long Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);

參數

utf8Text
ReadOnlySpan<Byte>

要剖析的UTF-8字元範圍。

provider
IFormatProvider

物件,提供與 utf8Text相關的特定文化特性格式資訊。

傳回

剖析 utf8Text的結果。

實作

適用於

.NET 9 和 .NET 8
產品 版本
.NET 8, 9

Parse(ReadOnlySpan<Char>, IFormatProvider)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs

將字元範圍剖析為值。

C#
public static long Parse (ReadOnlySpan<char> s, IFormatProvider? provider);

參數

s
ReadOnlySpan<Char>

要剖析的字元範圍。

provider
IFormatProvider

物件,提供與 s相關的特定文化特性格式資訊。

傳回

剖析 s的結果。

實作

適用於

.NET 9 及其他版本
產品 版本
.NET 7, 8, 9

Parse(String, NumberStyles)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs

將指定樣式中數位的字串表示轉換為其相等的64位帶正負號整數。

C#
public static long Parse (string s, System.Globalization.NumberStyles style);

參數

s
String

字串,包含要轉換的數位。

style
NumberStyles

NumberStyles 值的位元組合,表示 s允許的格式。 要指定的一般值是 Integer

傳回

64 位帶正負號的整數,相當於 s中指定的數位。

例外狀況

style 不是 NumberStyles 值。

-或-

style 不是 AllowHexSpecifierHexNumber 值的組合。

s 的格式與 style不相容。

s 代表小於 Int64.MinValue 或大於 Int64.MaxValue的數位。

-或-

style 支援小數位數,但 s 包含非零的小數位數。

範例

下列範例會使用 Int64.Parse(String, NumberStyles) 方法來剖析數個 Int64 值的字串表示。 這個範例目前的文化特性 en-US。

C#
using System;
using System.Globalization;

public class ParseInt32
{
   public static void Main()
   {
      Convert("104.0", NumberStyles.AllowDecimalPoint);
      Convert("104.9", NumberStyles.AllowDecimalPoint);
      Convert (" 106034", NumberStyles.None);
      Convert(" $17,198,064.42", NumberStyles.AllowCurrencySymbol |
                                 NumberStyles.Number);
      Convert(" $17,198,064.00", NumberStyles.AllowCurrencySymbol |
                                 NumberStyles.Number);
      Convert("103E06", NumberStyles.AllowExponent);
      Convert("1200E-02", NumberStyles.AllowExponent);
      Convert("1200E-03", NumberStyles.AllowExponent);
      Convert("-1,345,791", NumberStyles.AllowThousands);
      Convert("(1,345,791)", NumberStyles.AllowThousands |
                             NumberStyles.AllowParentheses);
      Convert("FFCA00A0", NumberStyles.HexNumber);
      Convert("0xFFCA00A0", NumberStyles.HexNumber);
   }

   private static void Convert(string value, NumberStyles style)
   {
      try
      {
         long number = Int64.Parse(value, style);
         Console.WriteLine("Converted '{0}' to {1}.", value, number);
      }
      catch (FormatException)
      {
         Console.WriteLine("Unable to convert '{0}'.", value);
      }
      catch (OverflowException)
      {
         Console.WriteLine("'{0}' is out of range of the Int64 type.", value);
      }
   }
}
// The example displays the following output to the console:
//       Converted '104.0' to 104.
//       '104.9' is out of range of the Int64 type.
//       Unable to convert ' 106034'.
//       ' $17,198,064.42' is out of range of the Int64 type.
//       Converted ' $17,198,064.00' to 17198064.
//       Converted '103E06' to 103000000.
//       Converted '1200E-02' to 12.
//       '1200E-03' is out of range of the Int64 type.
//       Unable to convert '-1,345,791'.
//       Converted '(1,345,791)' to -1345791.
//       Converted 'FFCA00A0' to 4291428512.
//       Unable to convert '0xFFCA00A0'.

備註

style 參數會定義樣式專案(例如空格符、正負號符號或千位分隔符符號),這些符號在剖析作業 s 參數中允許成功。 它必須是來自 NumberStyles 列舉的位旗標組合。 視 style的值而定,s 參數可能包含下列元素:

[ws][$][sign][digits,]digits[.fractional_digits][e[sign]exponential_digits][ws]

或者,如果 style 包含 AllowHexSpecifier

[ws]hexdigits[ws]

方括弧 ([ 和 ]) 中的元素是選擇性的。 下表描述每個元素。

元素 描述
ws 選擇性的空格符。 如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,則會在 s 開頭顯示空格符,如果 style 包含 NumberStyles.AllowTrailingWhite 旗標,則會出現在 s 結尾。
$ 特定文化特性的貨幣符號。 字串中的位置是由目前文化特性的 NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern 屬性所定義。 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,則目前的文化特性貨幣符號可能會出現在 s 中。
簽署 選擇性符號。 如果 style 包含 NumberStyles.AllowLeadingSign 旗標,則符號可能會出現在 s 開頭,如果 style 包含 NumberStyles.AllowTrailingSign 旗標,則它可能會出現在 s 結尾。 括弧可用於 s,如果 style 包含 NumberStyles.AllowParentheses 旗標,則表示負值。
位數

fractional_digits

exponential_digits
從 0 到 9 的數位序列。 若為 fractional_digits,則只有數位0有效。
特定文化特性的千位分隔符符號。 如果 style 包含 NumberStyles.AllowThousands 旗標,則目前文化特性的千位分隔符可以出現在 s 中。
特定文化特性的小數點符號。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則目前文化特性的小數點符號可能會出現在 s 中。 只有數位 0 可以顯示為小數位數,剖析作業才會成功;如果 fractional_digits 包含任何其他數位,則會擲回 OverflowException
e 'e' 或 'E' 字元,表示值是以指數表示法表示。 如果 style 包含 NumberStyles.AllowExponent 旗標,s 參數就可以以指數表示法來表示數位。
hexdigits 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

不論 style 自變數的值為何,剖析作業都會忽略 s 中任何終止的 NUL (U+0000) 字元。

只有數位的字串(對應至 NumberStyles.None 樣式),如果字串位於 Int64 類型的範圍,則一律會成功剖析。 其餘大部分 NumberStyles 成員控件元素,這些元素可能不是必須存在於輸入字串中。 下表指出個別 NumberStyles 成員如何影響 s中可能出現的專案。

NumberStyles 值 除了數位以外,允許的 元素
None 位數 元素。
AllowDecimalPoint 小數點 (. ) 和 小數位數 元素。
AllowExponent s 參數也可以使用指數表示法。 如果 s 表示指數表示法的數位,則產生的數值不能包含任何非零的小數位數。
AllowLeadingWhite s開頭的 ws ws 專案。
AllowTrailingWhite s結尾的 ws 專案。
AllowLeadingSign s開頭的 符號 專案。
AllowTrailingSign s結尾處的 符號 專案。
AllowParentheses 以括弧括住數值的括弧形式,符號 專案。
AllowThousands 千位分隔符 ( ) 元素。
AllowCurrencySymbol $ 專案。
Currency 都。 s 參數不能代表十六進位數或指數表示法的數位。
Float ws 項目位於 s的開頭或結尾,符號s開頭,以及小數點 (. ) 符號。 s 參數也可以使用指數表示法。
Number ws符號、千位分隔符()和小數點(.) 元素。
Any 除了 s 以外的所有樣式都不能代表十六進位數。

如果使用 NumberStyles.AllowHexSpecifier 旗標,s 必須是不含前置詞的十六進位值。 例如,“C9AF3” 剖析成功,但 “0xC9AF3” 則不會。 唯一可以與 s 參數結合的其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (NumberStyles 列舉包含包含兩個空格符旗標的複合數字樣式 NumberStyles.HexNumber

s 參數是使用目前系統文化特性初始化之 NumberFormatInfo 物件中的格式資訊來剖析。 若要指定格式資訊用於剖析作業的文化特性,請呼叫 Int64.Parse(String, NumberStyles, IFormatProvider) 多載。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Parse(String, IFormatProvider)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs

將指定之特定文化特性格式之數位的字串表示轉換為其相等的64位帶正負號整數。

C#
public static long Parse (string s, IFormatProvider provider);
C#
public static long Parse (string s, IFormatProvider? provider);

參數

s
String

字串,包含要轉換的數位。

provider
IFormatProvider

物件,提供與 s相關的特定文化特性格式資訊。

傳回

64 位帶正負號的整數,相當於 s中指定的數位。

實作

例外狀況

s 格式不正確。

s 代表小於 Int64.MinValue 或大於 Int64.MaxValue的數位。

範例

下列範例是 Web 窗體的按鈕點選事件處理程式。 它會使用 HttpRequest.UserLanguages 屬性所傳回的數位來判斷使用者的地區設定。 然後,它會具現化對應至該地區設定的 CultureInfo 物件。 屬於該 CultureInfo 物件的 NumberFormatInfo 對象接著會傳遞至 Parse(String, IFormatProvider) 方法,將使用者的輸入轉換成 Int64 值。

C#
protected void OkToLong_Click(object sender, EventArgs e)
{
    string locale;
    long number;
    CultureInfo culture;

    // Return if string is empty
    if (String.IsNullOrEmpty(this.inputNumber.Text))
        return;

    // Get locale of web request to determine possible format of number
    if (Request.UserLanguages.Length == 0)
        return;
    locale = Request.UserLanguages[0];
    if (String.IsNullOrEmpty(locale))
        return;

    // Instantiate CultureInfo object for the user's locale
    culture = new CultureInfo(locale);

    // Convert user input from a string to a number
    try
    {
        number = Int64.Parse(this.inputNumber.Text, culture.NumberFormat);
    }
    catch (FormatException)
    {
        return;
    }
    catch (Exception)
    {
        return;
    }
    // Output number to label on web form
    this.outputNumber.Text = "Number is " + number.ToString();
}

備註

這個 Parse(String, IFormatProvider) 方法的多載通常用來將各種格式的文字轉換成 Int64 值。 例如,它可以用來將使用者輸入的文字轉換成 HTML 文字框,轉換為數值。

s 參數包含一些表單:

[ws][sign]digits[ws]

方括弧 ([ 和 ]) 中的項目是選擇性的,其他專案如下。

ws 選擇性空格符。

sign 選擇性符號。

digits 範圍從 0 到 9 的數位序列。

s 參數是使用 NumberStyles.Integer 樣式來解譯。 除了十進位數之外,只允許前置和尾端空格與前置正負號。 若要明確定義可以存在於 s中的樣式專案,請使用 Int64.Parse(String, NumberStyles, IFormatProvider) 方法。

provider 參數是 IFormatProvider 實作,例如 NumberFormatInfoCultureInfo 物件。 provider 參數提供 s格式的文化特性特定資訊。 如果 providernull,則會使用目前文化特性的 NumberFormatInfo

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

來源:
Int64.cs
來源:
Int64.cs

將UTF-8字元的範圍剖析為值。

C#
public static long Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);

參數

utf8Text
ReadOnlySpan<Byte>

要剖析的UTF-8字元範圍。

style
NumberStyles

數字樣式的位元組合,可以存在於 utf8Text中。

provider
IFormatProvider

物件,提供與 utf8Text相關的特定文化特性格式資訊。

傳回

剖析 utf8Text的結果。

實作

適用於

.NET 9 和 .NET 8
產品 版本
.NET 8, 9

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs

將指定樣式和特定文化特性格式的數位範圍表示轉換為其64位帶正負號的整數對等。

C#
public static long Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
C#
public static long Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);

參數

s
ReadOnlySpan<Char>

範圍,包含表示要轉換之數位的字元。

style
NumberStyles

列舉值的位元組合,表示 s中可以存在的樣式專案。 要指定的一般值是 Integer

provider
IFormatProvider

IFormatProvider,提供與 s相關的特定文化特性格式資訊。

傳回

64 位帶正負號的整數,相當於 s中指定的數位。

實作

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Parse(String, NumberStyles, IFormatProvider)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs

將指定樣式和特定文化特性格式之數位的字串表示轉換為其64位帶正負號的整數對等。

C#
public static long Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
C#
public static long Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);

參數

s
String

字串,包含要轉換的數位。

style
NumberStyles

列舉值的位元組合,表示 s中可以存在的樣式專案。 要指定的一般值是 Integer

provider
IFormatProvider

IFormatProvider,提供與 s相關的特定文化特性格式資訊。

傳回

64 位帶正負號的整數,相當於 s中指定的數位。

實作

例外狀況

style 不是 NumberStyles 值。

-或-

style 不是 AllowHexSpecifierHexNumber 值的組合。

s 的格式與 style不相容。

s 代表小於 Int64.MinValue 或大於 Int64.MaxValue的數位。

-或-

style 支援小數位數,但 s 包含非零的小數位數。

範例

下列範例使用各種 styleprovider 參數來剖析 Int64 值的字串表示。 它也說明一些不同的方式,可以解譯相同的字串,取決於格式資訊用於剖析作業的文化特性。

C#
using System;
using System.Globalization;

public class ParseInt64
{
   public static void Main()
   {
      Convert("12,000", NumberStyles.Float | NumberStyles.AllowThousands,
              new CultureInfo("en-GB"));
      Convert("12,000", NumberStyles.Float | NumberStyles.AllowThousands,
              new CultureInfo("fr-FR"));
      Convert("12,000", NumberStyles.Float, new CultureInfo("en-US"));

      Convert("12 425,00", NumberStyles.Float | NumberStyles.AllowThousands,
              new CultureInfo("sv-SE"));
      Convert("12,425.00", NumberStyles.Float | NumberStyles.AllowThousands,
              NumberFormatInfo.InvariantInfo);
      Convert("631,900", NumberStyles.Integer | NumberStyles.AllowDecimalPoint,
              new CultureInfo("fr-FR"));
      Convert("631,900", NumberStyles.Integer | NumberStyles.AllowDecimalPoint,
              new CultureInfo("en-US"));
      Convert("631,900", NumberStyles.Integer | NumberStyles.AllowThousands,
              new CultureInfo("en-US"));
   }

   private static void Convert(string value, NumberStyles style,
                               IFormatProvider provider)
   {
      try
      {
         long number = Int64.Parse(value, style, provider);
         Console.WriteLine("Converted '{0}' to {1}.", value, number);
      }
      catch (FormatException)
      {
         Console.WriteLine("Unable to convert '{0}'.", value);
      }
      catch (OverflowException)
      {
         Console.WriteLine("'{0}' is out of range of the Int64 type.", value);
      }
   }
}
// This example displays the following output to the console:
//       Converted '12,000' to 12000.
//       Converted '12,000' to 12.
//       Unable to convert '12,000'.
//       Converted '12 425,00' to 12425.
//       Converted '12,425.00' to 12425.
//       '631,900' is out of range of the Int64 type.
//       Unable to convert '631,900'.
//       Converted '631,900' to 631900.

備註

style 參數會定義 s 參數中允許的樣式專案(例如空格符或正負號),讓剖析作業成功。 它必須是來自 NumberStyles 列舉的位旗標組合。 根據 style的值,s 參數可能包含下列元素:

[ws][$][sign][digits,]digits[.fractional_digits][e[sign]exponential_digits][ws]

或者,如果 style 包含 AllowHexSpecifier

[ws]hexdigits[ws]

方括弧 ([ 和 ]) 中的元素是選擇性的。 下表描述每個元素。

元素 描述
ws 選擇性的空格符。 如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,則會在 s 開頭顯示空格符,如果 style 包含 NumberStyles.AllowTrailingWhite 旗標,則會出現在 s 結尾。
$ 特定文化特性的貨幣符號。 字串中的位置是由 provider 參數之 GetFormat 方法所傳回之 NumberFormatInfo 物件的 NumberFormatInfo.CurrencyPositivePattern 屬性所定義。 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,貨幣符號可以出現在 s 中。
簽署 選擇性符號。 如果 style 包含 NumberStyles.AllowLeadingSign 旗標,或 sstyle 包含 NumberStyles.AllowTrailingSign 旗標,則符號會顯示在 s 開頭。 括弧可用於 s,如果 style 包含 NumberStyles.AllowParentheses 旗標,則表示負值。
位數

fractional_digits

exponential_digits
從 0 到 9 的數位序列。
特定文化特性的千位分隔符符號。 如果 style 包含 NumberStyles.AllowThousands 旗標,provider 所指定文化特性的千位分隔符可能會出現在 s 中。
特定文化特性的小數點符號。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,provider 所指定之文化特性的小數點符號可能會出現在 s 中。

只有數位 0 可以顯示為小數位數,剖析作業才會成功;如果 fractional_digits 包含任何其他數位,則會擲回 OverflowException
e 'e' 或 'E' 字元,表示值是以指數表示法表示。 如果 style 包含 NumberStyles.AllowExponent 旗標,s 參數就可以以指數表示法來表示數位。
hexdigits 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

不論 style 自變數的值為何,剖析作業都會忽略 s 中任何終止的 NUL (U+0000) 字元。

只有小數字數的字串(對應至 NumberStyles.None 樣式),如果字元串位於 Int64 類型的範圍,則一律會成功剖析。 其餘大部分 NumberStyles 成員控件元素,這些元素可能不是必須存在於此輸入字串中。 下表指出個別 NumberStyles 成員如何影響 s中可能出現的專案。

非複合 NumberStyles 值 除了數位以外,允許的 元素
NumberStyles.None 只有十進位數。
NumberStyles.AllowDecimalPoint 小數點 (. ) 和 小數位數 元素。 不過,小數位數 必須只包含一或多個0位數,否則會擲回 OverflowException
NumberStyles.AllowExponent s 參數也可以使用指數表示法。
NumberStyles.AllowLeadingWhite s開頭的 ws ws 專案。
NumberStyles.AllowTrailingWhite s結尾的 ws 專案。
NumberStyles.AllowLeadingSign 位數之前,可能會出現符號。
NumberStyles.AllowTrailingSign 符號可以在位數之後出現。
NumberStyles.AllowParentheses 以括弧括住數值的括弧形式,符號 專案。
NumberStyles.AllowThousands 千位分隔符 ( ) 元素。
NumberStyles.AllowCurrencySymbol $ 專案。

如果使用 NumberStyles.AllowHexSpecifier 旗標,s 必須是不含前置詞的十六進位值。 例如,“C9AF3” 剖析成功,但 “0xC9AF3” 則不會。 style 中唯一可以存在的其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (NumberStyles 列舉具有包含兩個空格符旗標的複合數字樣式 NumberStyles.HexNumber

provider 參數是 IFormatProvider 實作,例如 NumberFormatInfoCultureInfo 物件。 provider 參數會提供剖析中使用的文化特性特定資訊。 如果 providernull,則會使用目前文化特性的 NumberFormatInfo

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0