|
Este artículo proviene de un motor de traducción automática. Mueva el puntero sobre las frases del artículo para ver el texto original. Más información.
|
Traducción
Original
|
IFormattable (Interfaz)
Espacio de nombres: System
Ensamblado: mscorlib (en mscorlib.dll)
El tipo IFormattable expone los siguientes miembros.
Cadenas con formato estándar para dar formato a valores de enumeración (vea Cadenas de formato de enumeración). Cadenas con formato personalizado y estándar para dar formato a valores numéricos (vea Cadenas con formato numérico estándar y Cadenas con formato numérico personalizado). Cadenas con formato estándar y personalizado para dar formato a los valores de fecha y hora, (vea Cadenas con formato de fecha y hora estándar y Cadenas de formato de fecha y hora personalizadas). Cadenas con formato estándar y personalizado para dar formato a los intervalos de tiempo, (vea Cadenas de formato TimeSpan estándar y Cadenas de formato TimeSpan personalizado).
La clase System.Globalization.CultureInfo, que devuelve un objeto NumberFormatInfo para dar formato a valores numéricos o un objeto DateTimeFormatInfo para dar formato a valores de fecha y hora. La clase System.Globalization.NumberFormatInfo, que devuelve una instancia de sí misma para dar formato a valores numéricos. La clase System.Globalization.DateTimeFormatInfo, que devuelve una instancia de sí misma para dar formato a valores de fecha y hora.
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)); } } }
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)
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (no se admite el rol Server Core), Windows Server 2008 R2 (se admite el rol Server Core con SP1 o versiones posteriores; no se admite Itanium)
.NET Framework no admite todas las versiones de todas las plataformas. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.


