Ordinal suffixes with a custom IFormatProvider

Ordinal suffixes for english dates are composed by a number and 2 characters.

Some classes of the framework provide an usueful implementation of the IFormatter provider, like the Datetime class which allow the developer to display the date in various ways, even with ordinal suffixes.

If we  need to display the value of an object in a certain format we can either write an extension method or implement the IFormatProvider interface in conjuction of ICustomFormatter. Considering that an extension method, to be tested, needs to be wrapped in a traditional class and exposed trough a static one (as described here), the amount of code to write is almost the same. The second option appear to be the best choice.

We have an object, it could be an integer or a string, this should be displayed as an ordinal number, for istance 21 will become 21st, the 13 to 13th and so on.

With the right html the ordinal number will be displayed in a nice format. ordinalnew
   1: public class RankValueFormatterPlain : IFormatProvider , ICustomFormatter
   2:    {
   3:
   4:        #region ICustomFormatter Members
   5:
   6:        public string Format(string format, object arg, IFormatProvider formatProvider)
   7:        {
   8:            string suffix = string.Empty;
   9:            string rank = string.Empty;
  10:            if(arg is int || arg is string)
  11:            {
  12:                suffix = string.Empty;
  13:                rank = arg as string;
  14:                if (rank.EndsWith("1"))
  15:                    suffix = "st";
  16:                else if (rank.EndsWith("2"))
  17:                    suffix = "nd";
  18:                else if (rank.EndsWith("3"))
  19:                    suffix = "rd";
  20:                else if (!rank.EndsWith("0"))
  21:                    suffix = "th";
  22:            }
  23:            else
  24:            {
  25:                throw new NotImplementedException();
  26:            }
  27:            return string.Format("{0}{1}", rank, suffix);
  28:        }
  29:
  30:        #endregion
  31:
  32:        #region IFormatProvider Members
  33:
  34:        public object GetFormat(Type formatType)
  35:        {
  36:            return (formatType == typeof(ICustomFormatter)) ? this : null;
  37:        }
  38:
  39:        #endregion
  40:    }
  41:
  42:    public class RankValueFormatterStyledHtml : IFormatProvider, ICustomFormatter
  43:    {
  44:
  45:        #region ICustomFormatter Members
  46:
  47:        public string Format(string format, object arg, IFormatProvider formatProvider)
  48:        {
  49:            string suffix = string.Empty;
  50:            int rank = 0;
  51:            int condition1, condition2;
  52:            if (arg is int || arg is string)
  53:            {
  54:                suffix = string.Empty;
  55:                rank = Convert.ToInt32(arg);
  56:                condition1 = (rank % 100);
  57:                condition2 = (rank % 10);
  58:                if (condition1 == 11 || condition1 == 12 || condition1 == 13)
  59:                {
  60:                    suffix = "th";
  61:                }
  62:                else
  63:                {
  64:                    switch (condition2)
  65:                    {
  66:                        case 1:
  67:                            suffix = "st";
  68:                            break;
  69:
  70:                        case 2:
  71:                            suffix = "nd";
  72:                            break;
  73:
  74:                        case 3:
  75:                            suffix = "rd";
  76:                            break;
  77:
  78:                        default:
  79:                            suffix = "th";
  80:                            break;
  81:                    }
  82:                }
  83:            }
  84:            else
  85:            {
  86:                throw new NotImplementedException();
  87:            }
  88:            return string.Format("<td class=rank>{0}</td> <td class=rankSuffix>{1}</td>", rank, suffix);
  89:        }
  90:
  91:        #endregion
  92:
  93:        #region IFormatProvider Members
  94:
  95:        public object GetFormat(Type formatType)
  96:        {
  97:            return (formatType == typeof(ICustomFormatter)) ? this : null;
  98:        }
  99:
 100:        #endregion
 101:    }



No Comments


You can leave the first : )



Leave a Reply

Your email address will not be published. Required fields are marked *