IFormattable 인터페이스

정의

개체의 값을 문자열 표현으로 서식 지정하는 기능을 제공합니다.

public interface class IFormattable
public interface IFormattable
[System.Runtime.InteropServices.ComVisible(true)]
public interface IFormattable
type IFormattable = interface
[<System.Runtime.InteropServices.ComVisible(true)>]
type IFormattable = interface
Public Interface IFormattable
파생
특성

예제

다음 예제에서는 Temperature 인터페이스를 구현하는 IFormattable 클래스를 정의합니다. 이 클래스는 4 명의 형식 지정 자가 지원: 온도 섭씨;에 표시 되는 여부를 나타내는 "G" 및 "C" "F" 온도를 화씨로; 표시할 임을 나타냅니다 및 "K" 온도를 켈빈 표시할 임을 나타냅니다. 또한 합니다 IFormattable.ToString 구현도 처리할 수는 서식 문자열입니다 null 이거나 비어 있습니다. 다른 두 ToString 정의한 메서드를 Temperature 클래스는 호출을 래핑하는 IFormattable.ToString 구현 합니다.

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));
      }
   }
}
open System
open System.Globalization

type Temperature(temperature: decimal) =
    do 
        if temperature < -273.15M then
            raise (ArgumentOutOfRangeException $"{temperature} is less than absolute zero.")

    member _.Celsius =
        temperature

    member _.Fahrenheit =
        temperature * 9M / 5M + 32M

    member _.Kelvin =
        temperature + 273.15m

    override this.ToString() =
        this.ToString("G", CultureInfo.CurrentCulture)

    member this.ToString(format) =
        this.ToString(format, CultureInfo.CurrentCulture)

    member this.ToString(format, provider: IFormatProvider) =
        let format =
            if String.IsNullOrEmpty format then "G"
            else format

        let provider =
            if isNull provider then 
                CultureInfo.CurrentCulture :> IFormatProvider
            else provider

        match format.ToUpperInvariant() with
        | "G" | "C" ->
            temperature.ToString("F2", provider) + " °C"
        | "F" ->
            this.Fahrenheit.ToString("F2", provider) + " °F"
        | "K" ->
            this.Kelvin.ToString("F2", provider) + " K"
        | _ ->
            raise (FormatException $"The {format} format string is not supported.")

    interface IFormattable with
        member this.ToString(format, provider) = this.ToString(format, provider)
Imports System.Globalization

Public Class Temperature : Implements IFormattable
   Private temp As Decimal
   
   Public Sub New(temperature As Decimal)
      If temperature < -273.15 Then _ 
        Throw New ArgumentOutOfRangeException(String.Format("{0} is less than absolute zero.", _
                                              temperature))
      Me.temp = temperature
   End Sub
   
   Public ReadOnly Property Celsius As Decimal
      Get
         Return temp
      End Get
   End Property
   
   Public ReadOnly Property Fahrenheit As Decimal
      Get
         Return temp * 9 / 5 + 32
      End Get
   End Property
   
   Public ReadOnly Property Kelvin As Decimal
      Get
         Return temp + 273.15d
      End Get
   End Property

   Public Overrides Function ToString() As String
      Return Me.ToString("G", CultureInfo.CurrentCulture)
   End Function
      
   Public Overloads Function ToString(fmt As String) As String
      Return Me.ToString(fmt, CultureInfo.CurrentCulture)
   End Function
   
   Public Overloads Function ToString(fmt As String, provider As IFormatProvider) _
                   As String _
                   Implements IFormattable.ToString
      If String.IsNullOrEmpty(fmt) Then fmt = "G"
      If provider Is Nothing Then provider = CultureInfo.CurrentCulture
      
      Select Case fmt.ToUpperInvariant()
         Case "G", "C"
            Return temp.ToString("F2", provider) + " °C" 
         Case "F"
            Return Fahrenheit.ToString("F2", provider) + " °F"
         Case "K"
            Return Kelvin.ToString("F2", provider) + " K"
         Case Else
            Throw New FormatException(String.Format("The {0} format string is not supported.", fmt))
      End Select
   End Function
End Class

다음 예제에서는 호출을 IFormattable.ToString 구현을 직접 또는 합성 서식 문자열을 사용 하 여 합니다.

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)
open System
open System.Globalization

[<EntryPoint>]
let main _ =
    // Use composite formatting with format string in the format item.
    let temp1 = Temperature 0
    Console.WriteLine("{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)\n", temp1)

    // Use composite formatting with a format provider.
    let temp1 = Temperature -40
    String.Format(CultureInfo.CurrentCulture, "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)", temp1)
    |> printfn "%s"
    String.Format(CultureInfo "fr-FR", "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)\n", temp1)
    |> printfn "%s"

    // Call ToString method with format string.
    let temp1 = 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
    let temp1 = Temperature 100      
    let current = NumberFormatInfo.CurrentInfo
    let nl = 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))
    0

// 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)
Module Example
   Public Sub Main()
      ' Use composite formatting with format string in the format item.
      Dim temp1 As New Temperature(0)
      Console.WriteLine("{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)", temp1)
      Console.WriteLine()
      
      ' 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)", temp1))
      Console.WriteLine()
      
      ' Call ToString method with format string.
      temp1 = New Temperature(32)
      Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)", _
                        temp1.ToString("C"), temp1.ToString("K"), temp1.ToString("F"))
      Console.WriteLine()

      ' Call ToString with format string and format provider
      temp1 = New Temperature(100)      
      Dim current As NumberFormatInfo = NumberFormatInfo.CurrentInfo
      Dim nl As 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))
   End Sub
End Module
' 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.Format, 및 StringBuilder.AppendFormat(String, Object), 특히 합니다. ToString 메서드의 형식 문자열의 각 서식 항목에 대 한 호출 됩니다.

IFormattable 인터페이스는 기본 데이터 형식으로 구현 됩니다.

구현자 참고

보다는 문자열의 서식을 제어할 필요가 있는 클래스 ToString() 제공 구현 해야 IFormattable합니다.

구현 하는 클래스 IFormattable "G" (일반) 서식 지정자를 지원 해야 합니다. "G" 지정자 외 클래스 목록은 지원 되는 형식 지정자를 정의할 수 있습니다. 클래스는 형식 지정자를 처리 하도록 준비 해야 합니다 또한 null합니다. 코드 서식 지정 및 서식 지정에 대한 자세한 내용은 형식 지정을 참조하세요.

메서드

ToString(String, IFormatProvider)

지정된 형식을 사용하여 현재 인스턴스 값의 형식을 지정합니다.

적용 대상

추가 정보