1

I need to convert Day of the Month to how a person would say it.

For instance 4/26 would be spoken as Twenty-Sixth. 4/01 would be spoken as First.

I know I could use a look up table string foo = {"First", "Second", ...} then take the day of the Month number and pull out the string.

Is there a better way to do this?

jpiccolo
  • 768
  • 1
  • 6
  • 19
  • http://stackoverflow.com/questions/2729752/converting-numbers-in-to-words-c-sharp –  Apr 26 '12 at 18:33
  • I know you said you don't want to use a lookup table, but my opinion is a couple `Dictionary`s would be perfect. One for Month, one for Day. – Khan Apr 26 '12 at 18:34
  • @Jeff month name is built in to the DateTime class already – Muad'Dib Apr 26 '12 at 18:36
  • 1
    @Muad'Dib Then even better, you should only need a 31 length dictionary for day. ;) – Khan Apr 26 '12 at 18:37
  • 1
    How do you want to use it? Maybe an [enum](http://msdn.microsoft.com/en-us/library/cc138362.aspx) will help you. – chaliasos Apr 26 '12 at 18:39

2 Answers2

3

In general, yes, you can encode the rules of English to produce ordinal numbers. However, the first nineteen words will inevitably end up in a lookup table, because they are exceptions.

In case of numbering days of the month, the range of exceptional values (1 through 19) covers roughly 60% of the total number of word sequences that you need to produce, so it would make sense to skip the algorithm altogether, and put everything in a lookup table. This would improve readability, and simplify internationalization in case you decide to support languages other than English.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

There's no way around a lookup table (even if it's provided by a third party). But you can reduce the number of cases:

  • One entry for numbers 1-20 and 30 (as spoken in the date).
  • The missing numbers can be combined, e.g. using 20 + 1, 20 + 2, 30 + 1, etc.
Mario
  • 35,726
  • 5
  • 62
  • 78