BigInteger.ToString 方法

定義

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

多載

ToString(String, IFormatProvider)

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

ToString(String)

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

ToString(IFormatProvider)

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

ToString()

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

ToString(String, IFormatProvider)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
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 參數的 方法,並傳遞Type代表NumberFormatInfo型別的 GetFormat 物件。 GetFormat然後,方法會NumberFormatInfo傳回 物件,提供格式化value參數的資訊,例如負號符號、群組分隔符或小數點符號。 有三種方式可以使用 provider 參數,將格式資訊 ToString(String, IFormatProvider) 提供給 方法:

如果 providernull,則傳回字串的格式設定是以目前文化特性的對象為基礎 NumberFormatInfo

另請參閱

適用於

ToString(String)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
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)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
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))

備註

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

若要使用格式 針對文化特性 使用多載
往返 (“R”) 格式 默認 (目前) 文化特性 ToString()
特定格式 默認 (目前) 文化特性 ToString(String)
特定格式 特定文化特性 ToString(String, IFormatProvider)

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

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

適用於

ToString()

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
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 定義。

另請參閱

適用於