0

I would like to convert an integer into a word in the form of:

  • 1 => First
  • 2 => Second
  • 3 => Third

etc.

I have tried googling and have found answers on how to convert ints to plain english words like one, two, three etc. Does anyone know if there are any methods out there that do this already? I'd like to avoid writing my own if possible but I think that's what it will come to.

  • I think this is a duplicate of several others, that may be helpful to you: http://stackoverflow.com/questions/3911966/how-to-convert-number-to-words-in-java, http://stackoverflow.com/questions/2729752/converting-numbers-in-to-words-c-sharp, http://stackoverflow.com/questions/554314/how-can-i-convert-an-integer-into-its-verbal-representation – Alex May 12 '15 at 23:27

2 Answers2

3

No, a mapping like that does not currently exist in C# (apart from any third party libraries of course).

Easy enough to do though:

Dictionary<int, string> specialNumberNames = new Dictionary<int, string>()
{
   {1, "First"},
   {2, "Second"},
   ...
}

int number = 1;
string specialName = specialNumberNames[number];
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
0

If you're open to using a third-party library, you can use Humanizer. Sample usage from the link:

0.ToOrdinalWords() => "zeroth"
1.ToOrdinalWords() => "first"
2.ToOrdinalWords() => "second"
8.ToOrdinalWords() => "eighth"
10.ToOrdinalWords() => "tenth"
11.ToOrdinalWords() => "eleventh"
12.ToOrdinalWords() => "twelfth"
20.ToOrdinalWords() => "twentieth"
21.ToOrdinalWords() => "twenty first"
121.ToOrdinalWords() => "hundred and twenty first"
Ilian
  • 5,113
  • 1
  • 32
  • 41