Int64.Parse Int64.Parse Int64.Parse Int64.Parse Method

定義

將數字的字串表示轉換成它的對等 64 位元帶正負號的整數。Converts the string representation of a number to its 64-bit signed integer equivalent.

多載

Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider)

將指定樣式和特定文化特性格式之數字的字串表示轉換成它的對等 64 位元帶正負號的整數。Converts the string representation of a number in a specified style and culture-specific format to its 64-bit signed integer equivalent.

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles)

將指定樣式之數字的字串表示轉換成它的對等 64 位元帶正負號的整數。Converts the string representation of a number in a specified style to its 64-bit signed integer equivalent.

Parse(String) Parse(String) Parse(String) Parse(String)

將數字的字串表示轉換成它的對等 64 位元帶正負號的整數。Converts the string representation of a number to its 64-bit signed integer equivalent.

Parse(String, IFormatProvider) Parse(String, IFormatProvider) Parse(String, IFormatProvider) Parse(String, IFormatProvider)

將指定特定文化特性格式之數字的字串表示轉換成它的對等 64 位元帶正負號的整數。Converts the string representation of a number in a specified culture-specific format to its 64-bit signed integer equivalent.

Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider)

將指定樣式和特定文化特性格式之數字的字串表示轉換成它的對等 64 位元帶正負號的整數。Converts the string representation of a number in a specified style and culture-specific format to its 64-bit signed integer equivalent.

public:
 static long Parse(System::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 String String String

字串,包含要轉換的數字。A string containing a number to convert.

style
NumberStyles NumberStyles NumberStyles NumberStyles

列舉值的位元組合,表示 s 中可以存在的樣式項目。A bitwise combination of enumeration values that indicates the style elements that can be present in s. 一般會指定的值是 IntegerA typical value to specify is Integer.

provider
IFormatProvider IFormatProvider IFormatProvider IFormatProvider

IFormatProvider,提供 s 的相關特定文化特性格式資訊。An IFormatProvider that supplies culture-specific formatting information about s.

傳回

64 位元帶正負號的整數,與 s 中所指定的數字相等。A 64-bit signed integer equivalent to the number specified in s.

例外狀況

style 不是 NumberStyles 值。style is not a NumberStyles value.

-或--or- style 不是 AllowHexSpecifierHexNumber 值的組合。style is not a combination of AllowHexSpecifier and HexNumber values.

s 的格式與 style 不相容。s is not in a format compliant with style.

s 代表小於 MinValue 或大於 MaxValue 的數字。s represents a number less than MinValue or greater than MaxValue.

-或--or- style 支援小數數字,但 s 包含非零的小數數字。style supports fractional digits, but s includes non-zero fractional digits.

範例

下列範例會使用各種styleprovider剖析的字串表示的參數Int64值。The following example uses a variety of style and provider parameters to parse the string representations of Int64 values. 它也說明一些可以解譯相同的字串,根據文化特性的格式資訊用於剖析作業中的不同方式。It also illustrates some of the different ways the same string can be interpreted depending on the culture whose formatting information is used for the parsing operation.

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.
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剖析作業成功的參數。The style parameter defines the style elements (such as white space or the positive sign) that are allowed in the s parameter for the parse operation to succeed. 它必須是從位元旗標的組合NumberStyles列舉型別。It must be a combination of bit flags from the NumberStyles enumeration. 值而定style,則s參數可能包括下列項目:Depending on the value of style, the s parameter may include the following elements:

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

或者,如果style包含AllowHexSpecifier:Or, if style includes AllowHexSpecifier:

[ws]hexdigits[ws][ws]hexdigits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。Elements in square brackets ([ and ]) are optional. 下表說明每個元素。The following table describes each element.

元素Element 描述Description
wsws 選擇性的泛空白字元。Optional white space. 空白字元可以出現在開頭s如果style包含NumberStyles.AllowLeadingWhite旗標,也可以顯示在結尾s如果style包含NumberStyles.AllowTrailingWhite旗標。White space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
$ 特定文化特性的貨幣符號。A culture-specific currency symbol. 在字串中的位置由定義NumberFormatInfo.CurrencyPositivePattern的屬性NumberFormatInfo所傳回的物件GetFormat方法provider參數。Its position in the string is defined by the NumberFormatInfo.CurrencyPositivePattern property of the NumberFormatInfo object returned by the GetFormat method of the provider parameter. 貨幣符號會出現在s如果style包含NumberStyles.AllowCurrencySymbol旗標。The currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
簽署sign 選擇性正負號。An optional sign. 符號可以出現在開頭s如果style包含NumberStyles.AllowLeadingSign旗標或結尾處s如果style包含NumberStyles.AllowTrailingSign旗標。The sign can appear at the beginning of s if style includes the NumberStyles.AllowLeadingSign flag or at the end of s if style includes the NumberStyles.AllowTrailingSign flag. 可用於括號s表示負值style包含NumberStyles.AllowParentheses旗標。Parentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.
digitsdigits

fractional_digitsfractional_digits

exponential_digitsexponential_digits
從 0 到 9 的數字序列。A sequence of digits from 0 through 9.
, 特定文化特性的千位分隔符號。A culture-specific thousands separator symbol. 千分位分隔符號所指定的文化特性provider可以出現在s如果style包含NumberStyles.AllowThousands旗標。The thousands separator of the culture specified by provider can appear in s if style includes the NumberStyles.AllowThousands flag.
.. 特定文化特性的貨幣符號。A culture-specific decimal point symbol. 指定的文化特性的小數點符號provider可以出現在s如果style包含NumberStyles.AllowDecimalPoint旗標。The decimal point symbol of the culture specified by provider can appear in s if style includes the NumberStyles.AllowDecimalPoint flag.

只是數字 0 會顯示為小數位數的剖析作業才會成功;如果fractional_digits包含任何其他數字,OverflowException就會擲回。Only the digit 0 can appear as a fractional digit for the parse operation to succeed; if fractional_digits includes any other digit, an OverflowException is thrown.
ee 'E' 或者 'E' 字元,表示代表的值時,會以指數標記法。The 'e' or 'E' character, which indicates that the value is represented in exponential notation. s參數可代表指數標記法之數字,如果style包含NumberStyles.AllowExponent旗標。The s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.
hexdigitshexdigits 一連串的從 0 到 f 或 0 到 f 的十六進位數字A sequence of hexadecimal digits from 0 through f, or 0 through F.

注意

中的任何終止 NUL (u+0000) 字元s剖析作業中,不論值會忽略style引數。Any terminating NUL (U+0000) characters in s are ignored by the parsing operation, regardless of the value of the style argument.

十進位數字的字串 (對應至NumberStyles.None樣式) 的範圍內時永遠會成功的剖析Int64型別。A string with decimal digits only (which corresponds to the NumberStyles.None style) always parses successfully if it is in the range of the Int64 type. 大部分的其餘NumberStyles成員控制可能,但不是一定要出現在此輸入字串的項目。Most of the remaining NumberStyles members control elements that may be but are not required to be present in this input string. 下表指出如何個別NumberStyles成員會影響可能會出現在項目sThe following table indicates how individual NumberStyles members affect the elements that may be present in s.

非複合的 NumberStyles 值Non-composite NumberStyles values 允許 s 除了數字中的項目Elements permitted in s in addition to digits
NumberStyles.None 只有小數位數。Decimal digits only.
NumberStyles.AllowDecimalPoint 小數點 ( The decimal point ( . ) 和小數位數項目。) and fractional-digits elements. 不過,小數位數必須包含一個或多個 0 的數字或OverflowException就會擲回。However, fractional-digits must consist of only one or more 0 digits or an OverflowException is thrown.
NumberStyles.AllowExponent s參數也可以使用指數標記法。The s parameter can also use exponential notation.
NumberStyles.AllowLeadingWhite Ws開頭的項目sThe ws element at the beginning of s.
NumberStyles.AllowTrailingWhite Ws結尾的項目sThe ws element at the end of s.
NumberStyles.AllowLeadingSign 之前不可以出現登位數A sign can appear before digits.
NumberStyles.AllowTrailingSign 之後可以出現的徵兆位數A sign can appear after digits.
NumberStyles.AllowParentheses 括號括住的數字值的形式的項目。The sign element in the form of parentheses enclosing the numeric value.
NumberStyles.AllowThousands 千分位分隔符號 ( ) 項目。The thousands separator ( , ) element.
NumberStyles.AllowCurrencySymbol $ 項目。The $ element.

如果NumberStyles.AllowHexSpecifier旗標用,s必須是不含前置詞的十六進位值。If the NumberStyles.AllowHexSpecifier flag is used, s must be a hexadecimal value without a prefix. 例如,"C9AF3 「 剖析成功,但是"0xC9AF3 」 並不會。For example, "C9AF3" parses successfully, but "0xC9AF3" does not. 只有其他旗標可存在於styleNumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteThe only other flags that can be present in style are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. (NumberStyles列舉型別有複合的數字樣式NumberStyles.HexNumber,其中包含這兩個空白字元旗標。)(The NumberStyles enumeration has a composite number style, NumberStyles.HexNumber, that includes both white space flags.)

provider參數是IFormatProvider實作,例如NumberFormatInfoCultureInfo物件。The provider parameter is an IFormatProvider implementation, such as a NumberFormatInfo or CultureInfo object. provider參數提供用於剖析的特定文化特性資訊。The provider parameter supplies culture-specific information used in parsing. 如果providernull,則NumberFormatInfo會使用目前的文化特性。If provider is null, the NumberFormatInfo for the current culture is used.

另請參閱

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

public static long Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = null);
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 = null) As Long

參數

傳回

Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles)

將指定樣式之數字的字串表示轉換成它的對等 64 位元帶正負號的整數。Converts the string representation of a number in a specified style to its 64-bit signed integer equivalent.

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 String String String

字串,包含要轉換的數字。A string containing a number to convert.

style
NumberStyles NumberStyles NumberStyles NumberStyles

NumberStyles 值的位元組合,表示 s 的允許格式。A bitwise combination of NumberStyles values that indicates the permitted format of s. 一般會指定的值是 IntegerA typical value to specify is Integer.

傳回

64 位元帶正負號的整數,與 s 中所指定的數字相等。A 64-bit signed integer equivalent to the number specified in s.

例外狀況

style 不是 NumberStyles 值。style is not a NumberStyles value.

-或--or- style 不是 AllowHexSpecifierHexNumber 值的組合。style is not a combination of AllowHexSpecifier and HexNumber values.

s 的格式與 style 不相容。s is not in a format compliant with style.

s 代表小於 MinValue 或大於 MaxValue 的數字。s represents a number less than MinValue or greater than MaxValue.

-或--or- style 支援小數數字,但 s 包含非零的小數數字。style supports fractional digits but s includes non-zero fractional digits.

範例

下列範例會使用Int64.Parse(String, NumberStyles)方法來剖析的字串表示,數個Int64值。The following example uses the Int64.Parse(String, NumberStyles) method to parse the string representations of several Int64 values. 目前的文化特性的範例中是 EN-US。The current culture for the example is 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'.
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剖析作業成功的參數。The style parameter defines the style elements (such as white space, the positive or negative sign symbol, or the thousands separator symbol) that are allowed in the s parameter for the parse operation to succeed. 它必須是從位元旗標的組合NumberStyles列舉型別。It must be a combination of bit flags from the NumberStyles enumeration. 值而定style,則s參數可能包含下列項目:Depending on the value of style, the s parameter may contain the following elements:

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

或者,如果style包含AllowHexSpecifier:Or, if style includes AllowHexSpecifier:

[ws]hexdigits[ws][ws]hexdigits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。Elements in square brackets ([ and ]) are optional. 下表說明每個元素。The following table describes each element.

元素Element 描述Description
wsws 選擇性的泛空白字元。Optional white space. 空白字元可以出現在開頭s如果style包含NumberStyles.AllowLeadingWhite旗標,也可以顯示在結尾s如果style包含NumberStyles.AllowTrailingWhite旗標。White space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
$ 特定文化特性的貨幣符號。A culture-specific currency symbol. 在字串中的位置由定義NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern目前文化特性的屬性。Its position in the string is defined by the NumberFormatInfo.CurrencyNegativePattern and NumberFormatInfo.CurrencyPositivePattern properties of the current culture. 目前的文化特性的貨幣符號會出現在s如果style包含NumberStyles.AllowCurrencySymbol旗標。The current culture's currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
簽署sign 選擇性正負號。An optional sign. 符號可以出現在開頭s如果style包含NumberStyles.AllowLeadingSign旗標,也可以顯示在結尾s如果style包含NumberStyles.AllowTrailingSign旗標。The sign can appear at the beginning of s if style includes the NumberStyles.AllowLeadingSign flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingSign flag. 可用於括號s表示負值style包含NumberStyles.AllowParentheses旗標。Parentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.
digitsdigits

fractional_digitsfractional_digits

exponential_digitsexponential_digits
從 0 到 9 的數字序列。A sequence of digits from 0 through 9. 針對fractional_digits,只有數字 0 是有效。For fractional_digits, only the digit 0 is valid.
, 特定文化特性的千位分隔符號。A culture-specific thousands separator symbol. 目前的文化特性的千分位分隔符號可以出現在s如果style包含NumberStyles.AllowThousands旗標。The current culture's thousands separator can appear in s if style includes the NumberStyles.AllowThousands flag.
.. 特定文化特性的貨幣符號。A culture-specific decimal point symbol. 目前的文化特性的貨幣符號會出現在s如果style包含NumberStyles.AllowDecimalPoint旗標。The current culture's decimal point symbol can appear in s if style includes the NumberStyles.AllowDecimalPoint flag. 只是數字 0 會顯示為小數位數的剖析作業才會成功;如果fractional_digits包含任何其他數字,OverflowException就會擲回。Only the digit 0 can appear as a fractional digit for the parse operation to succeed; if fractional_digits includes any other digit, an OverflowException is thrown.
ee 'E' 或者 'E' 字元,表示代表的值時,會以指數標記法。The 'e' or 'E' character, which indicates that the value is represented in exponential notation. s參數可代表指數標記法之數字,如果style包含NumberStyles.AllowExponent旗標。The s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.
hexdigitshexdigits 一連串的從 0 到 f 或 0 到 f 的十六進位數字A sequence of hexadecimal digits from 0 through f, or 0 through F.

注意

中的任何終止 NUL (u+0000) 字元s剖析作業中,不論值會忽略style引數。Any terminating NUL (U+0000) characters in s are ignored by the parsing operation, regardless of the value of the style argument.

只有數字的字串 (對應至NumberStyles.None樣式) 的範圍內時永遠會成功的剖析Int64型別。A string with digits only (which corresponds to the NumberStyles.None style) always parses successfully if it is in the range of the Int64 type. 大部分的其餘NumberStyles成員控制可能,但不是一定要出現在輸入字串的項目。Most of the remaining NumberStyles members control elements that may be but are not required to be present in the input string. 下表指出如何個別NumberStyles成員會影響可能會出現在項目sThe following table indicates how individual NumberStyles members affect the elements that may be present in s.

NumberStyles 值NumberStyles value 允許 s 除了數字中的項目Elements permitted in s in addition to digits
None 數字只有項目。The digits element only.
AllowDecimalPoint 小數點 ( The decimal point ( . ) 和小數位數項目。) and fractional-digits elements.
AllowExponent s參數也可以使用指數標記法。The s parameter can also use exponential notation. 如果s表示指數標記法,產生的數字值的數字不能包含任何非零的小數數字。If s represents a number in exponential notation, the resulting numeric value cannot include any non-zero fractional digits.
AllowLeadingWhite Ws開頭的項目sThe ws element at the beginning of s.
AllowTrailingWhite Ws結尾的項目sThe ws element at the end of s.
AllowLeadingSign 開頭的項目sThe sign element at the beginning of s.
AllowTrailingSign 結尾的項目sThe sign element at the end of s.
AllowParentheses 括號括住的數字值的形式的項目。The sign element in the form of parentheses enclosing the numeric value.
AllowThousands 千分位分隔符號 ( ) 項目。The thousands separator ( , ) element.
AllowCurrencySymbol $ 項目。The $ element.
Currency 全部。All. s參數不能代表十六進位數字或指數標記法之數字。The s parameter cannot represent a hexadecimal number or a number in exponential notation.
Float Ws元素的開頭或結尾s開頭s,並在小數點 ( The ws element at the beginning or end of s, sign at the beginning of s, and the decimal point ( . ) 符號。) symbol. s參數也可以使用指數標記法。The s parameter can also use exponential notation.
Number Ws,千分位分隔符號 ( ),和小數點 ( The ws, sign, thousands separator ( , ), and decimal point ( . ) 項目。) elements.
Any 所有的樣式,除了s無法代表十六進位數字。All styles, except s cannot represent a hexadecimal number.

如果NumberStyles.AllowHexSpecifier旗標用,s必須是不含前置詞的十六進位值。If the NumberStyles.AllowHexSpecifier flag is used, s must be a hexadecimal value without a prefix. 例如,"C9AF3 「 剖析成功,但是"0xC9AF3 」 並不會。For example, "C9AF3" parses successfully, but "0xC9AF3" does not. 只有其他旗標,可以結合s參數都NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteThe only other flags that can be combined with the s parameter are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. (NumberStyles列舉會包含複合的數字樣式NumberStyles.HexNumber,其中包含這兩個空白字元旗標。)(The NumberStyles enumeration includes a composite number style, NumberStyles.HexNumber, that includes both white space flags.)

s參數會剖析使用的格式設定資訊中NumberFormatInfo初始化目前系統文化特性的物件。The s parameter is parsed using the formatting information in a NumberFormatInfo object that is initialized for the current system culture. 若要指定的文化特性的格式資訊用於剖析作業,請呼叫Int64.Parse(String, NumberStyles, IFormatProvider)多載。To specify the culture whose formatting information is used for the parse operation, call the Int64.Parse(String, NumberStyles, IFormatProvider) overload.

另請參閱

Parse(String) Parse(String) Parse(String) Parse(String)

將數字的字串表示轉換成它的對等 64 位元帶正負號的整數。Converts the string representation of a number to its 64-bit signed integer equivalent.

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 String String String

字串,包含要轉換的數字。A string containing a number to convert.

傳回

64 位元帶正負號的整數,與 s 中所包含的數字相等。A 64-bit signed integer equivalent to the number contained in s.

例外狀況

s 的格式不正確。s is not in the correct format.

s 代表小於 MinValue 或大於 MaxValue 的數字。s represents a number less than MinValue or greater than MaxValue.

範例

下列範例示範如何將字串值轉換成 64 位元帶正負號的整數值,使用Int64.Parse(String)方法。The following example demonstrates how to convert a string value into a 64-bit signed integer value using the Int64.Parse(String) method. 然後,它會顯示產生的長整數值。It then displays the resulting long integer value.

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.
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參數會包含數字的格式:The s parameter contains a number of the form:

[ws][sign]digits[ws][ws][sign]digits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。Elements in square brackets ([ and ]) are optional. 下表說明每個元素。The following table describes each element.

元素Element 描述Description
wsws 選擇性的泛空白字元。Optional white space.
Signsign 選擇性正負號。An optional sign.
digitsdigits 範圍從 0 到 9 的數字順序。A sequence of digits ranging from 0 to 9.

s參數會解譯使用NumberStyles.Integer樣式。The s parameter is interpreted using the NumberStyles.Integer style. 除了十進位數字,只允許前置和尾端空格,以及前置正負號。In addition to decimal digits, only leading and trailing spaces together with a leading sign are allowed. 若要明確地定義中可以存在的樣式項目s,使用任何一種Int64.Parse(String, NumberStyles)Int64.Parse(String, NumberStyles, IFormatProvider)方法。To explicitly define the style elements that can be present in s, use either the Int64.Parse(String, NumberStyles) or the Int64.Parse(String, NumberStyles, IFormatProvider) method.

s參數會剖析使用的格式設定資訊中NumberFormatInfo初始化目前系統文化特性的物件。The s parameter is parsed using the formatting information in a NumberFormatInfo object that is initialized for the current system culture. 若要剖析的字串時使用其他文化特性格式資訊,請使用Int64.Parse(String, NumberStyles, IFormatProvider)方法。To parse a string using the formatting information of some other culture, use the Int64.Parse(String, NumberStyles, IFormatProvider) method.

另請參閱

Parse(String, IFormatProvider) Parse(String, IFormatProvider) Parse(String, IFormatProvider) Parse(String, IFormatProvider)

將指定特定文化特性格式之數字的字串表示轉換成它的對等 64 位元帶正負號的整數。Converts the string representation of a number in a specified culture-specific format to its 64-bit signed integer equivalent.

public:
 static long Parse(System::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 String String String

字串,包含要轉換的數字。A string containing a number to convert.

provider
IFormatProvider IFormatProvider IFormatProvider IFormatProvider

物件,提供關於 s 的特定文化特性格式資訊。An object that supplies culture-specific formatting information about s.

傳回

64 位元帶正負號的整數,與 s 中所指定的數字相等。A 64-bit signed integer equivalent to the number specified in s.

例外狀況

s 的格式不正確。s is not in the correct format.

s 代表小於 MinValue 或大於 MaxValue 的數字。s represents a number less than MinValue or greater than MaxValue.

範例

下列範例是按鈕 click 事件處理常式的 Web 表單。The following example is the button click event handler of a Web form. 它會使用所傳回的陣列HttpRequest.UserLanguages屬性來判斷使用者的地區設定。It uses the array returned by the HttpRequest.UserLanguages property to determine the user's locale. 它接著會執行個體化CultureInfo對應至該地區設定物件。It then instantiates a CultureInfo object that corresponds to that locale. NumberFormatInfo所屬的物件CultureInfo物件會傳遞至Parse(String, IFormatProvider)方法,將轉換使用者輸入的目標Int64值。The NumberFormatInfo object that belongs to that CultureInfo object is then passed to the Parse(String, IFormatProvider) method to convert the user's input to an Int64 value.

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值。This overload of the Parse(String, IFormatProvider) method is typically used to convert text that can be formatted in a variety of ways to an Int64 value. 比方說,它可以用來轉換使用者 HTML 文字方塊中輸入數值的文字。For example, it can be used to convert the text entered by a user into an HTML text box to a numeric value.

s參數會包含數字的格式:The s parameter contains a number of the form:

[ws][sign]digits[ws][ws][sign]digits[ws]

在方括號 ([和]) 是選擇性的項目與其他項目如下所示。Items in square brackets ([ and ]) are optional, and other items are as follows.

wsws
選擇性的泛空白字元。An optional white space.

Signsign
選擇性正負號。An optional sign.

digitsdigits
範圍從 0 到 9 的數字順序。A sequence of digits ranging from 0 to 9.

s參數會解譯使用NumberStyles.Integer樣式。The s parameter is interpreted using the NumberStyles.Integer style. 除了十進位數字,只允許前置和尾端空格,以及前置正負號。In addition to decimal digits, only leading and trailing spaces together with a leading sign are allowed. 若要明確地定義中可以存在的樣式項目s,使用Int64.Parse(String, NumberStyles, IFormatProvider)方法。To explicitly define the style elements that can be present in s, use the Int64.Parse(String, NumberStyles, IFormatProvider) method.

provider參數是IFormatProvider實作,例如NumberFormatInfoCultureInfo物件。The provider parameter is an IFormatProvider implementation, such as a NumberFormatInfo or CultureInfo object. provider參數提供特定文化特性格式資訊sThe provider parameter supplies culture-specific information about the format of s. 如果providernull,則NumberFormatInfo會使用目前的文化特性。If provider is null, the NumberFormatInfo for the current culture is used.

另請參閱

適用於