IFormattable インターフェイス
定義
オブジェクトの値を文字列形式で書式設定する機能を提供します。Provides functionality to format the value of an object into a string representation.
public interface class IFormattable
[System.Runtime.InteropServices.ComVisible(true)]
public interface IFormattable
type IFormattable = interface
Public Interface IFormattable
- 派生
- 属性
例
次の例では、 Temperature
インターフェイスを実装する IFormattable クラスを定義しています。The following example defines a Temperature
class that implements the IFormattable interface. クラスは、次の4つの書式指定子をサポートしています。"G" と "C" は、気温を摂氏で表示することを示します。"F"。気温が華氏で表示されることを示します。"K" は、気温がケルビンで表示されることを示します。The class supports four format specifiers: "G" and "C", which indicate that the temperature is to be displayed in Celsius; "F", which indicates that the temperature is to be displayed in Fahrenheit; and "K", which indicates that the temperature is to be displayed in Kelvin. また、 IFormattable.ToString実装では、 null
または空の書式指定文字列を処理することもできます。In addition, the IFormattable.ToString implementation also can handle a format string that is null
or empty. クラスによっToString
て定義される他の2つのメソッドはIFormattable.ToString 、実装への呼び出しをラップするだけです。 Temperature
The other two ToString
methods defined by the Temperature
class simply wrap a call to the IFormattable.ToString implementation.
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));
}
}
}
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直接または複合書式指定文字列を使用して、実装を呼び出します。The following example then calls the IFormattable.ToString implementation either directly or by using a composite format string.
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)
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は、書式指定文字列と書式プロバイダーに基づいて、オブジェクトを文字列形式に変換します。The IFormattable interface converts an object to its string representation based on a format string and a format provider.
書式指定文字列は、通常、オブジェクトの全般的な外観を定義します。A format string typically defines the general appearance of an object. たとえば、.NET Framework では次のことがサポートされています。For example, the .NET Framework supports the following:
列挙値の書式を設定するための標準書式指定文字列 (「列挙型書式指定文字列」を参照してください)。Standard format strings for formatting enumeration values (see Enumeration Format Strings).
数値の書式設定に使用する標準およびカスタムの書式指定文字列 (「標準の数値書式指定文字列」および「カスタム数値書式指定文字列」を参照してください)。Standard and custom format strings for formatting numeric values (see Standard Numeric Format Strings and Custom Numeric Format Strings).
日付と時刻の値を書式設定するための標準およびカスタムの書式指定文字列 (「標準の日付と時刻の書式指定文字列」および「カスタム日時書式指定文字列」を参照)。Standard and custom format strings for formatting date and time values (see Standard Date and Time Format Strings and Custom Date and Time Format Strings).
時間間隔を書式設定するための標準およびカスタムの書式指定文字列 (「標準の Timespan 書式指定文字列」および「カスタム TimeSpan 書式指定文字列」を参照してください)。Standard and custom format strings for formatting time intervals (see Standard TimeSpan Format Strings and Custom TimeSpan Format Strings).
独自の書式指定文字列を定義して、アプリケーション定義型の書式設定をサポートすることもできます。You can also define your own format strings to support formatting of your application-defined types.
書式プロバイダーは、オブジェクトを文字列形式に変換するときに使用するシンボルを定義する書式設定オブジェクトを返します。A format provider returns a formatting object that typically defines the symbols used in converting an object to its string representation. たとえば、数値を通貨値に変換すると、書式プロバイダーによって、結果文字列に表示される通貨記号が定義されます。For example, when you convert a number to a currency value, a format provider defines the currency symbol that appears in the result string. .NET Framework は、次の3つの書式プロバイダーを定義します。The .NET Framework defines three format providers:
数値を書式設定するためのNumberFormatInfoオブジェクト、または日付と時刻の値を書式設定するためのSystem.Globalization.CultureInfo オブジェクトのいずれかDateTimeFormatInfoを返すクラス。The System.Globalization.CultureInfo class, which returns either a NumberFormatInfo object for formatting numeric values, or a DateTimeFormatInfo object for formatting date and time values.
System.Globalization.NumberFormatInfoクラス。数値を書式設定するために自身のインスタンスを返します。The System.Globalization.NumberFormatInfo class, which returns an instance of itself for formatting numeric values.
System.Globalization.DateTimeFormatInfoクラス。日付と時刻の値を書式設定するために自身のインスタンスを返します。The System.Globalization.DateTimeFormatInfo class, which returns an instance of itself for formatting date and time values.
また、独自のカスタム書式プロバイダーを定義して、書式設定で使用されるカルチャ固有の情報、職業に固有の情報、または業界固有の情報を提供することもできます。In addition, you can define your own custom format providers to supply culture-specific, profession-specific, or industry-specific information used in formatting. カスタム書式プロバイダーを使用したカスタム書式設定の実装の詳細にICustomFormatterついては、「」を参照してください。For more information about implementing custom formatting by using a custom format provider, see ICustomFormatter.
インターフェイスIFormattableは、実装する型のToString書式指定サービスを提供する、という1つのメソッドを定義します。The IFormattable interface defines a single method, ToString, that supplies formatting services for the implementing type. メソッドToStringは、直接呼び出すことができます。The ToString method can be called directly. さらに、メソッドConvert.ToString(Object)およびConvert.ToString(Object, IFormatProvider)メソッドによって自動的に呼び出され、.NET Framework の複合書式指定機能を使用するメソッドによって呼び出されます。In addition, it is 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. そのようなConsole.WriteLine(String, Object)メソッドString.FormatにはStringBuilder.AppendFormat(String, Object)、、、などがあります。Such methods include Console.WriteLine(String, Object), String.Format, and StringBuilder.AppendFormat(String, Object), among others. メソッドToStringは、メソッドの書式指定文字列の各書式項目に対して呼び出されます。The ToString method is called for each format item in the method's format string.
IFormattableインターフェイスは、基本データ型によって実装されます。The IFormattable interface is implemented by the base data types.
注意 (実装者)
ToString()で提供される文字列の書式設定をより詳細に制御するIFormattable必要があるクラスは、を実装する必要があります。Classes that require more control over the formatting of strings than ToString() provides should implement IFormattable.
を実装IFormattableするクラスは、"G" (general) 書式指定子をサポートしている必要があります。A class that implements IFormattable must support the "G" (general) format specifier. "G" 指定子に加えて、クラスは、サポートする書式指定子のリストを定義できます。Besides the "G" specifier, the class can define the list of format specifiers that it supports. また、クラスは、というnull
書式指定子を処理できるように準備する必要があります。In addition, the class must be prepared to handle a format specifier that is null
. 書式設定および書式設定コードの詳細については、「型の書式設定」を参照してください。For more information about formatting and formatting codes, see Formatting Types
メソッド
ToString(String, IFormatProvider) |
指定した書式を使用して、現在のインスタンスの値に書式を付けます。Formats the value of the current instance using the specified format. |