BigInteger.ToString 方法

定義

將目前 BigInteger 物件的數值,轉換為其相等的字串表示。

多載

ToString(String, IFormatProvider)

使用指定的格式和特定文化特性的格式資訊,將目前 BigInteger 物件的數值,轉換為其相等的字串表示。

ToString(String)

使用指定的格式,將目前 BigInteger 物件的值,轉換為其相等字串表示。

ToString(IFormatProvider)

使用指定的文化特性特定格式資訊,將目前 BigInteger 物件的數值轉換為其對等字串表示。

ToString()

將目前 BigInteger 物件的數值,轉換為其相等的字串表示。

ToString(String, IFormatProvider)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

使用指定的格式和特定文化特性的格式資訊,將目前 BigInteger 物件的數值,轉換為其相等的字串表示。

public:
 virtual System::String ^ ToString(System::String ^ format, IFormatProvider ^ provider);
public string ToString (string format, IFormatProvider provider);
public string ToString (string? format, IFormatProvider? provider);
override this.ToString : string * IFormatProvider -> string
Public Function ToString (format As String, provider As IFormatProvider) As String

參數

format
String

標準或自訂數值格式字串。

provider
IFormatProvider

物件,提供特定文化特性格式資訊。

傳回

目前 BigInteger 值的字串表示 (如 formatprovider 參數所指定)。

實作

例外狀況

format 不是有效的格式字串。

範例

下列範例會 BigInteger 初始化 值,並使用標準格式字串和 NumberFormatInfo 物件將它顯示為負號,將波浪 (~) 定義為負號。

// Redefine the negative sign as the tilde for the invariant culture.
NumberFormatInfo bigIntegerFormatter = new NumberFormatInfo();
bigIntegerFormatter.NegativeSign = "~";

BigInteger value = BigInteger.Parse("-903145792771643190182");
string[] specifiers = { "C", "D", "D25", "E", "E4", "e8", "F0",
                        "G", "N0", "P", "R", "X", "0,0.000",
                        "#,#.00#;(#,#.00#)" };

foreach (string specifier in specifiers)
   Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier,
                     bigIntegerFormatter));

// The example displays the following output:
//    C: (☼903,145,792,771,643,190,182.00)
//    D: ~903145792771643190182
//    D25: ~0000903145792771643190182
//    E: ~9.031457E+020
//    E4: ~9.0314E+020
//    e8: ~9.03145792e+020
//    F0: ~903145792771643190182
//    G: ~903145792771643190182
//    N0: ~903,145,792,771,643,190,182
//    P: ~90,314,579,277,164,319,018,200.00 %
//    R: ~903145792771643190182
//    X: CF0A55968BB1A7545A
//    0,0.000: ~903,145,792,771,643,190,182.000
//    #,#.00#;(#,#.00#): (903,145,792,771,643,190,182.00)
' Redefine the negative sign as the tilde for the invariant culture.
Dim bigIntegerFormatter As New NumberFormatInfo()
bigIntegerFormatter.NegativeSign = "~"

Dim value As BigInteger = BigInteger.Parse("-903145792771643190182")
Dim specifiers() As String = { "C", "D", "D25", "E", "E4", "e8", "F0", 
                               "G", "N0", "P", "R", "X", "0,0.000", 
                               "#,#.00#;(#,#.00#)" }

For Each specifier As String In specifiers
   Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier, 
                     bigIntegerformatter))
Next   
' The example displays the following output:
'    C: (☼903,145,792,771,643,190,182.00)
'    D: ~903145792771643190182
'    D25: ~0000903145792771643190182
'    E: ~9.031457E+020
'    E4: ~9.0314E+020
'    e8: ~9.03145792e+020
'    F0: ~903145792771643190182
'    G: ~903145792771643190182
'    N0: ~903,145,792,771,643,190,182
'    P: ~90,314,579,277,164,319,018,200.00 %
'    R: ~903145792771643190182
'    X: CF0A55968BB1A7545A
'    0,0.000: ~903,145,792,771,643,190,182.000
'    #,#.00#;(#,#.00#): (903,145,792,771,643,190,182.00)

備註

方法 ToString(String, IFormatProvider) 會使用 NumberFormatInfo 指定文化特性的 物件,以指定格式格式化 BigInteger 值。 如果您想要使用來回格式或預設文化特性設定,請使用 方法的其他多載 ToString ,如下所示:

使用格式 針對文化特性 使用多載
來回 (「R」) 格式 預設 (目前) 文化特性 ToString()
來回 (「R」) 格式 特定文化特性 ToString(IFormatProvider)
特定格式 預設 (目前) 文化特性 ToString(String)

參數 format 可以是任何有效的 標準數值字串,或 自訂數值格式字串的任何組合。 如果 format 等於 String.Empty 或 為 null ,則目前 BigInteger 物件的傳回值會使用來回格式標準格式化 (「R」) 。 如果 format 是任何其他值,方法會 FormatException 擲回 。

.NET 提供廣泛的格式支援,如下列格式主題中更詳細地說明:

參數 provider 是實作 IFormatProvider 。 其 GetFormat 方法會傳 NumberFormatInfo 回 物件,提供這個方法所傳回字串格式的特定文化特性資訊。 ToString(String, IFormatProvider)叫用 方法時,它會呼叫 provider 參數的 GetFormat 方法,並傳遞 Type 代表型別的 NumberFormatInfo 物件。 然後,方法 GetFormat 會傳 NumberFormatInfo 回 物件,提供設定參數格式 value 的資訊,例如負號符號、群組分隔符號符號或小數點符號。 有三種方式可以使用 provider 參數,將格式資訊 ToString(String, IFormatProvider) 提供給 方法:

如果 為 providernull ,則傳回字串的格式會以目前文化特性的物件為基礎 NumberFormatInfo

另請參閱

適用於

ToString(String)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

使用指定的格式,將目前 BigInteger 物件的值,轉換為其相等字串表示。

public:
 System::String ^ ToString(System::String ^ format);
public string ToString (string format);
public string ToString (string? format);
override this.ToString : string -> string
Public Function ToString (format As String) As String

參數

format
String

標準或自訂數值格式字串。

傳回

目前 BigInteger 值的字串表示,採用 format 參數所指定的格式。

例外狀況

format 不是有效的格式字串。

範例

下列範例會 BigInteger 初始化 值,並使用每個標準格式字串和一些自訂格式字串加以顯示。

BigInteger value = BigInteger.Parse("-903145792771643190182");
string[] specifiers = { "C", "D", "D25", "E", "E4", "e8", "F0",
                        "G", "N0", "P", "R", "X", "0,0.000",
                        "#,#.00#;(#,#.00#)" };

foreach (string specifier in specifiers)
   Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));

// The example displays the following output:
//       C: ($903,145,792,771,643,190,182.00)
//       D: -903145792771643190182
//       D25: -0000903145792771643190182
//       E: -9.031457E+020
//       E4: -9.0314E+020
//       e8: -9.03145792e+020
//       F0: -903145792771643190182
//       G: -903145792771643190182
//       N0: -903,145,792,771,643,190,182
//       P: -90,314,579,277,164,319,018,200.00 %
//       R: -903145792771643190182
//       X: CF0A55968BB1A7545A
//       0,0.000: -903,145,792,771,643,190,182.000
//       #,#.00#;(#,#.00#): (903,145,792,771,643,190,182.00)
Dim value As BigInteger = BigInteger.Parse("-903145792771643190182")
Dim specifiers() As String = { "C", "D", "D25", "E", "E4", "e8", "F0", 
                               "G", "N0", "P", "R", "X", "0,0.000", 
                               "#,#.00#;(#,#.00#)" }

For Each specifier As String In specifiers
   Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
Next   
' The example displays the following output:
'       C: ($903,145,792,771,643,190,182.00)
'       D: -903145792771643190182
'       D25: -0000903145792771643190182
'       E: -9.031457E+020
'       E4: -9.0314E+020
'       e8: -9.03145792e+020
'       F0: -903145792771643190182
'       G: -903145792771643190182
'       N0: -903,145,792,771,643,190,182
'       P: -90,314,579,277,164,319,018,200.00 %
'       R: -903145792771643190182
'       X: CF0A55968BB1A7545A
'       0,0.000: -903,145,792,771,643,190,182.000
'       #,#.00#;(#,#.00#): (903,145,792,771,643,190,182.00)

備註

方法 ToString(String) 會使用 NumberFormatInfo 代表目前文化特性慣例的 物件,以指定格式格式化 BigInteger 值。 如果您想要使用 「R」 或來回行程格式或指定不同的文化特性,請使用 方法的其他多載 ToString ,如下所示:

使用格式 針對文化特性 使用多載
來回 (「R」) 格式 預設 (目前) 文化特性 ToString()
來回 (「R」) 格式 特定文化特性 ToString(IFormatProvider)
特定格式 特定文化特性 ToString(String, IFormatProvider)

參數 format 可以是任何有效的 標準數值字串,或 自訂數值格式字串的任何組合。 如果 format 等於 String.Empty 或 為 null ,則目前 BigInteger 物件的傳回值會使用來回格式標準格式化 (「R」) 。 如果 format 是任何其他值,方法會 FormatException 擲回 。

.NET 提供廣泛的格式支援,如下列格式主題中更詳細地說明:

傳回字串的格式是由 NumberFormatInfo 目前文化特性的物件所決定。 根據 format 參數,這個物件會控制輸出字串中的負號、群組分隔符號和小數點符號等符號。 若要為目前文化特性以外的文化特性提供格式資訊,請呼叫 ToString(String, IFormatProvider) 多載。

另請參閱

適用於

ToString(IFormatProvider)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

使用指定的文化特性特定格式資訊,將目前 BigInteger 物件的數值轉換為其對等字串表示。

public:
 System::String ^ ToString(IFormatProvider ^ provider);
public string ToString (IFormatProvider provider);
public string ToString (IFormatProvider? provider);
override this.ToString : IFormatProvider -> string
Public Function ToString (provider As IFormatProvider) As String

參數

provider
IFormatProvider

物件,提供特定文化特性格式資訊。

傳回

目前 BigInteger 值的字串表示,採用 provider 參數所指定的格式。

範例

下列範例會具現化自訂物件,該物件 NumberFormatInfo 會將圖格 (~) 定義為負號。 方法 ToString(IFormatProvider) 接著會使用自訂 NumberFormatInfo 物件來顯示負 BigInteger 值。

BigInteger number = 9867857831128;
number = BigInteger.Pow(number, 3) * BigInteger.MinusOne;

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

Console.WriteLine(number.ToString(bigIntegerProvider));
Dim number As BigInteger = 9867857831128
number = BigInteger.Pow(number, 3) * BigInteger.MinusOne

Dim bigIntegerProvider As New NumberFormatInfo()
bigIntegerProvider.NegativeSign = "~"      

Console.WriteLine(number.ToString(bigIntegerProvider))

備註

方法 BigIntegerToString(IFormatProvider) 使用 NumberFormatInfo 指定文化特性的 物件,格式化 「R」 或來回格式中的值。 如果您想要指定不同的格式或目前的文化特性,請使用 方法的其他多載 ToString ,如下所示:

使用格式 針對文化特性 使用多載
來回 (「R」) 格式 預設 (目前) 文化特性 ToString()
特定格式 預設 (目前) 文化特性 ToString(String)
特定格式 特定文化特性 ToString(String, IFormatProvider)

參數 provider 是實作 IFormatProvider 。 其 GetFormat 方法會傳 NumberFormatInfo 回 物件,提供這個方法所傳回字串格式的特定文化特性資訊。 如果 為 providernull ,則會 BigInteger 使用 NumberFormatInfo 目前文化特性的物件來格式化值。 使用一般格式規範控制值字串表示 BigInteger 之物件的唯一屬性 NumberFormatInfoNumberFormatInfo.NegativeSign ,它會定義代表負號的字元。

參數 provider 可以是下列其中一項:

適用於

ToString()

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

將目前 BigInteger 物件的數值,轉換為其相等的字串表示。

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

傳回

目前 BigInteger 值的字串表示。

範例

下列範例會使用預設 ToString() 方法顯示 BigInteger 值。 它也會顯示使用某些標準格式規範所產生的值字串表示 BigInteger 。 這些範例會使用 en-US 文化特性的格式設定慣例來顯示。

// Initialize a BigInteger value.
BigInteger value = BigInteger.Add(UInt64.MaxValue, 1024);

// Display value using the default ToString method.
Console.WriteLine(value.ToString());
// Display value using some standard format specifiers.
Console.WriteLine(value.ToString("G"));
Console.WriteLine(value.ToString("C"));
Console.WriteLine(value.ToString("D"));
Console.WriteLine(value.ToString("F"));
Console.WriteLine(value.ToString("N"));
Console.WriteLine(value.ToString("X"));
// The example displays the following output on a system whose current
// culture is en-US:
//       18446744073709552639
//       18446744073709552639
//       $18,446,744,073,709,552,639.00
//       18446744073709552639
//       18446744073709552639.00
//       18,446,744,073,709,552,639.00
//       100000000000003FF
' Initialize a BigInteger value.
Dim value As BigInteger = BigInteger.Add(UInt64.MaxValue, 1024)

' Display value using the default ToString method.
Console.WriteLine(value.ToString())        
' Display value using some standard format specifiers.
Console.WriteLine(value.ToString("G"))
Console.WriteLine(value.ToString("C"))
Console.WriteLine(value.ToString("D"))
Console.WriteLine(value.ToString("F"))
Console.WriteLine(value.ToString("N"))
Console.WriteLine(value.ToString("X"))       
' The example displays the following output on a system whose current 
' culture is en-US:
'       18446744073709552639
'       18446744073709552639
'       $18,446,744,073,709,552,639.00
'       18446744073709552639
'       18446744073709552639.00
'       18,446,744,073,709,552,639.00
'       100000000000003FF

備註

方法會 ToString() 格式化 BigInteger 目前文化特性的 「R」 或來回格式中的值。 如果您想要指定不同的格式或文化特性,請使用 方法的其他多載 ToString ,如下所示:

使用格式 針對文化特性 使用多載
來回 (「R」) 格式 特定文化特性 ToString
特定格式 預設 (目前) 文化特性 ToString(String)
特定格式 特定文化特性 ToString(String, IFormatProvider)

如果值的值為負數,則值的字串表示 BigInteger 包含負號,以及介於 0 到 9 之間的數位序列,而沒有前置零。 負號是由 NumberFormatInfo 目前文化特性的物件所定義。

另請參閱

適用於