System.IFormattable.ToString Method

Formats the value of the current instance using the specified format.

Syntax

public string ToString (string format, IFormatProvider formatProvider)

Parameters

format
The format to use.
formatProvider
The provider to use to format the value.

Returns

The value of the current instance in the specified format.

Exceptions

TypeReason
FormatExceptionThe specified format is invalid or cannot be used with the type of the current instance.

Remarks

The IFormattable.ToString(string, IFormatProvider) method converts a value to a string representation that can be expressed in multiple ways. Its precise format depends on specific symbols or a specified order defined by specific cultures, professions, or industries. You can call the method directly. It is also called automatically by the Convert.ToString(object) and Convert.ToString(object, IFormatProvider) methods, and by methods that use the composite formatting feature in the .NET Framework, such as string.Format(string, Object[]), Console.WriteLine(string, Object[]), and System.Text.StringBuilder.AppendFormat(string, Object[]). (For more information, see Composite Formatting.)

Composite formatting methods call the IFormattable.ToString(string, IFormatProvider) method once for each format item in a format string. The parameters passed to the method depend on the specific formatting method that is called and on the content of the format item, as follows:

  • If the format item does not include a format string (for example, if the format item is simply {0}), it is passed null as the value of the string parameter.

  • If the format item includes a format string (for example, {0:G}), that format string is passed as the value of the string parameter.

  • If the original method call does not include an IFormatProvider parameter, System.Globalization.CultureInfo.CurrentCulture is passed as the value of the IFormatProvider parameter.

  • If the original method call includes an IFormatProvider parameter, the provider that is supplied in the method call is passed as the value of the IFormatProvider parameter.

Note:

An object's IFormattable.ToString(string, IFormatProvider) implementation is called by composite formatting methods only if they are not passed an ICustomFormatter format provider, or if the ICustomFormatter.Format(string, object, IFormatProvider) method of the custom format provider returns null.

The .NET Framework includes three format providers, all of which implement the IFormatProvider interface:

  • System.Globalization.NumberFormatInfo supplies numeric formatting information, such as the characters to use for decimal and group separators, and the spelling and placement of currency symbols in monetary values.

  • System.Globalization.DateTimeFormatInfo supplies date-related and time-related formatting information, such as the position of the month, the day, and the year in a date pattern.

  • System.Globalization.CultureInfo contains the default formatting information in a specific culture, including the numeric format information, and date-related and time-related formatting information.

In addition, you can define your own custom format provider.

Example

The following example demonstrates using the IFormattable.ToString(string, IFormatProvider) method to display values in a variety of formats. The current system culture is U.S. English, which provides the default values for the formatProvider parameter of IFormattable.ToString(string, IFormatProvider).

C# Example

using System;
class FormattableExample {
    public static void Main() {
    double d = 123.12345678901234;
    string[] formats = {"C","E","e","F","G","N","P","R"};
    for (int i = 0; i< formats.Length;i++) 
        Console.WriteLine("{0:R} as {1}:  {2}",d,formats[i],d.ToString(formats[i],null));

    string[]intFormats = {"D","x","X"};
    int val = 255;
    for (int i = 0; i< intFormats.Length;i++) 
        Console.WriteLine("{0} as {1}:  {2}",val,intFormats[i],val.ToString(intFormats[i],null));

    }
}

The output is

123.12345678901234 as C: $123.12
123.12345678901234 as E: 1.231235E+002
123.12345678901234 as e: 1.231235e+002
123.12345678901234 as F: 123.12
123.12345678901234 as G: 123.123456789012
123.12345678901234 as N: 123.12
123.12345678901234 as P: 12,312.35 %
123.12345678901234 as R: 123.12345678901234
255 as D: 255
255 as x: ff
255 as X: FF

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0