IFormattable.ToString(String, IFormatProvider) 方法
定义
使用指定格式对当前实例的值设置格式。Formats the value of the current instance using the specified format.
public:
System::String ^ ToString(System::String ^ format, IFormatProvider ^ formatProvider);
public string ToString (string format, IFormatProvider formatProvider);
public string ToString (string? format, IFormatProvider? formatProvider);
abstract member ToString : string * IFormatProvider -> string
Public Function ToString (format As String, formatProvider As IFormatProvider) As String
参数
- format
- String
要使用的格式。The format to use.
- 或 --or-
null 引用(在 Visual Basic 中为 Nothing
),用于使用为 IFormattable 实现的类型定义的默认格式。A null reference (Nothing
in Visual Basic) to use the default format defined for the type of the IFormattable implementation.
- formatProvider
- IFormatProvider
要用于格式化值的提供程序。The provider to use to format the value.
- 或 --or-
null 引用(在 Visual Basic 中为 Nothing
),用于从操作系统的当前区域设置获取数字格式信息。A null reference (Nothing
in Visual Basic) to obtain the numeric format information from the current locale setting of the operating system.
返回
采用指定格式的当前实例的值。The value of the current instance in the specified format.
示例
下面的示例演示 Temperature
实现方法的类 ToString 。The following example demonstrates a Temperature
class that implements the ToString method. 此代码示例是为类提供的更大示例的一部分 IFormattable 。This code example is part of a larger example provided for the IFormattable class.
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
注解
ToString方法将值转换为可通过多种方式表示的字符串表示形式。The ToString method converts a value to a string representation that can be expressed in multiple ways. 其精确格式取决于特定的符号或特定区域性、职业或行业定义的指定顺序。Its precise format depends on specific symbols or a specified order defined by specific cultures, professions, or industries. 可以直接调用方法。You can call the method directly. 它还由和方法自动调用 Convert.ToString(Object) , Convert.ToString(Object, IFormatProvider) 以及在 .NET Framework 中使用复合格式设置功能的方法,例如 String.Format(String, Object[]) 、 Console.WriteLine(String, Object[]) 和 StringBuilder.AppendFormat(String, Object[]) 。It is also 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, such as String.Format(String, Object[]), Console.WriteLine(String, Object[]), and StringBuilder.AppendFormat(String, Object[]). (有关详细信息,请参阅 复合格式设置。 ) (For more information, see Composite Formatting.)
复合格式设置方法 ToString 为格式字符串中的每个格式项调用方法一次。Composite formatting methods call the ToString method once for each format item in a format string. 传递给方法的参数取决于在格式项的内容上调用和的特定格式设置方法,如下所示:The parameters passed to the method depend on the specific formatting method that is called and on the content of the format item, as follows:
如果格式项不包含格式字符串 (例如,如果仅) 格式项
{0}
,则会将其null
作为参数的值传递 System.String 。If the format item does not include a format string (for example, if the format item is simply{0}
), it is passednull
as the value of the System.String parameter.如果格式项包含格式字符串 (例如
{0:G}
) ,则该格式字符串将作为参数的值传递 System.String 。If the format item includes a format string (for example,{0:G}
), that format string is passed as the value of the System.String parameter.如果原始方法调用不包含 System.IFormatProvider 参数, CultureInfo.CurrentCulture 则将作为参数的值传递 System.IFormatProvider 。If the original method call does not include an System.IFormatProvider parameter, CultureInfo.CurrentCulture is passed as the value of the System.IFormatProvider parameter.
如果原始方法调用包含 System.IFormatProvider 参数,则方法调用中提供的提供程序将作为参数的值传递 System.IFormatProvider 。If the original method call includes an System.IFormatProvider parameter, the provider that is supplied in the method call is passed as the value of the System.IFormatProvider parameter.
备注
ToString仅当对象未传递 ICustomFormatter 格式提供程序或 Format 自定义格式提供程序的方法返回时,复合格式设置方法才能调用该对象的实现 null
。An object's ToString implementation is called by composite formatting methods only if they are not passed an ICustomFormatter format provider, or if the Format method of the custom format provider returns null
.
.NET Framework 包括三种格式提供程序,它们都实现 IFormatProvider 接口:The .NET Framework includes three format providers, all of which implement the IFormatProvider interface:
NumberFormatInfo 提供数字格式设置信息,如用于小数点和组分隔符的字符,以及货币值中的货币符号的拼写和位置。NumberFormatInfo supplies numeric formatting information, such as the characters to use for decimal and group separators, and the spelling and placement of currency symbols in monetary values.
DateTimeFormatInfo 提供日期相关和与时间相关的格式设置信息,例如日期模式中月份、日期和年份的位置。DateTimeFormatInfo supplies date-related and time-related formatting information, such as the position of the month, the day, and the year in a date pattern.
CultureInfo 包含特定区域性中的默认格式设置信息,包括数字格式信息以及与日期相关的格式设置信息。CultureInfo contains the default formatting information in a specific culture, including the numeric format information, and date-related and time-related formatting information.
此外,还可以定义自己的自定义格式提供程序。In addition, you can define your own custom format provider.
实施者说明
ToString(String, IFormatProvider)方法必须支持 (常规) 格式说明符的 "G"。The ToString(String, IFormatProvider) method 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.