共用方式為


Int64.Parse 方法

定義

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

多載

名稱 Description
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
來源:
Int64.cs
來源:
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 的數字。

範例

以下範例示範如何利用此 Int64.Parse(String) 方法將字串值轉換為 64 位元有符號整數值。 然後,它會顯示產生的長整數值。

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 style 來解釋的。 除了十進位數之外,只允許前置和尾端空格與前置正負號。 要明確定義可以存在 s於 中的樣式元素,請使用 該 Int64.Parse(String, NumberStyles) 方法或 方法 Int64.Parse(String, NumberStyles, IFormatProvider)

s參數是利用為當前系統文化初始化的物件格式資訊NumberFormatInfo進行解析。 若要解析字串,使用其他文化的格式資訊,請使用該 Int64.Parse(String, NumberStyles, IFormatProvider) 方法。

另請參閱

適用於

Parse(ReadOnlySpan<Byte>, IFormatProvider)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs
來源:
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(ReadOnlySpan<Char>, IFormatProvider)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs
來源:
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(String, NumberStyles)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs
來源:
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不是 和 HexNumber 的組合AllowHexSpecifier

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 標的組合。 根據 的 style值, s 參數可能包含以下元素:

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

或,若 style 包含 AllowHexSpecifier

[ws]hexdigits[ws]

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

元素 描述
ws 選擇性的空格符。 如果包含該旗幟,空白可以出現在 的s開頭,如果包含該NumberStyles.AllowTrailingWhite旗,則可以出現在 的結尾stylesNumberStyles.AllowLeadingWhitestyle
$ 特定文化特性的貨幣符號。 它在字串中的位置由NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern當前文化的性質所定義。 當前s文化的貨幣符號可以出現style在包含NumberStyles.AllowCurrencySymbol國旗的情況下。
簽署 選擇性符號。 如果包含NumberStyles.AllowLeadingSign旗幟,標誌可以出現在 的styles開頭,如果包含NumberStyles.AllowTrailingSign該旗幟,則可以出現在 的結尾sstyle。 若s包含旗標,style則可使用NumberStyles.AllowParentheses括號表示負值。
數字

fractional_digits

exponential_digits
從 0 到 9 的數位序列。 對於 fractional_digits,只有數字0有效。
, 特定文化特性的千位分隔符符號。 現今文化的千分隔符可以出現s在包含styleNumberStyles.AllowThousands旗幟時。
. 特定文化特性的小數點符號。 當前文化的小數點符號可以出現sstyle在包含NumberStyles.AllowDecimalPoint國旗的情況下。 只有數字 0 可以以小數數字出現,才能成功進行解析操作;若 fractional_digits 包含其他數字,則擲出 a OverflowException
e 'e' 或 'E' 字元,表示值是以指數表示法表示。 若s包含style旗標,參數NumberStyles.AllowExponent可用指數符號表示數字。
六角位數 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。

只有數字的字串(對應 NumberStyles.None 於樣式)若在該類型範圍內 Int64 ,則能成功解析。 大多數剩餘 NumberStyles 成員控制這些元素可能存在但不必須存在於輸入字串中。 下表顯示個別 NumberStyles 成員如何影響可能存在於 s中的元素。

NumberStyles 值 除了數位以外,允許的 元素
None 只有 數字 元素。
AllowDecimalPoint 小數點( . )和 小數數字 元素。
AllowExponent 參數 s 也可以使用指數符號。 若 s 表示指數符號中的數字,所得數值不得包含任何非零的分數數字。
AllowLeadingWhite 開頭sw 元素。
AllowTrailingWhite 末尾s 元素。
AllowLeadingSign 符號 元素位於 的開頭 s
AllowTrailingSign 符號 元素位於 的結尾 s
AllowParentheses 符號 元素以 括號形式包含數值。
AllowThousands 千分隔符( )元素。
AllowCurrencySymbol 元素 $
Currency 都。 該 s 參數無法代表十六進位數或指數符號中的數字。
Float 符號s的開頭或結尾有 ws 元素,符號開頭s符號,以及小數點(. )符號。 參數 s 也可以使用指數符號。
Number 包括 ws符號、千分音符( 、 ) 和小數點( . )元素。
Any 除了 s 不能代表十六進位數字外,所有樣式都無法代表。

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

s參數是利用為當前系統文化初始化的物件格式資訊NumberFormatInfo進行解析。 若要指定用於解析操作的格式資訊文化,請呼叫 overload Int64.Parse(String, NumberStyles, IFormatProvider)

另請參閱

適用於

Parse(String, IFormatProvider)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs
來源:
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 窗體的按鈕點選事件處理程式。 它利用屬性 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 選擇性空格符。

sign 選擇性符號。

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

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

參數 provider 是一個 IFormatProvider 實作,例如 a NumberFormatInfoCultureInfo 物件。 該 provider 參數提供關於 格式 s的文化特定資訊。 若 providernullNumberFormatInfo則 為當前文化。

另請參閱

適用於

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs
來源:
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(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs
來源:
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(String, NumberStyles, IFormatProvider)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs
來源:
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不是 和 HexNumber 的組合AllowHexSpecifier

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 標的組合。 根據 的 style值, s 參數可能包含以下元素:

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

或,若 style 包含 AllowHexSpecifier

[ws]hexdigits[ws]

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

元素 描述
ws 選擇性的空格符。 如果包含該旗幟,空白可以出現在 的s開頭,如果包含該NumberStyles.AllowTrailingWhite旗,則可以出現在 的結尾stylesNumberStyles.AllowLeadingWhitestyle
$ 特定文化特性的貨幣符號。 它在字串中的位置由NumberFormatInfo.CurrencyPositivePattern參數方法NumberFormatInfo回傳的GetFormat物件性質provider所定義。 貨幣s符號可出現style在包含NumberStyles.AllowCurrencySymbol旗幟時。
簽署 選擇性符號。 該標誌可以出現在 如果s包含旗幟的開頭,或在包含旗幟的話結尾styleNumberStyles.AllowTrailingSignsNumberStyles.AllowLeadingSignstyles包含旗標,style則可使用NumberStyles.AllowParentheses括號表示負值。
數字

fractional_digits

exponential_digits
從 0 到 9 的數位序列。
, 特定文化特性的千位分隔符符號。 文化的provider千分隔符可出現s在包含styleNumberStyles.AllowThousands旗幟時。
. 特定文化特性的小數點符號。 provider若包含s旗幟,則可出現在 中styleNumberStyles.AllowDecimalPoint

只有數字 0 可以以小數數字出現,才能成功進行解析操作;若 fractional_digits 包含其他數字,則擲出 a OverflowException
e 'e' 或 'E' 字元,表示值是以指數表示法表示。 若s包含style旗標,參數NumberStyles.AllowExponent可用指數符號表示數字。
六角位數 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。

僅有十進位數字的字串(對應 NumberStyles.None 於樣式)若在該類型範圍內 Int64 ,則總是能成功解析。 其餘 NumberStyles 大多數成員控制此輸入字串中可能存在但不強制存在的元素。 下表顯示個別 NumberStyles 成員如何影響可能存在於 s中的元素。

非複合 NumberStyles 值 除了數位以外,允許的 元素
NumberStyles.None 只有十進位數。
NumberStyles.AllowDecimalPoint 小數點( . )和 小數數字 元素。 然而, 小數數字 必須只包含一個或多個 0,否則會丟出 a OverflowException
NumberStyles.AllowExponent 參數 s 也可以使用指數符號。
NumberStyles.AllowLeadingWhite 開頭sw 元素。
NumberStyles.AllowTrailingWhite 末尾s 元素。
NumberStyles.AllowLeadingSign 符號可以出現在 數字之前。
NumberStyles.AllowTrailingSign 數字 後面會出現符號。
NumberStyles.AllowParentheses 符號 元素以 括號形式包含數值。
NumberStyles.AllowThousands 千分隔符( )元素。
NumberStyles.AllowCurrencySymbol 元素 $

NumberStyles.AllowHexSpecifier 使用該旗標, s 必須為無前綴的十六進位值。 例如,“C9AF3” 剖析成功,但 “0xC9AF3” 則不會。 唯一可能存在於 中的 style 其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (列 NumberStyles 舉採用複合數字風格, NumberStyles.HexNumber包含兩個空白旗。)

參數 provider 是一個 IFormatProvider 實作,例如 a NumberFormatInfoCultureInfo 物件。 參數 provider 提供解析中所需的特定文化資訊。 若 providernullNumberFormatInfo則 為當前文化。

另請參閱

適用於