Share via


BigInteger.ToString Method (String, IFormatProvider)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System.Numerics
Assembly:  System.Numerics (in System.Numerics.dll)

Syntax

'Declaration
Public Function ToString ( _
    format As String, _
    provider As IFormatProvider _
) As String
public string ToString(
    string format,
    IFormatProvider provider
)

Parameters

  • format
    Type: System.String
    A standard numeric format string. The BigInteger type supports the "D", "G", "R", and "X" standard format strings.

Return Value

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

Implements

IFormattable.ToString(String, IFormatProvider)

Exceptions

Exception Condition
FormatException

format is not a valid format string.

Remarks

The format parameter can be one of four standard numeric format specifiers ("D", "G", "R", and "X"). If format is equal to String.Empty or is nulla null reference (Nothing in Visual Basic), the return value of the current BigInteger object is formatted with the general format specifier ("G"). If format is any other value, the method throws a FormatException.

In most cases, the ToString method supports 50 decimal digits of precision. That is, if the BigInteger value has more than 50 digits, only the 50 most significant digits are preserved in the output string; all other digits are replaced with zeros. However, BigInteger supports the "R" standard format specifier, which is intended to round-trip numeric values. The string returned by the ToString(String) method with the "R" format string preserves the whole BigInteger value.

The .NET Framework 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 nulla null reference (Nothing in Visual Basic), the formatting of the returned string is based on the NumberFormatInfo object of the current culture.

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.
Dim bigIntegerFormatter As New NumberFormatInfo()
bigIntegerFormatter.NegativeSign = "~"

Dim value As BigInteger = BigInteger.MinusOne * BigInteger.Multiply(UInt64.MaxValue, 48) +
                          17702077233584712662ul
Dim specifiers() As String = { "D", "D25", "G", "R", "X" }

For Each specifier As String In specifiers
   outputBlock.Text += String.Format("{0}: {1}", specifier, value.ToString(specifier,  
                     bigIntegerformatter)) + vbCrLf
Next
' The example displays the following output:
'    D: ~903145792771643190182
'    D25: ~0000903145792771643190182
'    G: ~903145792771643190182
'    R: ~903145792771643190182
'    X: CF0A55968BB1A7545A
// Redefine the negative sign as the tilde for the invariant culture.
NumberFormatInfo bigIntegerFormatter = new NumberFormatInfo();
bigIntegerFormatter.NegativeSign = "~";

BigInteger value = BigInteger.MinusOne * UInt64.MaxValue * 48 + 17702077233584712662;
string[] specifiers = { "D", "D25", "G", "R", "X" };

foreach (string specifier in specifiers)
   outputBlock.Text += String.Format("{0}: {1}\n", specifier, value.ToString(specifier,
                     bigIntegerFormatter));

// The example displays the following output:
//    D: ~903145792771643190182
//    D25: ~0000903145792771643190182
//    G: ~903145792771643190182
//    R: ~903145792771643190182
//    X: CF0A55968BB1A7545A

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.