本文整理汇总了C#中System.IFormattable接口的典型用法代码示例。如果您正苦于以下问题:C# IFormattable接口的具体用法?C# IFormattable怎么用?C# IFormattable使用的例子?那么恭喜您, 这里精选的接口代码示例或许可以为您提供帮助。
IFormattable接口属于System命名空间,在下文中一共展示了IFormattable接口的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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));
}
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:60,代码来源:IFormattable
示例2: Main
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));
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:28,代码来源:IFormattable 输出:
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)
注:本文中的System.IFormattable接口示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论