使用英语阅读

通过


IFormattable 接口

定义

提供一种功能,用以将对象的值格式化为字符串表示形式。

C#
public interface IFormattable
C#
[System.Runtime.InteropServices.ComVisible(true)]
public interface IFormattable
派生
属性

示例

下面的示例定义一个实现 Temperature 接口的 IFormattable 类。 类支持四种格式说明符:“G”和“C”,指示温度以摄氏度为单位显示;“F”,指示温度以华氏度显示;和“K”,指示温度以开氏度为单位显示。 此外,实现 IFormattable.ToString 还可以处理为 null 或空的格式字符串。 类定义的Temperature另外两ToString个方法只是包装对 实现的IFormattable.ToString调用。

C#
using System;
using System.Globalization;

public class Temperature : IFormattable
{
   private decimal temp;

   public Temperature(decimal temperature)
   {
      if (temperature < -273.15m)
        throw new ArgumentOutOfRangeException(String.Format("{0} is less than absolute zero.",
                                              temperature));
      this.temp = temperature;
   }

   public decimal Celsius
   {
      get { return temp; }
   }

   public decimal Fahrenheit
   {
      get { return temp * 9 / 5 + 32; }
   }

   public decimal Kelvin
   {
      get { return temp + 273.15m; }
   }

   public override string ToString()
   {
      return this.ToString("G", CultureInfo.CurrentCulture);
   }

   public string ToString(string format)
   {
      return this.ToString(format, CultureInfo.CurrentCulture);
   }

   public string ToString(string format, IFormatProvider provider)
   {
      if (String.IsNullOrEmpty(format)) format = "G";
      if (provider == null) provider = CultureInfo.CurrentCulture;

      switch (format.ToUpperInvariant())
      {
         case "G":
         case "C":
            return temp.ToString("F2", provider) + " °C";
         case "F":
            return Fahrenheit.ToString("F2", provider) + " °F";
         case "K":
            return Kelvin.ToString("F2", provider) + " K";
         default:
            throw new FormatException(String.Format("The {0} format string is not supported.", format));
      }
   }
}

下面的示例随后直接或使用复合格式字符串调用 IFormattable.ToString 实现。

C#
public class Example
{
   public static void Main()
   {
      // Use composite formatting with format string in the format item.
      Temperature temp1 = new Temperature(0);
      Console.WriteLine("{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)\n", temp1);

      // Use composite formatting with a format provider.
      temp1 = new Temperature(-40);
      Console.WriteLine(String.Format(CultureInfo.CurrentCulture, "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)", temp1));
      Console.WriteLine(String.Format(new CultureInfo("fr-FR"), "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)\n", temp1));

      // Call ToString method with format string.
      temp1 = new Temperature(32);
      Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)\n",
                        temp1.ToString("C"), temp1.ToString("K"), temp1.ToString("F"));

      // Call ToString with format string and format provider
      temp1 = new Temperature(100)      ;
      NumberFormatInfo current = NumberFormatInfo.CurrentInfo;
      CultureInfo nl = new CultureInfo("nl-NL");
      Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)",
                        temp1.ToString("C", current), temp1.ToString("K", current), temp1.ToString("F", current));
      Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)",
                        temp1.ToString("C", nl), temp1.ToString("K", nl), temp1.ToString("F", nl));
   }
}
// The example displays the following output:
//    0.00 °C (Celsius) = 273.15 K (Kelvin) = 32.00 °F (Fahrenheit)
//
//    -40.00 °C (Celsius) = 233.15 K (Kelvin) = -40.00 °F (Fahrenheit)
//    -40,00 °C (Celsius) = 233,15 K (Kelvin) = -40,00 °F (Fahrenheit)
//
//    32.00 °C (Celsius) = 305.15 K (Kelvin) = 89.60 °F (Fahrenheit)
//
//    100.00 °C (Celsius) = 373.15 K (Kelvin) = 212.00 °F (Fahrenheit)
//    100,00 °C (Celsius) = 373,15 K (Kelvin) = 212,00 °F (Fahrenheit)

注解

接口 IFormattable 基于格式字符串和格式提供程序将对象转换为其字符串表示形式。

格式字符串通常定义对象的常规外观。 例如,.NET Framework支持以下内容:

还可以定义自己的格式字符串,以支持应用程序定义类型的格式设置。

格式提供程序返回格式设置对象,该对象通常定义用于将对象转换为其字符串表示形式的符号。 例如,将数字转换为货币值时,格式提供程序定义结果字符串中显示的货币符号。 .NET Framework定义了三个格式提供程序:

此外,可以定义自己的自定义格式提供程序,以提供格式设置中使用的特定于区域性、特定于专业或行业的信息。 有关使用自定义格式提供程序实现自定义格式设置的详细信息,请参阅 ICustomFormatter

接口 IFormattable 定义单个方法 ToString,该方法为实现类型提供格式设置服务。 ToString可以直接调用 方法。 此外,它由 Convert.ToString(Object)Convert.ToString(Object, IFormatProvider) 方法以及.NET Framework中使用复合格式设置功能的方法自动调用。 此类方法包括 Console.WriteLine(String, Object)String.FormatStringBuilder.AppendFormat(String, Object)等。 对 ToString 方法的格式字符串中的每个格式项调用 方法。

接口 IFormattable 由基数据类型实现。

实施者说明

需要对字符串的格式设置进行比提供的更多的 ToString() 控制的类应实现 IFormattable

实现 的 IFormattable 类必须支持“G” (常规) 格式说明符。 除了“G”说明符之外, 类还可以定义它支持的格式说明符列表。 此外, 类必须准备好处理 格式 null说明符。 有关设置代码格式和格式设置的详细信息,请参阅 设置类型格式

方法

ToString(String, IFormatProvider)

使用指定格式对当前实例的值设置格式。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另请参阅