BigInteger.Parse 方法

定義

將數字的字串表示,轉換為其相等的 BigInteger

多載

Parse(String)

將數字的字串表示,轉換為其相等的 BigInteger

Parse(ReadOnlySpan<Char>, IFormatProvider)

將字元範圍剖析為值。

Parse(String, NumberStyles)

將指定樣式中數字的字串表示轉換為其相等的 BigInteger

Parse(String, IFormatProvider)

將使用指定特定文化特性格式之數字的字串表示轉換為其相等的 BigInteger

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

將所指定唯讀字元範圍中具有所指定樣式的數字表示法轉換為其 BigInteger 對等項目。

Parse(String, NumberStyles, IFormatProvider)

將數字的字串表示 (使用指定樣式和特定文化特性的格式) 轉換為其相等的 BigInteger

Parse(String)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

將數字的字串表示,轉換為其相等的 BigInteger

public:
 static System::Numerics::BigInteger Parse(System::String ^ value);
public static System.Numerics.BigInteger Parse (string value);
static member Parse : string -> System.Numerics.BigInteger
Public Shared Function Parse (value As String) As BigInteger

參數

value
String

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

傳回

值,相當於以 value 參數指定的數字。

例外狀況

valuenull

value 的格式不正確。

範例

下列範例會 Parse(String) 使用 方法來具現化兩 BigInteger 個 物件。 它會將每個物件乘以另一個數位,然後呼叫 Compare 方法來判斷兩個值之間的關聯性。

string stringToParse = String.Empty;
try
{
   // Parse two strings.
   string string1, string2;
   string1 = "12347534159895123";
   string2 = "987654321357159852";
   stringToParse = string1;
   BigInteger number1 = BigInteger.Parse(stringToParse);
   Console.WriteLine("Converted '{0}' to {1:N0}.", stringToParse, number1);
   stringToParse = string2;
   BigInteger number2 = BigInteger.Parse(stringToParse);
   Console.WriteLine("Converted '{0}' to {1:N0}.", stringToParse, number2);
   // Perform arithmetic operations on the two numbers.
   number1 *= 3;
   number2 *= 2;
   // Compare the numbers.
   int result = BigInteger.Compare(number1, number2);
   switch (result)
   {
      case -1:
         Console.WriteLine("{0} is greater than {1}.", number2, number1);
         break;
      case 0:
         Console.WriteLine("{0} is equal to {1}.", number1, number2);
         break;
      case 1:
         Console.WriteLine("{0} is greater than {1}.", number1, number2);
         break;
   }
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse {0}.", stringToParse);
}
// The example displays the following output:
//    Converted '12347534159895123' to 12,347,534,159,895,123.
//    Converted '987654321357159852' to 987,654,321,357,159,852.
//    1975308642714319704 is greater than 37042602479685369.
Dim stringToParse As String = String.Empty
Try
   ' Parse two strings.
   Dim string1, string2 As String
   string1 = "12347534159895123"
   string2 = "987654321357159852"
   stringToParse = string1
   Dim number1 As BigInteger = BigInteger.Parse(stringToParse)
   Console.WriteLine("Converted '{0}' to {1:N0}.", stringToParse, number1)
   stringToParse = string2
   Dim number2 As BigInteger = BigInteger.Parse(stringToParse)
   Console.WriteLine("Converted '{0}' to {1:N0}.", stringToParse, number2)
   ' Perform arithmetic operations on the two numbers.
   number1 *= 3
   number2 *= 2
   ' Compare the numbers.
   Select Case BigInteger.Compare(number1, number2)
      Case -1
         Console.WriteLine("{0} is greater than {1}.", number2, number1)
      Case 0
         Console.WriteLine("{0} is equal to {1}.", number1, number2)
      Case 1
         Console.WriteLine("{0} is greater than {1}.", number1, number2)
   End Select      
Catch e As FormatException
   Console.WriteLine("Unable to parse {0}.", stringToParse)
End Try
' The example displays the following output:
'    Converted '12347534159895123' to 12,347,534,159,895,123.
'    Converted '987654321357159852' to 987,654,321,357,159,852.
'    1975308642714319704 is greater than 37042602479685369.

備註

參數 value 應該是以下列形式表示數位的字串。

[ws][sign]digits[ws]

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

元素 描述
ws 選擇性空白字元。
簽署 選擇性符號。 有效的符號字元取決於 NumberFormatInfo.NegativeSign 目前文化特性的 和 NumberFormatInfo.PositiveSign 屬性。
數字 介於 0 到 9 的數位序列。 忽略任何前置零。

注意

參數指定的 value 字串會使用 NumberStyles.Integer 樣式來解譯。 它不能包含任何群組分隔符號或小數分隔符號,而且不能有小數部分。

參數 value 是使用物件中 System.Globalization.NumberFormatInfo 針對目前系統文化特性初始化的格式資訊進行剖析。 如需詳細資訊,請參閱NumberFormatInfo.CurrentInfo。 若要使用特定文化特性的格式資訊剖析字串,請使用 Parse(String, IFormatProvider) 方法。

重要

如果您使用 Parse 方法來回傳方法所輸出 ToString 值的字串表示 BigInteger 法,則應該使用 BigInteger.ToString(String) 方法搭配 「R」 格式規範來產生值的字串表示 BigInteger 。 否則,的字串表示 BigInteger 只會保留原始值的 50 個最大有效位數,而且當您使用 Parse 方法來還原 BigInteger 值時,可能會遺失資料。

另請參閱

適用於

Parse(ReadOnlySpan<Char>, IFormatProvider)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

將字元範圍剖析為值。

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

參數

s
ReadOnlySpan<Char>

要剖析的字元範圍。

provider
IFormatProvider

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

傳回

剖析 s 的結果。

實作

適用於

Parse(String, NumberStyles)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

將指定樣式中數字的字串表示轉換為其相等的 BigInteger

public:
 static System::Numerics::BigInteger Parse(System::String ^ value, System::Globalization::NumberStyles style);
public static System.Numerics.BigInteger Parse (string value, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> System.Numerics.BigInteger
Public Shared Function Parse (value As String, style As NumberStyles) As BigInteger

參數

value
String

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

style
NumberStyles

指定 value 可以使用的格式之列舉值的位元組合。

傳回

值,相當於以 value 參數指定的數字。

例外狀況

style 不是 NumberStyles 值。

-或-

style 包含 AllowHexSpecifierHexNumber 旗標和其他值。

valuenull

value 不符合由 NumberStyles 所指定的輸入模式。

範例

下列範例說明對 方法的 Parse(String, NumberStyles) 呼叫,其中包含參數的數個可能值 style 。 其說明如何將字串解譯為十六進位值,以及如何禁止空格和符號。

BigInteger number;
// Method should succeed (white space and sign allowed)
number = BigInteger.Parse("   -68054   ", NumberStyles.Integer);
Console.WriteLine(number);
// Method should succeed (string interpreted as hexadecimal)
number = BigInteger.Parse("68054", NumberStyles.AllowHexSpecifier);
Console.WriteLine(number);
// Method call should fail: sign not allowed
try
{
   number = BigInteger.Parse("   -68054  ", NumberStyles.AllowLeadingWhite
                                            | NumberStyles.AllowTrailingWhite);
   Console.WriteLine(number);
}
catch (FormatException e)
{
   Console.WriteLine(e.Message);
}
// Method call should fail: white space not allowed
try
{
   number = BigInteger.Parse("   68054  ", NumberStyles.AllowLeadingSign);
   Console.WriteLine(number);
}
catch (FormatException e)
{
   Console.WriteLine(e.Message);
}
//
// The method produces the following output:
//
//     -68054
//     426068
//     Input string was not in a correct format.
//     Input string was not in a correct format.
Dim number As BigInteger 
' Method should succeed (white space and sign allowed)
number = BigInteger.Parse("   -68054   ", NumberStyles.Integer)
Console.WriteLine(number)
' Method should succeed (string interpreted as hexadecimal)
number = BigInteger.Parse("68054", NumberStyles.AllowHexSpecifier)
Console.WriteLine(number)
' Method call should fail: sign not allowed
Try
   number = BigInteger.Parse("   -68054  ", NumberStyles.AllowLeadingWhite _
                                            Or NumberStyles.AllowTrailingWhite)
   Console.WriteLine(number)
Catch e As FormatException
   Console.WriteLine(e.Message)
End Try                                                     
' Method call should fail: white space not allowed
Try
   number = BigInteger.Parse("   68054  ", NumberStyles.AllowLeadingSign)
   Console.WriteLine(number)
Catch e As FormatException
   Console.WriteLine(e.Message)
End Try    
'
' The method produces the following output:
'
'     -68054
'     426068
'     Input string was not in a correct format.
'     Input string was not in a correct format.

備註

參數 style 會定義樣式專案 (,例如空白字元、正負號符號、群組分隔符號符號,或參數中 value 允許的小數點符號) ,讓剖析作業成功。 styles 必須是列舉中的 NumberStyles 位旗標組合。 style當包含十六進位值的字串表示、當數字系統 (十進位或十六進位) value 只有在執行時間才知道時,或當您想要不允許空白字元或中的 value 符號時,參數會使這個方法多載很有用 value

根據 的值 stylevalue 參數可能包含下列元素:

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

如果 style 包含 NumberStyles.AllowHexSpecifier ,參數 value 可能包含下列元素:

[ws]hexdigits[ws]

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

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

fractional_digits

exponential_digits
從 0 到 9 的數位序列。 對於 fractional_digits,只有數位 0 有效。
, 特定文化特性的群組分隔符號符號。 如果 style 包含 NumberStyles.AllowThousands 旗標,則目前文化特性的群組分隔符號可以出現在 中 value
. 特定文化特性的小數點符號。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則目前文化特性的小數點符號可以出現在 中 value 。 只有數位 0 可以顯示為小數位數,剖析作業才會成功;如果 fractional_digits 包含任何其他數位, FormatException 則會擲回 。
E 「e」 或 「E」 字元,表示值是以指數 (科學) 標記法表示。 如果 style 包含 旗標,參數 value 可以表示指數標記法的數位 NumberStyles.AllowExponent
hexdigits 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

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

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

NumberStyles 除了數位之外允許 value 的專案
None 僅限 digits 元素。
AllowDecimalPoint 小數點 (.) 和 小數位數元素
AllowExponent 「e」 或 「E」 字元,表示指數標記法,以及 exponential_digits
AllowLeadingWhite 開頭的 valuews元素。
AllowTrailingWhite 結尾處的 valuews元素。
AllowLeadingSign 開頭的 valuesign元素。
AllowTrailingSign 結尾處的 value符號專案。
AllowParentheses 以括弧括住數值形式的 sign 元素。
AllowThousands 群組分隔符號 (,) 專案。
AllowCurrencySymbol 貨幣 ($) 元素。
Currency 所有元素。 不過, value 不能代表十六進位數或指數標記法中的數位。
Float 開頭或結尾的 valuews元素,會在 開頭加上符號value ,小數點 () 符號。 參數 value 也可以使用指數標記法。
Number 、、群組分隔符號 () 和小數點 () 元素。 signws
Any 所有元素。 不過, value 不能代表十六進位數。

重要

如果您使用 Parse 方法來回傳方法所輸出 ToString 值的字串表示 BigInteger 法,則應該使用 BigInteger.ToString(String) 方法搭配 「R」 格式規範來產生值的字串表示 BigInteger 。 否則,的字串表示 BigInteger 只會保留原始值的 50 個最大有效位數,而且當您使用 Parse 方法來還原 BigInteger 值時,可能會遺失資料。

不同于允許但不需要的其他 NumberStyles 值,在 中 value 存在特定樣式元素,樣式 NumberStyles.AllowHexSpecifier 值表示 中的 value 個別數值字元一律會解譯為十六進位字元。 有效的十六進位字元為 0-9、A-F 和 a-f。 唯一可以與 style 參數結合的其他旗標為 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite 。 (列舉 NumberStyles 包含複合數位樣式, HexNumber 其中包含空白字元旗標.)

注意

如果 value 是十六進位數位的字串標記法,則不能在前面加上任何裝飾 (,例如 0x&h) ,將它區分為十六進位數。 這會導致轉換失敗。

如果 value 是十六進位字串, Parse(String, NumberStyles) 則如果方法的前兩個十六進位數位大於或等於 0x80 ,則方法會使用兩個補數標記法來解譯 value 為負數。 換句話說,方法會將 中 value 第一個位元組的最高順序位解譯為符號位。 為了確保十六進位字串正確解譯為正數,中的 value 第一個數位必須有零值。 例如,方法會 0x80 解譯為負值,但它會將 0x0800x0080 解譯為正值。 下列範例說明十六進位字串之間的差異,這些字串代表負值和正值。

using System;
using System.Globalization;
using System.Numerics;

public class Example
{
   public static void Main()
   {
      string[] hexStrings = { "80", "E293", "F9A2FF", "FFFFFFFF",
                              "080", "0E293", "0F9A2FF", "0FFFFFFFF",
                              "0080", "00E293", "00F9A2FF", "00FFFFFFFF" };
      foreach (string hexString in hexStrings)
      {
         BigInteger number = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier);
         Console.WriteLine("Converted 0x{0} to {1}.", hexString, number);
      }
   }
}
// The example displays the following output:
//       Converted 0x80 to -128.
//       Converted 0xE293 to -7533.
//       Converted 0xF9A2FF to -417025.
//       Converted 0xFFFFFFFF to -1.
//       Converted 0x080 to 128.
//       Converted 0x0E293 to 58003.
//       Converted 0x0F9A2FF to 16360191.
//       Converted 0x0FFFFFFFF to 4294967295.
//       Converted 0x0080 to 128.
//       Converted 0x00E293 to 58003.
//       Converted 0x00F9A2FF to 16360191.
//       Converted 0x00FFFFFFFF to 4294967295.
Imports System.Globalization
Imports System.Numerics

Module Example
   Public Sub Main()
      Dim hexStrings() As String = { "80", "E293", "F9A2FF", "FFFFFFFF", 
                                     "080", "0E293", "0F9A2FF", "0FFFFFFFF",  
                                     "0080", "00E293", "00F9A2FF", "00FFFFFFFF" }
      For Each hexString As String In hexStrings
         Dim number As BigInteger = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier)
         Console.WriteLine("Converted 0x{0} to {1}.", hexString, number)
      Next         
   End Sub
End Module
' The example displays the following output:
'       Converted 0x80 to -128.
'       Converted 0xE293 to -7533.
'       Converted 0xF9A2FF to -417025.
'       Converted 0xFFFFFFFF to -1.
'       Converted 0x080 to 128.
'       Converted 0x0E293 to 58003.
'       Converted 0x0F9A2FF to 16360191.
'       Converted 0x0FFFFFFFF to 4294967295.
'       Converted 0x0080 to 128.
'       Converted 0x00E293 to 58003.
'       Converted 0x00F9A2FF to 16360191.
'       Converted 0x00FFFFFFFF to 4294967295.

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

另請參閱

適用於

Parse(String, IFormatProvider)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

將使用指定特定文化特性格式之數字的字串表示轉換為其相等的 BigInteger

public:
 static System::Numerics::BigInteger Parse(System::String ^ value, IFormatProvider ^ provider);
public:
 static System::Numerics::BigInteger Parse(System::String ^ value, IFormatProvider ^ provider) = IParsable<System::Numerics::BigInteger>::Parse;
public static System.Numerics.BigInteger Parse (string value, IFormatProvider provider);
public static System.Numerics.BigInteger Parse (string value, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> System.Numerics.BigInteger
Public Shared Function Parse (value As String, provider As IFormatProvider) As BigInteger

參數

value
String

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

provider
IFormatProvider

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

傳回

值,相當於以 value 參數指定的數字。

實作

例外狀況

valuenull

value 的格式不正確。

範例

下列範例示範兩種方式,將磚 (~) 定義為格式化 BigInteger 值的負號。 請注意,若要以與原始字串相同的格式顯示 BigInteger 值,您的程式碼必須呼叫 BigInteger.ToString(IFormatProvider) 方法,並傳遞 NumberFormatInfo 提供格式資訊的物件。

第一個範例會定義實作 IFormatProvider 的類別,並使用 GetFormat 方法來傳回 NumberFormatInfo 提供格式化資訊的物件。

public class BigIntegerFormatProvider : IFormatProvider
{
   public object GetFormat(Type formatType)
   {
      if (formatType == typeof(NumberFormatInfo))
      {
         NumberFormatInfo numberFormat = new NumberFormatInfo();
         numberFormat.NegativeSign = "~";
         return numberFormat;
      }
      else
      {
         return null;
      }
   }
}
Public Class BigIntegerFormatProvider : Implements IFormatProvider
   Public Function GetFormat(formatType As Type) As Object _
                            Implements IFormatProvider.GetFormat
      If formatType Is GetType(NumberFormatInfo) Then
         Dim numberFormat As New NumberFormatInfo
         numberFormat.NegativeSign = "~"
         Return numberFormat
      Else
         Return Nothing
      End If      
   End Function
End Class

BigInteger然後可以使用下列程式碼具現化 物件:

BigInteger number = BigInteger.Parse("~6354129876", new BigIntegerFormatProvider());
// Display value using same formatting information
Console.WriteLine(number.ToString(new BigIntegerFormatProvider()));
// Display value using formatting of current culture
Console.WriteLine(number);
Dim number As BigInteger = BigInteger.Parse("~6354129876", New BigIntegerFormatProvider)
' Display value using same formatting information
Console.WriteLine(number.ToString(New BigIntegerFormatProvider))
' Display value using formatting of current culture
Console.WriteLine(number)

第二個範例更為簡單。 它會將提供格式資訊的物件傳遞 NumberFormatInfoprovider 參數。

NumberFormatInfo fmt = new NumberFormatInfo();
fmt.NegativeSign = "~";

BigInteger number = BigInteger.Parse("~6354129876", fmt);
// Display value using same formatting information
Console.WriteLine(number.ToString(fmt));
// Display value using formatting of current culture
Console.WriteLine(number);
Dim fmt As New NumberFormatInfo()
fmt.NegativeSign = "~"

Dim number As BigInteger = BigInteger.Parse("~6354129876", fmt)
' Display value using same formatting information
Console.WriteLine(number.ToString(fmt))
' Display value using formatting of current culture
Console.WriteLine(number)

備註

參數 value 應該是數位的字串表示,格式如下:

[ws][sign]digits[ws]

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

元素 描述
ws 選擇性空白字元。
簽署 選擇性符號。 有效的符號字元是由 NumberFormatInfo.NegativeSign 物件的 方法所 provider 傳回之 物件的 GetFormatNumberFormatInfo.PositiveSign 屬性 NumberFormatInfo 所決定。
數字 介於 0 到 9 的數位序列。 忽略任何前置零。

注意

參數指定的 value 字串會使用 NumberStyles.Integer 樣式來解譯。 它不能包含任何群組分隔符號或小數分隔符號,而且不能有小數部分。

重要

如果您使用 Parse 方法來回傳方法所輸出 ToString 值的字串表示 BigInteger 法,則應該使用 BigInteger.ToString(String) 方法搭配 「R」 格式規範來產生值的字串表示 BigInteger 。 否則,的字串表示 BigInteger 只會保留原始值的 50 個最大有效位數,而且當您使用 Parse 方法來還原 BigInteger 值時,可能會遺失資料。

參數 provider 是實作 IFormatProvider ,其 GetFormat 方法會 NumberFormatInfo 傳回提供特定文化特性格式資訊的 物件。 Parse(String, IFormatProvider)叫用 方法時,它會呼叫 provider 參數的 方法,並傳遞 Type 代表 NumberFormatInfo 型別的 GetFormat 物件。 GetFormat然後,方法會 NumberFormatInfo 傳回 物件,提供參數格式 value 的相關資訊。 有三種方式可以使用 provider 參數,將自訂格式資訊提供給剖析作業:

如果 providernull ,則會 value 根據 NumberFormatInfo 目前文化特性的物件來解譯 的格式。

另請參閱

適用於

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

將所指定唯讀字元範圍中具有所指定樣式的數字表示法轉換為其 BigInteger 對等項目。

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

參數

value
ReadOnlySpan<Char>

包含要轉換之數字的唯讀字元範圍。

style
NumberStyles

指定 value 可以使用的格式之列舉值的位元組合。

provider
IFormatProvider

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

傳回

值,相當於以 value 參數指定的數字。

實作

例外狀況

style 不是 NumberStyles 值。

-或-

style 包含 AllowHexSpecifierHexNumber 旗標和其他值。

valuenull

value 不符合由 style 所指定的輸入模式。

備註

參數 style 會定義樣式專案 (,例如空白字元、正負號符號、群組分隔符號符號,或參數中 value 允許的小數點符號) ,讓剖析作業成功。 styles 必須是列舉中的 NumberStyles 位旗標組合。 如果 style 包含十六進位值的標記法、當數字系統 (十進位或十六進位) value 只有在執行時間才知道,或您想要不允許中的 value 空白字元或登入符號時,參數會使這個方法多載很有用 value

根據 的值 stylevalue 參數可能包含下列元素:

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

如果 style 包含 NumberStyles.AllowHexSpecifier ,參數 value 可能包含下列元素:

[ws]hexdigits[ws]

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

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

fractional_digits

exponential_digits
從 0 到 9 的數位序列。 對於 fractional_digits,只有數位 0 有效。
, 特定文化特性的群組分隔符號符號。 如果 style 包含 NumberStyles.AllowThousands 旗標,則 所 provider 指定文化特性的群組分隔符號可以出現在 value 中。
. 特定文化特性的小數點符號。 如果包含 旗標,則所 provider 指定文化特性的小數點符號可以出現在 中 valueNumberStyles.AllowDecimalPointstyle 只有數位 0 可以顯示為小數位數,剖析作業才會成功;如果 fractional_digits 包含任何其他數位, FormatException 則會擲回 。
E 「e」 或 「E」 字元,表示值是以指數 (科學) 標記法表示。 如果 style 包含 旗標,參數 value 可以表示指數標記法的數位 NumberStyles.AllowExponent
hexdigits 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

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

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

NumberStyles 值 除了數位之外,值中允許的專案
None 僅限 digits 元素。
AllowDecimalPoint 小數點 (.) 和 小數位數元素
AllowExponent 表示指數標記法的 「e」 或 「E」 字元。 以及 exponential_digits
AllowLeadingWhite 開頭的 valuews元素。
AllowTrailingWhite 結尾處的 valuews元素。
AllowLeadingSign 開頭的 valuesign元素。
AllowTrailingSign 結尾處的 value符號專案。
AllowParentheses 以括弧括住數值形式的 sign 元素。
AllowThousands 群組分隔符號 (,) 專案。
AllowCurrencySymbol 貨幣 ($) 元素。
Currency 所有元素。 不過, value 不能代表十六進位數或指數標記法中的數位。
Float 開頭或結尾處的 valuews元素,會在 開頭 value加上小數點 (.) 符號。 參數 value 也可以使用指數標記法。
Number wssign 、群組分隔符號 (、) 和小數點 (.) 元素。
Any 所有元素。 不過, value 不能代表十六進位數。

與其他允許但不需要中 value 特定樣式元素存在的值 NumberStyles 不同,樣式 NumberStyles.AllowHexSpecifier 值表示 中的 value 個別數值字元一律會解譯為十六進位字元。 有效的十六進位字元為 0-9、A-F 和 a-f。 唯一可以與 style 參數結合的其他旗標為 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite 。 (列舉 NumberStyles 包含複合數位樣式, HexNumber 其中包含空白字元旗標.)

參數 provider 是實作 IFormatProvider 。 其 GetFormat 方法會 NumberFormatInfo 傳回 物件,這個物件提供 有關 格式 value 的文化特性特定資訊。 一般而言, provider 可以是下列任一項:

如果 providernull ,則會 NumberFormatInfo 使用目前文化特性的 物件。

另請參閱

適用於

Parse(String, NumberStyles, IFormatProvider)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

將數字的字串表示 (使用指定樣式和特定文化特性的格式) 轉換為其相等的 BigInteger

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

參數

value
String

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

style
NumberStyles

指定 value 可以使用的格式之列舉值的位元組合。

provider
IFormatProvider

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

傳回

值,相當於以 value 參數指定的數字。

實作

例外狀況

style 不是 NumberStyles 值。

-或-

style 包含 AllowHexSpecifierHexNumber 旗標和其他值。

valuenull

value 不符合由 style 所指定的輸入模式。

範例

下列範例會使用 和 provider 參數的各種值 style 組合,對 Parse(String, NumberStyles, IFormatProvider) 方法進行數次呼叫。

// Call parse with default values of style and provider
Console.WriteLine(BigInteger.Parse("  -300   ",
                  NumberStyles.Integer, CultureInfo.CurrentCulture));
// Call parse with default values of style and provider supporting tilde as negative sign
Console.WriteLine(BigInteger.Parse("   ~300  ",
                                   NumberStyles.Integer, new BigIntegerFormatProvider()));
// Call parse with only AllowLeadingWhite and AllowTrailingWhite
// Exception thrown because of presence of negative sign
try
{
   Console.WriteLine(BigInteger.Parse("    ~300   ",
                                NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite,
                                new BigIntegerFormatProvider()));
}
catch (FormatException e)
{
   Console.WriteLine("{0}: \n   {1}", e.GetType().Name, e.Message);
}
// Call parse with only AllowHexSpecifier
// Exception thrown because of presence of negative sign
try
{
   Console.WriteLine(BigInteger.Parse("-3af", NumberStyles.AllowHexSpecifier,
                                      new BigIntegerFormatProvider()));
}
catch (FormatException e)
{
   Console.WriteLine("{0}: \n   {1}", e.GetType().Name, e.Message);
}
// Call parse with only NumberStyles.None
// Exception thrown because of presence of white space and sign
try
{
   Console.WriteLine(BigInteger.Parse(" -300 ", NumberStyles.None,
                                      new BigIntegerFormatProvider()));
}
catch (FormatException e)
{
   Console.WriteLine("{0}: \n   {1}", e.GetType().Name, e.Message);
}
// The example displays the followingoutput:
//       -300
//       -300
//       FormatException:
//          The value could not be parsed.
//       FormatException:
//          The value could not be parsed.
//       FormatException:
//          The value could not be parsed.
' Call parse with default values of style and provider
Console.WriteLine(BigInteger.Parse("  -300   ", _
                  NumberStyles.Integer, CultureInfo.CurrentCulture))
' Call parse with default values of style and provider supporting tilde as negative sign
Console.WriteLine(BigInteger.Parse("   ~300  ", _
                                   NumberStyles.Integer, New BigIntegerFormatProvider()))
' Call parse with only AllowLeadingWhite and AllowTrailingWhite
' Exception thrown because of presence of negative sign
Try
   Console.WriteLIne(BigInteger.Parse("    ~300   ", _
                                      NumberStyles.AllowLeadingWhite Or NumberStyles.AllowTrailingWhite, _
                                      New BigIntegerFormatProvider()))
Catch e As FormatException
   Console.WriteLine("{0}: {1}   {2}", e.GetType().Name, vbCrLf, e.Message)
End Try                                   
' Call parse with only AllowHexSpecifier
' Exception thrown because of presence of negative sign
Try
   Console.WriteLIne(BigInteger.Parse("-3af", NumberStyles.AllowHexSpecifier, _
                                      New BigIntegerFormatProvider()))
Catch e As FormatException
   Console.WriteLine("{0}: {1}   {2}", e.GetType().Name, vbCrLf, e.Message)
End Try                                 
' Call parse with only NumberStyles.None
' Exception thrown because of presence of white space and sign
Try
   Console.WriteLIne(BigInteger.Parse(" -300 ", NumberStyles.None, _
                                      New BigIntegerFormatProvider()))
Catch e As FormatException
   Console.WriteLine("{0}: {1}   {2}", e.GetType().Name, vbCrLf, e.Message)
End Try
' The example displays the following output:
'       -300
'       -300
'       FormatException:
'          The value could not be parsed.
'       FormatException:
'          The value could not be parsed.
'       FormatException:
'          The value could not be parsed.

方法的一些個別呼叫 Parse(String, NumberStyles, IFormatProvider) 會傳遞下列 BigIntegerFormatProvider 類別的實例,它會將波浪 (~) 定義為負號。

public class BigIntegerFormatProvider : IFormatProvider
{
   public object GetFormat(Type formatType)
   {
      if (formatType == typeof(NumberFormatInfo))
      {
         NumberFormatInfo numberFormat = new NumberFormatInfo();
         numberFormat.NegativeSign = "~";
         return numberFormat;
      }
      else
      {
         return null;
      }
   }
}
Public Class BigIntegerFormatProvider : Implements IFormatProvider
   Public Function GetFormat(formatType As Type) As Object _
                            Implements IFormatProvider.GetFormat
      If formatType Is GetType(NumberFormatInfo) Then
         Dim numberFormat As New NumberFormatInfo
         numberFormat.NegativeSign = "~"
         Return numberFormat
      Else
         Return Nothing
      End If      
   End Function
End Class

備註

參數 style 會定義樣式專案 (,例如空白字元、正負號符號、群組分隔符號符號,或參數中 value 允許的小數點符號) ,讓剖析作業成功。 styles 必須是列舉中的 NumberStyles 位旗標組合。 style當包含十六進位值的字串表示、當數字系統 (十進位或十六進位) value 只有在執行時間才知道時,或當您想要不允許空白字元或中的 value 符號時,參數會使這個方法多載很有用 value

根據 的值 stylevalue 參數可能包含下列元素:

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

如果 style 包含 NumberStyles.AllowHexSpecifier ,參數 value 可能包含下列元素:

[ws]hexdigits[ws]

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

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

fractional_digits

exponential_digits
從 0 到 9 的數位序列。 對於 fractional_digits,只有數位 0 有效。
, 特定文化特性的群組分隔符號符號。 如果包含 旗標,則 所 provider 指定文化特性的群組分隔符號符號可以出現在 中 valueNumberStyles.AllowThousandsstyle
. 特定文化特性的小數點符號。 如果包含 旗標,則 所指定 provider 文化特性的小數點符號可能會出現在 中 valueNumberStyles.AllowDecimalPointstyle 只有數位 0 可以顯示為小數位數,剖析作業才會成功;如果 fractional_digits 包含任何其他數位, FormatException 則會擲回 。
E 「e」 或 「E」 字元,表示該值是以指數 (科學) 標記法表示。 如果 style 包含 NumberStyles.AllowExponent 旗標,參數 value 可以代表指數標記法的數位。
hexdigits 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

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

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

NumberStyles 值 除了數位之外,值中允許的專案
None 僅限 digits 元素。
AllowDecimalPoint 小數點 (.) 和 小數位數元素
AllowExponent 表示指數標記法的 「e」 或 「E」 字元。 以及 exponential_digits
AllowLeadingWhite 開頭的 valuews元素。
AllowTrailingWhite 結尾處的 valuews專案。
AllowLeadingSign 開頭的 value符號專案。
AllowTrailingSign 結尾的 value符號專案。
AllowParentheses 以括弧括住數值形式的 sign 元素。
AllowThousands 群組分隔符號 (,) 元素。
AllowCurrencySymbol 貨幣 ($) 專案。
Currency 所有元素。 不過, value 不能以指數標記法表示十六進位數位或數位。
Float 開頭或結尾的 valuews元素,在 開頭 value為 符號,而小數點 (.) 符號。 參數 value 也可以使用指數標記法。
Number wssign 、群組分隔符號 (、) 和小數點 (.) 專案。
Any 所有元素。 不過, value 不能代表十六進位數位。

重要

如果您使用 Parse 方法來回傳回方法所輸出 ToString 值的字串表示 BigInteger ,您應該使用 BigInteger.ToString(String) 方法搭配 「R」 格式規範來產生值的字串表示 BigInteger 。 否則,的 BigInteger 字串表示只會保留原始值的 50 個最大有效位數,而且當您使用 Parse 方法來還原 BigInteger 值時,可能會遺失資料。

NumberStyles與其他允許但不需要在 中 valueNumberStyles.AllowHexSpecifier 存在特定樣式專案的值不同,樣式值表示 中的 value 個別數值字元一律會解譯為十六進位字元。 有效的十六進位字元為 0-9、A-F 和 a-f。 唯一可以與 style 參數結合的其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite 。 (列舉 NumberStyles 包含複合編號樣式 , HexNumber 其中包含空白字元旗標.)

注意

如果 value 是十六進位數位的字串標記法,則不能在前面加上任何裝飾 (,例如 0x&h) 將它區分為十六進位數位。 這會導致轉換失敗。

如果 value 是十六進位字串, Parse(String, NumberStyles) 則如果前兩個十六進位數位大於或等於 0x80 ,則方法會解譯 value 為使用兩個補數表示所儲存的負數。 換句話說,方法會將 中 value 第一個位元組的最高順序位解譯為符號位。 為了確保十六進位字串正確解譯為正數,中的 value 第一個數位必須有零的值。 例如,方法會解譯 0x80 為負值,但會將 或 0x0080 解譯 0x080 為正值。 下列範例說明代表負值和正值的十六進位字串之間的差異。

using System;
using System.Globalization;
using System.Numerics;

public class Example
{
   public static void Main()
   {
      string[] hexStrings = { "80", "E293", "F9A2FF", "FFFFFFFF",
                              "080", "0E293", "0F9A2FF", "0FFFFFFFF",
                              "0080", "00E293", "00F9A2FF", "00FFFFFFFF" };
      foreach (string hexString in hexStrings)
      {
         BigInteger number = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier);
         Console.WriteLine("Converted 0x{0} to {1}.", hexString, number);
      }
   }
}
// The example displays the following output:
//       Converted 0x80 to -128.
//       Converted 0xE293 to -7533.
//       Converted 0xF9A2FF to -417025.
//       Converted 0xFFFFFFFF to -1.
//       Converted 0x080 to 128.
//       Converted 0x0E293 to 58003.
//       Converted 0x0F9A2FF to 16360191.
//       Converted 0x0FFFFFFFF to 4294967295.
//       Converted 0x0080 to 128.
//       Converted 0x00E293 to 58003.
//       Converted 0x00F9A2FF to 16360191.
//       Converted 0x00FFFFFFFF to 4294967295.
Imports System.Globalization
Imports System.Numerics

Module Example
   Public Sub Main()
      Dim hexStrings() As String = { "80", "E293", "F9A2FF", "FFFFFFFF", 
                                     "080", "0E293", "0F9A2FF", "0FFFFFFFF",  
                                     "0080", "00E293", "00F9A2FF", "00FFFFFFFF" }
      For Each hexString As String In hexStrings
         Dim number As BigInteger = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier)
         Console.WriteLine("Converted 0x{0} to {1}.", hexString, number)
      Next         
   End Sub
End Module
' The example displays the following output:
'       Converted 0x80 to -128.
'       Converted 0xE293 to -7533.
'       Converted 0xF9A2FF to -417025.
'       Converted 0xFFFFFFFF to -1.
'       Converted 0x080 to 128.
'       Converted 0x0E293 to 58003.
'       Converted 0x0F9A2FF to 16360191.
'       Converted 0x0FFFFFFFF to 4294967295.
'       Converted 0x0080 to 128.
'       Converted 0x00E293 to 58003.
'       Converted 0x00F9A2FF to 16360191.
'       Converted 0x00FFFFFFFF to 4294967295.

參數 provider 是實作 IFormatProvider 。 其 GetFormat 方法會傳 NumberFormatInfo 回 物件,提供 有關 格式 value 的文化特性特定資訊。 一般而言, provider 可以是下列任一項:

如果 為 providernull ,則會 NumberFormatInfo 使用目前文化特性的物件。

另請參閱

適用於