本文整理匯總了C#中System.IFormattable.ToString方法的典型用法代碼示例。如果您正苦於以下問題:C# IFormattable.ToString方法的具體用法?C# IFormattable.ToString怎麽用?C# IFormattable.ToString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.IFormattable
的用法示例。
在下文中一共展示了IFormattable.ToString方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Temperature
//引入命名空間
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));
}
}
}