Bewerken

Share via


BigInteger.ToString Method

Definition

Converts the numeric value of the current BigInteger object to its equivalent string representation.

Overloads

ToString(String, IFormatProvider)

Converts the numeric value of the current BigInteger object to its equivalent string representation by using the specified format and culture-specific format information.

ToString(String)

Converts the numeric value of the current BigInteger object to its equivalent string representation by using the specified format.

ToString(IFormatProvider)

Converts the numeric value of the current BigInteger object to its equivalent string representation by using the specified culture-specific formatting information.

ToString()

Converts the numeric value of the current BigInteger object to its equivalent string representation.

ToString(String, IFormatProvider)

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

Converts the numeric value of the current BigInteger object to its equivalent string representation by using the specified format and culture-specific format information.

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

Parameters

format
String

A standard or custom numeric format string.

provider
IFormatProvider

An object that supplies culture-specific formatting information.

Returns

The string representation of the current BigInteger value as specified by the format and provider parameters.

Implements

Exceptions

format is not a valid format string.

Examples

The following example initializes a BigInteger value, and displays it to the console using a standard format string and a NumberFormatInfo object that defines the tilde (~) as a negative sign.

// 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.
    let bigIntegerFormatter = NumberFormatInfo()
    bigIntegerFormatter.NegativeSign <- "~"

    let value = BigInteger.Parse "-903145792771643190182"

    let specifiers =
        [| "C"
           "D"
           "D25"
           "E"
           "E4"
           "e8"
           "F0"
           "G"
           "N0"
           "P"
           "R"
           "X"
           "0,0.000"
           "#,#.00#;(#,#.00#)" |]

    for specifier in specifiers do
        printfn $"{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)

Remarks

The ToString(String, IFormatProvider) method formats a BigInteger value in a specified format by using the NumberFormatInfo object of a specified culture. If you want to use the round-trip format or default culture settings, use the other overloads of the ToString method, as follows:

To use format For culture Use the overload
Round-trip ("R") format Default (current) culture ToString()
Round-trip ("R") format A specific culture ToString(IFormatProvider)
A specific format Default (current) culture ToString(String)

The format parameter can be any valid standard numeric string, or any combination of custom numeric format strings. If format is equal to String.Empty or is null, the return value of the current BigInteger object is formatted with the round-trip format specifier ("R"). If format is any other value, the method throws a FormatException.

.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

The provider parameter is an IFormatProvider implementation. Its GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of the string returned by this method. When the ToString(String, IFormatProvider) method is invoked, it calls the provider parameter's GetFormat method and passes it a Type object that represents the NumberFormatInfo type. The GetFormat method then returns the NumberFormatInfo object that provides information for formatting the value parameter, such as the negative sign symbol, the group separator symbol, or the decimal point symbol. There are three ways to use the provider parameter to supply formatting information to the ToString(String, IFormatProvider) method:

  • You can pass a CultureInfo object that represents the culture that supplies formatting information. Its GetFormat method returns the NumberFormatInfo object that provides numeric formatting information for that culture.

  • You can pass the actual NumberFormatInfo object that provides numeric formatting information. (Its implementation of GetFormat just returns itself.)

  • You can pass a custom object that implements IFormatProvider. Its GetFormat method instantiates and returns the NumberFormatInfo object that provides formatting information.

If provider is null, the formatting of the returned string is based on the NumberFormatInfo object of the current culture.

See also

Applies to

ToString(String)

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

Converts the numeric value of the current BigInteger object to its equivalent string representation by using the specified format.

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

Parameters

format
String

A standard or custom numeric format string.

Returns

The string representation of the current BigInteger value in the format specified by the format parameter.

Exceptions

format is not a valid format string.

Examples

The following example initializes a BigInteger value and displays it by using each standard format string and some custom format strings.

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)
    // Define a BigInteger value.
    let value = BigInteger.Parse "-903145792771643190182"

    let specifiers =
        [| "C"
           "D"
           "D25"
           "E"
           "E4"
           "e8"
           "F0"
           "G"
           "N0"
           "P"
           "R"
           "X"
           "0,0.000"
           "#,#.00#;(#,#.00#)" |]

    for specifier in specifiers do
        printfn $"{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)

Remarks

The ToString(String) method formats a BigInteger value in a specified format by using a NumberFormatInfo object that represents the conventions of the current culture. If you want to use the "R", or round-trip, format or specify a different culture, use the other overloads of the ToString method, as follows:

To use format For culture Use the overload
Round-trip ("R") format Default (current) culture ToString()
Round-trip ("R") format A specific culture ToString(IFormatProvider)
A specific format A specific culture ToString(String, IFormatProvider)

The format parameter can be any valid standard numeric string, or any combination of custom numeric format strings. If format is equal to String.Empty or is null, the return value of the current BigInteger object is formatted with the round-trip format specifier ("R"). If format is any other value, the method throws a FormatException.

.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

The format of the returned string is determined by the NumberFormatInfo object for the current culture. Depending on the format parameter, this object controls symbols such as the negative sign, the group separator, and the decimal point symbol in the output string. To provide formatting information for cultures other than the current culture, call the ToString(String, IFormatProvider) overload.

See also

Applies to

ToString(IFormatProvider)

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

Converts the numeric value of the current BigInteger object to its equivalent string representation by using the specified culture-specific formatting information.

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

Parameters

provider
IFormatProvider

An object that supplies culture-specific formatting information.

Returns

The string representation of the current BigInteger value in the format specified by the provider parameter.

Examples

The following example instantiates a custom NumberFormatInfo object that defines the tilde (~) as a negative sign. The ToString(IFormatProvider) method then uses the custom NumberFormatInfo object to display a negative BigInteger value.

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

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

Console.WriteLine(number.ToString(bigIntegerProvider));
let number = bigint 9867857831128L
let number = BigInteger.Pow(number, 3) * BigInteger.MinusOne

let bigIntegerProvider = NumberFormatInfo()
bigIntegerProvider.NegativeSign <- "~"

printfn $"{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))

Remarks

The ToString(IFormatProvider) method formats a BigInteger value in the "R", or round-trip, format by using the NumberFormatInfo object of a specified culture. If you want to specify a different format or the current culture, use the other overloads of the ToString method, as follows:

To use format For culture Use the overload
Round-trip ("R") format Default (current) culture ToString()
A specific format Default (current) culture ToString(String)
A specific format A specific culture ToString(String, IFormatProvider)

The provider parameter is an IFormatProvider implementation. Its GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of the string returned by this method. If provider is null, the BigInteger value is formatted using the NumberFormatInfo object of the current culture. The only property of the NumberFormatInfo object that controls the string representation of the BigInteger value using the general format specifier is NumberFormatInfo.NegativeSign, which defines the character that represents the negative sign.

The provider parameter can be one of the following:

Applies to

ToString()

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

Converts the numeric value of the current BigInteger object to its equivalent string representation.

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

Returns

The string representation of the current BigInteger value.

Examples

The following example displays a BigInteger value by using the default ToString() method. It also displays the string representations of the BigInteger value that results from using some standard format specifiers. The examples are displayed using the formatting conventions of the en-US culture.

// 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.
    let value = BigInteger.Add(UInt64.MaxValue, 1024)

    // Display value using the default ToString method.
    printfn $"{value.ToString()}"
    // Display value using some standard format specifiers.
    printfn $"""{value.ToString("G")}"""
    printfn $"""{value.ToString("C")}"""
    printfn $"""{value.ToString("D")}"""
    printfn $"""{value.ToString("F")}"""
    printfn $"""{value.ToString("N")}"""
    printfn $"""{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

Remarks

The ToString() method formats a BigInteger value in the "R", or round-trip, format of the current culture. If you want to specify a different format or culture, use the other overloads of the ToString method, as follows:

To use format For culture Use the overload
Round-trip ("R") format A specific culture ToString
A specific format Default (current) culture ToString(String)
A specific format A specific culture ToString(String, IFormatProvider)

The string representation of the BigInteger value includes a negative sign if its value is negative, and a sequence of digits ranging from 0 to 9 without leading zeros. The negative sign is defined by the NumberFormatInfo object for the current culture.

See also

Applies to