using System; namespace Quadarax.Foundation.Core.Value.Formatters { /// /// Formats a numeric value based on a format P:Plural:Singular /// public class PluralFormatter : ICustomFormatter, IFormatProvider { public string Format(string format, object arg, IFormatProvider formatProvider) { if (arg !=null) { var parts = format.Split(':'); // ["P", "Plural", "Singular"] if (parts[0] == "P") // correct format? { // which index postion to use int partIndex = (arg.ToString() == "1")?2:1; // pick string (safe guard for array bounds) and format return String.Format("{0} {1}", arg, (parts.Length>partIndex?parts[partIndex]:"")); } } return String.Format(format, arg); } public object GetFormat(Type formatType) { return formatType == typeof(ICustomFormatter)?this:null; } } }