|
Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações.
|
Tradução
Original
|
Interface IFormatProvider
Namespace: System
Assembly: mscorlib (em mscorlib.dll)
O tipo IFormatProvider expõe os membros a seguir.
O NumberFormatInfo classe, que fornece informações que são usadas para formatar números, como, por exemplo, a moeda, milhares separador e símbolos do separador decimal para uma determinada cultura. O DateTimeFormatInfo classe, que fornece informações que são usadas para formatar datas e horas, como, por exemplo, os símbolos de separador de data e hora para uma determinada cultura ou a ordem e a forma de componentes do dia, mês e ano uma data. O CultureInfo classe que representa uma determinada cultura. Sua GetFormat método retorna um culture-specific NumberFormatInfo ou DateTimeFormatInfo o objeto, dependendo se a CultureInfo objeto é usado em uma formatação ou análise de operação que envolva números ou datas e horas.
using System; using System.Globalization; public class Example { public static void Main() { DateTime dateValue = new DateTime(2009, 6, 1, 4, 37, 0); CultureInfo[] cultures = { new CultureInfo("en-US"), new CultureInfo("fr-FR"), new CultureInfo("it-IT"), new CultureInfo("de-DE") }; foreach (CultureInfo culture in cultures) Console.WriteLine("{0}: {1}", culture.Name, dateValue.ToString(culture)); } } // The example displays the following output: // en-US: 6/1/2009 4:37:00 PM // fr-FR: 01/06/2009 16:37:00 // it-IT: 01/06/2009 16.37.00 // de-DE: 01.06.2009 16:37:00
public class AcctNumberFormat : IFormatProvider, ICustomFormatter { private const int ACCT_LENGTH = 12; public object GetFormat(Type formatType) { if (formatType == typeof(ICustomFormatter)) return this; else return null; } public string Format(string fmt, object arg, IFormatProvider formatProvider) { // Provide default formatting if arg is not an Int64. if (arg.GetType() != typeof(Int64)) try { return HandleOtherFormats(fmt, arg); } catch (FormatException e) { throw new FormatException(String.Format("The format of '{0}' is invalid.", fmt), e); } // Provide default formatting for unsupported format strings. string ufmt = fmt.ToUpper(CultureInfo.InvariantCulture); if (! (ufmt == "H" || ufmt == "I")) try { return HandleOtherFormats(fmt, arg); } catch (FormatException e) { throw new FormatException(String.Format("The format of '{0}' is invalid.", fmt), e); } // Convert argument to a string. string result = arg.ToString(); // If account number is less than 12 characters, pad with leading zeroes. if (result.Length < ACCT_LENGTH) result = result.PadLeft(ACCT_LENGTH, '0'); // If account number is more than 12 characters, truncate to 12 characters. if (result.Length > ACCT_LENGTH) result = result.Substring(0, ACCT_LENGTH); if (ufmt == "I") // Integer-only format. return result; // Add hyphens for H format specifier. else // Hyphenated format. return result.Substring(0, 5) + "-" + result.Substring(5, 3) + "-" + result.Substring(8); } private string HandleOtherFormats(string format, object arg) { if (arg is IFormattable) return ((IFormattable)arg).ToString(format, CultureInfo.CurrentCulture); else if (arg != null) return arg.ToString(); else return String.Empty; } }
using System; using System.Globalization; public enum DaysOfWeek { Monday=1, Tuesday=2 }; public class TestFormatting { public static void Main() { long acctNumber; double balance; DaysOfWeek wday; string output; acctNumber = 104254567890; balance = 16.34; wday = DaysOfWeek.Monday; output = String.Format(new AcctNumberFormat(), "On {2}, the balance of account {0:H} was {1:C2}.", acctNumber, balance, wday); Console.WriteLine(output); wday = DaysOfWeek.Tuesday; output = String.Format(new AcctNumberFormat(), "On {2}, the balance of account {0:I} was {1:C2}.", acctNumber, balance, wday); Console.WriteLine(output); } } // The example displays the following output: // On Monday, the balance of account 10425-456-7890 was $16.34. // On Tuesday, the balance of account 104254567890 was $16.34.
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Função Server Core sem suporte), Windows Server 2008 R2 (Função Server Core com suporte com o SP1 ou posterior, Itanium sem suporte)
O .NET Framework não oferece suporte a todas as versões de cada plataforma. Para obter uma lista das versões com suporte, consulte .Requisitos de sistema do NET Framework.


