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”,指示温度以摄氏度显示:“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) 和 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) |
使用指定格式设置当前实例的值的格式。 |