Int64.Parse 方法

定義

將數字的字串表示轉換成它的對等 64 位元帶正負號的整數。

多載

Parse(String, NumberStyles, IFormatProvider)

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

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

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

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

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

Parse(String, IFormatProvider)

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

Parse(String)

將數字的字串表示轉換成它的對等 64 位元帶正負號的整數。

Parse(ReadOnlySpan<Char>, IFormatProvider)

將字元範圍剖析為值。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

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

Parse(String, NumberStyles)

將指定樣式之數字的字串表示轉換成它的對等 64 位元帶正負號的整數。

Parse(String, NumberStyles, IFormatProvider)

Source:
Int64.cs
Source:
Int64.cs
Source:
Int64.cs

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

public:
 static long Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
 static long Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<long>::Parse;
public static long Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static long Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> int64
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Long

參數

s
String

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

style
NumberStyles

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

provider
IFormatProvider

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

傳回

64 位元帶正負號的整數,與 s 中所指定的數字相等。

實作

例外狀況

snull

style 不是 NumberStyles 值。

-或-

style 不是 AllowHexSpecifierHexNumber 值的組合。

s 的格式與 style 不相容。

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

-或-

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

範例

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

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.
open System
open System.Globalization

let convert (value: string) style provider =
    try
        let number = Int64.Parse(value, style, provider)
        printfn $"Converted '{value}' to {number}."
    with
    | :? FormatException ->
        printfn $"Unable to convert '{value}'."
    | :? OverflowException ->
        printfn $"'{value}' is out of range of the Int64 type."

convert "12,000" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "en-GB")
convert "12,000" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "fr-FR")
convert "12,000" NumberStyles.Float (CultureInfo "en-US")
convert "12 425,00" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "sv-SE")
convert "12,425.00" (NumberStyles.Float ||| NumberStyles.AllowThousands) NumberFormatInfo.InvariantInfo
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint) (CultureInfo "fr-FR")
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint) (CultureInfo "en-US")
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowThousands) (CultureInfo "en-US")

// 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.
Imports System.Globalization

Module ParseInt64
   Public Sub Main()
      Convert("12,000", NumberStyles.Float Or NumberStyles.AllowThousands, _
              New CultureInfo("en-GB"))      
      Convert("12,000", NumberStyles.Float Or NumberStyles.AllowThousands, _
              New CultureInfo("fr-FR"))
      Convert("12,000", NumberStyles.Float, New CultureInfo("en-US"))
      
      Convert("12 425,00", NumberStyles.Float Or NumberStyles.AllowThousands, _
              New CultureInfo("sv-SE")) 
      Convert("12,425.00", NumberStyles.Float Or NumberStyles.AllowThousands, _
              NumberFormatInfo.InvariantInfo) 
      Convert("631,900", NumberStyles.Integer Or NumberStyles.AllowDecimalPoint, _ 
              New CultureInfo("fr-FR"))
      Convert("631,900", NumberStyles.Integer Or NumberStyles.AllowDecimalPoint, _
              New CultureInfo("en-US"))
      Convert("631,900", NumberStyles.Integer Or NumberStyles.AllowThousands, _
              New CultureInfo("en-US"))
   End Sub

   Private Sub Convert(value As String, style As NumberStyles, _
                       provider As IFormatProvider)
      Try
         Dim number As Long = Int64.Parse(value, style, provider)
         Console.WriteLine("Converted '{0}' to {1}.", value, number)
      Catch e As FormatException
         Console.WriteLine("Unable to convert '{0}'.", value)
      Catch e As OverflowException
         Console.WriteLine("'{0}' is out of range of the Int64 type.", value)   
      End Try
   End Sub                       
End Module
' 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 位旗標組合。 根據 的值 styles 參數可能包含下列元素:

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

或者,如果 style 包含 AllowHexSpecifier

[ws]hexdigits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。

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

fractional_digits

exponential_digits
從 0 到 9 的數位序列。
, 特定文化特性的千位分隔符號符號。 如果包含 旗標,則 所指定 provider 文化特性的千位分隔符號可以出現在 中 sNumberStyles.AllowThousandsstyle
. 特定文化特性的小數點符號。 如果包含 旗標,則 所指定 provider 文化特性的小數點符號可能會出現在 中 sNumberStyles.AllowDecimalPointstyle

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

注意

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

只有十進位數的字串 (對應至 NumberStyles.None 樣式) 一律會在類型範圍內 Int64 成功剖析。 大部分的其餘 NumberStyles 成員控制項專案可能存在,但不需要出現在這個輸入字串中。 下表指出個別 NumberStyles 成員如何影響 中 s 可能存在的專案。

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

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

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

另請參閱

適用於

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Source:
Int64.cs
Source:
Int64.cs
Source:
Int64.cs

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

public static long Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
public static long Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> int64
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Long

參數

s
ReadOnlySpan<Char>

範圍,其包含代表所要轉換數字的字元。

style
NumberStyles

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

provider
IFormatProvider

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

傳回

64 位元帶正負號的整數,與 s 中所指定的數字相等。

實作

適用於

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

Source:
Int64.cs
Source:
Int64.cs

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

public static long Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> int64
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Long

參數

utf8Text
ReadOnlySpan<Byte>

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

style
NumberStyles

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

provider
IFormatProvider

提供關於 utf8Text 之特定文化特性格式資訊的物件。

傳回

剖析 utf8Text 的結果。

實作

適用於

Parse(String, IFormatProvider)

Source:
Int64.cs
Source:
Int64.cs
Source:
Int64.cs

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

public:
 static long Parse(System::String ^ s, IFormatProvider ^ provider);
public:
 static long Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<long>::Parse;
public static long Parse (string s, IFormatProvider provider);
public static long Parse (string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> int64
Public Shared Function Parse (s As String, provider As IFormatProvider) As Long

參數

s
String

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

provider
IFormatProvider

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

傳回

64 位元帶正負號的整數,與 s 中所指定的數字相等。

實作

例外狀況

snull

s 的格式不正確。

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

範例

下列範例是 Web 表單的按鈕 Click 事件處理常式。 它會使用 屬性傳回的 HttpRequest.UserLanguages 陣列來判斷使用者的地區設定。 然後,它會具現化 CultureInfo 對應至該地區設定的 物件。 NumberFormatInfo屬於該 CultureInfo 物件的 物件接著會傳遞至 方法, Parse(String, IFormatProvider) 以將使用者的輸入 Int64 轉換為值。

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();
}
Protected Sub OkToLong_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToLong.Click
   Dim locale As String
   Dim culture As CultureInfo
   Dim number As Long

   ' Return if string is empty
   If String.IsNullOrEmpty(Me.inputNumber.Text) Then Exit Sub

   ' Get locale of web request to determine possible format of number
   If Request.UserLanguages.Length = 0 Then Exit Sub
   locale = Request.UserLanguages(0)
   If String.IsNullOrEmpty(locale) Then Exit Sub

   ' 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(Me.inputNumber.Text, culture.NumberFormat)
   Catch ex As FormatException
      Exit Sub
   Catch ex As Exception
      Exit Sub
   End Try

   ' Output number to label on web form
   Me.outputNumber.Text = "Number is " & number.ToString()
End Sub

備註

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

參數 s 包含一些格式:

[ws][sign]digits[ws]

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

ws 選擇性空白字元。

符號 選擇性符號。

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

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

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

另請參閱

適用於

Parse(String)

Source:
Int64.cs
Source:
Int64.cs
Source:
Int64.cs

將數字的字串表示轉換成它的對等 64 位元帶正負號的整數。

public:
 static long Parse(System::String ^ s);
public static long Parse (string s);
static member Parse : string -> int64
Public Shared Function Parse (s As String) As Long

參數

s
String

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

傳回

64 位元帶正負號的整數,與 s 中所包含的數字相等。

例外狀況

snull

s 的格式不正確。

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

範例

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

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.
open System

let convert value =
    try
        let number = Int64.Parse value
        printfn $"Converted '{value}' to {number}."
    with
    | :? FormatException ->
        printfn $"Unable to convert '{value}'."
    | :? OverflowException ->
        printfn $"'{value}' is out of range."

convert "  179042  "
convert " -2041326 "
convert " +8091522 "
convert "   1064.0   "
convert "  178.3"
convert String.Empty

decimal Int64.MaxValue + 1M
|> string
|> convert

// 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.
Module ParseInt64
   Public Sub Main()
      Convert("  179032  ")
      Convert(" -2041326 ")
      Convert(" +8091522 ")
      Convert("   1064.0   ")
      Convert("  178.3")
      Convert(String.Empty)
      Convert((CDec(Int64.MaxValue) + 1).ToString())
   End Sub

   Private Sub Convert(value As String)
      Try
         Dim number As Long = Int64.Parse(value)
         Console.WriteLine("Converted '{0}' to {1}.", value, number)
      Catch e As FormatException
         Console.WriteLine("Unable to convert '{0}'.", value)
      Catch e As OverflowException
         Console.WriteLine("'{0}' is out of range.", value)      
      End Try
   End Sub
End Module
' This example displays the following output to the console:
'       Converted '  179032  ' to 179032.
'       Converted ' -2041326 ' to -2041326.
'       Converted ' +8091522 ' to 8091522.
'       Unable to convert '   1064.0   '.
'       Unable to convert '  178.3'.
'       Unable to convert ''.
'       '9223372036854775808' 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) 方法。

另請參閱

適用於

Parse(ReadOnlySpan<Char>, IFormatProvider)

Source:
Int64.cs
Source:
Int64.cs
Source:
Int64.cs

將字元範圍剖析為值。

public:
 static long Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<long>::Parse;
public static long Parse (ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> int64
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Long

參數

s
ReadOnlySpan<Char>

要剖析的字元範圍。

provider
IFormatProvider

提供關於 s 之特定文化特性格式資訊的物件。

傳回

剖析 s 的結果。

實作

適用於

Parse(ReadOnlySpan<Byte>, IFormatProvider)

Source:
Int64.cs
Source:
Int64.cs

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

public:
 static long Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<long>::Parse;
public static long Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> int64
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As Long

參數

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

提供關於 utf8Text 之特定文化特性格式資訊的物件。

傳回

剖析 utf8Text 的結果。

實作

適用於

Parse(String, NumberStyles)

Source:
Int64.cs
Source:
Int64.cs
Source:
Int64.cs

將指定樣式之數字的字串表示轉換成它的對等 64 位元帶正負號的整數。

public:
 static long Parse(System::String ^ s, System::Globalization::NumberStyles style);
public static long Parse (string s, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> int64
Public Shared Function Parse (s As String, style As NumberStyles) As Long

參數

s
String

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

style
NumberStyles

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

傳回

64 位元帶正負號的整數,與 s 中所指定的數字相等。

例外狀況

snull

style 不是 NumberStyles 值。

-或-

style 不是 AllowHexSpecifierHexNumber 值的組合。

s 的格式與 style 不相容。

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

-或-

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

範例

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

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'.
open System
open System.Globalization

let convert value (style: NumberStyles) =
    try
        let number = Int64.Parse(value, style)
        printfn $"converted '{value}' to {number}." 
    with
    | :? FormatException ->
        printfn $"Unable to convert '{value}'."
    | :? OverflowException ->
        printfn $"'{value}' is out of range of the Int64 type."

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


// 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'.
Imports System.Globalization

Module ParseInt64
   Public Sub Main()
      Convert("104.0", NumberStyles.AllowDecimalPoint)    
      Convert("104.9", NumberStyles.AllowDecimalPoint)
      Convert (" 106034", NumberStyles.None)
      Convert(" $17,198,064.42", NumberStyles.AllowCurrencySymbol Or _
                                 NumberStyles.Number)
      Convert(" $17,198,064.00", NumberStyles.AllowCurrencySymbol Or _
                                 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 Or _
                             NumberStyles.AllowParentheses)
      Convert("FFCA00A0", NumberStyles.HexNumber)                       
      Convert("0xFFCA00A0", NumberStyles.HexNumber)                       
   End Sub
   
   Private Sub Convert(value As String, style As NumberStyles)
      Try
         Dim number As Long = Int64.Parse(value, style)
         Console.WriteLine("Converted '{0}' to {1}.", value, number)
      Catch e As FormatException
         Console.WriteLine("Unable to convert '{0}'.", value)
      Catch e As OverflowException
         Console.WriteLine("'{0}' is out of range of the Int64 type.", value)   
      End Try
   End Sub
End Module
' 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 位旗標組合。 根據 的值 styles 參數可能包含下列元素:

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

或者,如果 style 包含 AllowHexSpecifier

[ws]hexdigits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。

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

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 的十六進位數位序列。

注意

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

只有數位的字串 (對應至 NumberStyles.None 樣式) 一律會在類型範圍內 Int64 成功剖析。 大部分的其餘 NumberStyles 成員控制項專案可能存在,但不需要出現在輸入字串中。 下表指出個別 NumberStyles 成員如何影響 中 s 可能存在的專案。

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

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

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

另請參閱

適用於