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

一个提供区域性特定的格式设置信息的对象。

返回

formatprovider 参数指定的当前 BigInteger 值的字符串表示形式。

实现

例外

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)BigInteger使用NumberFormatInfo指定区域性的 对象设置指定格式的值的格式。 如果要使用往返格式或默认区域性设置,请使用 方法的其他重载 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))

注解

方法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()

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 对象为当前区域性定义。

另请参阅

适用于