/// <summary> Converts a Customer instance into equal string using formatting.</summary> /// <param name="format"> Format string</param> /// <param name="arg"> Object to format</param> /// <param name="formatProvider"> Object of format provider</param> /// <returns> String format of Customer instance</returns> public string Format(string format, object arg, IFormatProvider formatProvider) { if (!formatProvider.Equals(this)) { throw new ArgumentException(); } if (string.IsNullOrEmpty(format)) { format = "NPR"; } Customer customer = arg as Customer; if (ReferenceEquals(customer, null)) { throw new FormatException(); } switch (format) { case "NPR": return($"Customer record: {customer.Name}, {customer.ContactPhone}, {customer.Revenue:N}"); case "NRP": return($"Customer record: {customer.Name}, {customer.Revenue:N}, {customer.ContactPhone}"); case "N": return($"Customer record: {customer.Name}"); case "Nup": return($"Customer record: {customer.Name.ToUpperInvariant()}"); case "Nlow": return($"Customer record: {customer.Name.ToLowerInvariant()}"); case "P": return($"Customer record: {customer.ContactPhone}"); case "R": return($"Customer record: {customer.Revenue:N}"); case "NP": return($"Customer record: {customer.Name}, {customer.ContactPhone}"); case "NR": return($"Customer record: {customer.Name}, {customer.Revenue:N}"); default: throw new FormatException(); } }
public string Format(string fmt, Object arg, IFormatProvider formatProvider) { // Exit if another format provider is used. if (!formatProvider.Equals(this)) { return(null); } // Exit if the type to be formatted is not a Boolean if (!(arg is Boolean)) { return(null); } bool value = (bool)arg; switch (culture.Name) { case "en-US": return(value.ToString()); case "fr-FR": if (value) { return("vrai"); } else { return("faux"); } case "ru-RU": if (value) { return("верно"); } else { return("неверно"); } default: return(value.ToString()); } }
public string Format(string format, object arg, IFormatProvider formatProvider) { if (!formatProvider.Equals(this)) { return(null); } if (!format.StartsWith("^")) { return(null); } String[] parts = format.Split(new char[] { '^' }); int choice = ((int)arg) == 1 ? 1 : 2; return(String.Format("{0} {1}", arg, parts[choice])); }
/// <summary> /// Converts the value of a specified object to an equivalent string representation using specified format and culture-specific formatting information. /// </summary> /// <param name="format"> A format string containing formatting specifications </param> /// <param name="arg"> An object to format </param> /// <param name="formatProvider"> An object that supplies format information about the current instance </param> /// <returns> The string representation of the value of arg, formatted as specified by format and formatProvider </returns> public string Format(string format, object arg, IFormatProvider formatProvider) { if (!formatProvider.Equals(this)) { return(null); } if (arg is Customer customer) { return($"Customer info -- Name: {customer.Name}, " + $"Contact phone: {customer.ContactPhone}, Revenue: {customer.Revenue}."); } else if (arg != null) { return(arg.ToString()); } else { return(string.Empty); } }
/// <inheritdoc /> public string Format(string format, object arg, IFormatProvider formatProvider) { if (!formatProvider.Equals(this)) { return(null); } if (format.Trim().ToUpper() != SupportedFormat) { return(null); } var book = arg as Book; if (ReferenceEquals(book, null)) { return(null); } return($"ISBN 13: {book.Isbn}, {book.Author}, {book.Title}"); }
public string Format(string format, object arg, IFormatProvider formatProvider) { var timeSpan = arg as TimeSpan?; if (!formatProvider.Equals(this) || timeSpan == null) return null; var days = (int) Math.Floor(timeSpan.Value.TotalDays); var hours = (int) Math.Floor((double) timeSpan.Value.Hours); var minutes = (int) Math.Floor((double) timeSpan.Value.Minutes); var seconds = (int) Math.Floor((double) timeSpan.Value.Seconds); return format .Replace("dd", days.ToString("00")) .Replace("d", days.ToString()) .Replace("hh", hours.ToString("00")) .Replace("h", hours.ToString()) .Replace("mm", minutes.ToString("00")) .Replace("m", minutes.ToString()) .Replace("ss", seconds.ToString("00")) .Replace("s", seconds.ToString()); }
public string Format(string format, object arg, IFormatProvider formatProvider) { if (!formatProvider.Equals(this)) { return(null); } // Handle only hexadecimal format string. if (!format.StartsWith("X")) { return(null); } byte[] bytes; string output = null; // Handle only integral types. if (arg is Byte) { bytes = BitConverter.GetBytes((Byte)arg); } else if (arg is Int16) { bytes = BitConverter.GetBytes((Int16)arg); } else if (arg is Int32) { bytes = BitConverter.GetBytes((Int32)arg); } else if (arg is Int64) { bytes = BitConverter.GetBytes((Int64)arg); } else if (arg is SByte) { bytes = BitConverter.GetBytes((SByte)arg); } else if (arg is UInt16) { bytes = BitConverter.GetBytes((UInt16)arg); } else if (arg is UInt32) { bytes = BitConverter.GetBytes((UInt32)arg); } else if (arg is UInt64) { bytes = BitConverter.GetBytes((UInt64)arg); } else { return(null); } for (int ctr = bytes.Length - 1; ctr >= 0; ctr--) { output += String.Format("{0:X2} ", bytes[ctr]); } return(output.Trim()); }
public string Format(string format, object arg, IFormatProvider formatProvider) { if (!formatProvider.Equals(this)) return null; // Handle only hexadecimal format string. if (!format.StartsWith("X")) return null; byte[] bytes; string output = null; // Handle only integral types. if (arg is byte) bytes = BitConverter.GetBytes((byte)arg); else if (arg is Int16) bytes = BitConverter.GetBytes((Int16)arg); else if (arg is Int32) bytes = BitConverter.GetBytes((Int32)arg); else if (arg is Int64) bytes = BitConverter.GetBytes((Int64)arg); else if (arg is SByte) bytes = BitConverter.GetBytes((SByte)arg); else if (arg is UInt16) bytes = BitConverter.GetBytes((UInt16)arg); else if (arg is UInt32) bytes = BitConverter.GetBytes((UInt32)arg); else if (arg is UInt64) bytes = BitConverter.GetBytes((UInt64)arg); else if (arg is byte[]) bytes = (byte[])arg; else return null; for (int ctr = bytes.Length - 1; ctr >= 0; ctr--) output += String.Format("{0:X2} ", bytes[ctr]); return output.Trim(); }
public string Format(string format, object arg, IFormatProvider formatProvider) { try { if (!formatProvider.Equals(this)) return (null); if (!format.StartsWith("MAC")) return (null); if (arg is UInt64) { return (toString((UInt64)arg)); } else { return (null); } } catch { return (null); } }