IFormattable 介面
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供將物件值格式化為字串表示的功能。
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 類。 本類別支援四個格式指定符:「G」與「C」,表示溫度以攝氏(Celsius)表示;「F」表示溫度以華氏(華氏)表示;以及「K」,表示溫度以開爾文為單位顯示。 此外,實 IFormattable.ToString 作也能處理格式字串為 或 null 空。 類別定義Temperature的另外兩個ToString方法則只是將呼叫包裹到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 支援以下功能:
用於列舉值格式化的標準格式字串(參見列舉格式字串)。
標準與自訂格式字串用於格式化日期與時間值(參見 標準日期與時間格式字串 及 自訂日期與時間格式字串)。
用於時間區間格式化的標準與自訂格式字串(參見 標準 TimeSpan 格式字串 與 自訂 TimeSpan 格式字串)。
你也可以自訂格式字串,以支援應用程式定義型別的格式化。
格式提供者會回傳一個格式化物件,通常定義用來將物件轉換為字串表示的符號。 例如,當你將數字轉換成貨幣值時,格式提供者會定義結果字串中出現的貨幣符號。 .NET Framework 定義了三種格式提供者:
這個 System.Globalization.CultureInfo 類別,會回傳 NumberFormatInfo 一個用於格式化數值的物件,或 DateTimeFormatInfo 一個用於格式化日期與時間值的物件。
該 System.Globalization.NumberFormatInfo 類別回傳一個實例來格式化數值。
該 System.Globalization.DateTimeFormatInfo 類別,會回傳一個實例來格式化日期和時間值。
此外,你也可以自訂格式供應商,提供針對特定文化、專業或產業的格式資訊。 欲了解更多使用自訂格式提供者實作自訂格式的資訊,請參見 ICustomFormatter。
介面 IFormattable 定義了一個單一方法,為 ToString實作型別提供格式化服務。 ToString此方法可直接呼叫。 此外,它會被 Convert.ToString(Object) and Convert.ToString(Object, IFormatProvider) 方法自動呼叫,以及使用 .NET Framework 中 複合格式化功能 的方法。 此類方法包括 Console.WriteLine(String, Object)、 String.Format、 StringBuilder.AppendFormat(String, Object)及 等。 ToString方法的每個格式項目都會呼叫該方法的格式字串。
介面 IFormattable 由基礎資料型態實作。
給實施者的注意事項
需要比 提供更多字串 ToString() 格式控制的類別應該實作 IFormattable。
實作 IFormattable 的類別必須支援「G」(一般)格式規範符。 除了「G」這個指定符外,該類別還可以定義它所支援的格式指定符清單。 此外,類別必須準備好處理格式 null指定符。 欲了解更多格式及格式代碼資訊,請參閱 格式類型
方法
| 名稱 | Description |
|---|---|
| ToString(String, IFormatProvider) |
以指定格式化當前實例的值。 |