Export (0) Print
Expand All

Convert.ToString Method (Object, IFormatProvider)

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

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

public static string ToString(
	Object value,
	IFormatProvider provider
)

Parameters

value
Type: System.Object

An object that supplies the value to convert, or null.

provider
Type: System.IFormatProvider

An object that supplies culture-specific formatting information.

Return Value

Type: System.String
The string representation of value, or String.Empty if value is an object whose value is null. If value is null, the method returns null.

If the value parameter implements the IConvertible interface, the method calls the IConvertible.ToString(IFormatProvider) implementation of value. Otherwise, if the value parameter implements the IFormattable interface, the method calls its IFormattable.ToString(String, IFormatProvider) implementation. If value implements neither interface, the method calls the value parameter's ToString() method, and the provider parameter is ignored.

The provider parameter is used if the value parameter implements the IConvertible or IFormattable interface. The most common use of the provider parameter is to specify culture-specific information used in the conversion of value. For example, if the value parameter is a negative decimal number, the provider parameter can supply culture-specific information about the notation used for the negative sign and decimal separator. The second example in the next section illustrates a format provider that does not supply culture-sensitive formatting information.

The following example defines a Temperature class that overrides the Object.ToString method but does not implement the IConvertible interface. The example illustrates how calls to the Convert.ToString(Object, IFormatProvider) method, in turn, call the Temperature.ToString method.

using System;

public class Temperature
{
   private decimal m_Temp;

   public Temperature(decimal temperature)
   {
      this.m_Temp = temperature;
   }

   public decimal Celsius
   {
      get { return this.m_Temp; }   
   }

   public decimal Kelvin
   {
      get { return this.m_Temp + 273.15m; }    
   }

   public decimal Fahrenheit
   {
      get { return Math.Round((decimal) (this.m_Temp * 9 / 5 + 32), 2); }
   }

   public override string ToString()
   {
      return m_Temp.ToString("N2") + " °C";
   }

}

public class Example
{
   public static void Main()
   {
      Temperature cold = new Temperature(-40);
      Temperature freezing = new Temperature(0);
      Temperature boiling = new Temperature(100);

      Console.WriteLine(Convert.ToString(cold, null));
      Console.WriteLine(Convert.ToString(freezing, null));
      Console.WriteLine(Convert.ToString(boiling, null));
   }
}
// The example dosplays the following output: 
//       -40.00 °C 
//       0.00 °C 
//       100.00 °C

The following example defines a Temperature class that implements the IFormattable interface but does not implement the IConvertible interface. Its IFormattable.ToString implementation represents the Temperature value in Celsius, Fahrenheit, or Kelvin, depending on the format string. The example also defines a TemperatureProvider class that implements IFormatProvider and provides a randomly generated format string that is used by the IFormattable implementation of the Temperature class.

using System;

public class Temperature : IFormattable 
{
   private decimal m_Temp; 

   public Temperature(decimal temperature)
   {
      this.m_Temp = temperature;
   } 

   public decimal Celsius 
   { get { return this.m_Temp; } } 

   public decimal Kelvin 
   { get { return this.m_Temp + 273.15m; } }   

   public decimal Fahrenheit
   {  get { return Math.Round(this.m_Temp * 9m / 5m + 32m, 2); } }

   public override String ToString() 
   { 
      return ToString("G", null); 
   } 

   public String ToString(String fmt, IFormatProvider provider) 
   {
      TemperatureProvider formatter = null;
      if (provider != null) 
         formatter = provider.GetFormat(typeof(TemperatureProvider)) 
                                       as TemperatureProvider;

      if (String.IsNullOrWhiteSpace(fmt)) {
         if (formatter != null) 
            fmt = formatter.Format;
         else
            fmt = "G";
      }

      switch (fmt.ToUpper()) {
         case "G":
         case "C":
            return m_Temp.ToString("N2") + " °C"; 
         case "F":
            return Fahrenheit.ToString("N2") + " °F";
         case "K":
            return Kelvin.ToString("N2") + " K";
         default:
            throw new FormatException(String.Format("'{0}' is not a valid format specifier.", fmt));
      }
   }                             
} 

public class TemperatureProvider : IFormatProvider
{
   private String[] fmtStrings = { "C", "G", "F", "K" };
   private Random rnd = new Random();

   public Object GetFormat(Type formatType) 
   { 
      return this; 
   }

   public String Format
   { get { return fmtStrings[rnd.Next(0, fmtStrings.Length)]; } }
}

public class Example
{
   public static void Main()
   {
      Temperature cold = new Temperature (-40);
      Temperature freezing = new Temperature (0);
      Temperature boiling = new Temperature (100);

      TemperatureProvider tp = new TemperatureProvider();

      Console.WriteLine(Convert.ToString(cold, tp));
      Console.WriteLine(Convert.ToString(freezing, tp));
      Console.WriteLine(Convert.ToString(boiling, tp));
   }
}
// The example displays output like the following: 
//       -40.00 °C 
//       273.15 K 
//       100.00 °C

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft