1

I have a console application which asks a user to input a number using digits (1-9 and 0). I was wondering if there is a way where I can then convert that digit to a string of text.

Thanks. I have found some code online (here), but I am unsure of how to implement most of it to a console app.

Community
  • 1
  • 1
Vlad781
  • 37
  • 1
  • 2
  • 7
  • 1
    possible duplicate of [Convert integers to written numbers](http://stackoverflow.com/questions/3213/convert-integers-to-written-numbers) – Shai Jul 23 '12 at 08:34
  • 1
    Seems to me that what you've linked to has everything you need. `string` works fine in console applications, funnily enough. – Marc Gravell Jul 23 '12 at 08:34
  • 2
    The code you linked to looks quite useful. Where are you having trouble implementing it? (The Stackoverflowers will gladly help you with specific problems, but they won't write your application for you.) – Heinzi Jul 23 '12 at 08:35

2 Answers2

1

I'd write a function

string DigitToText(int digit)
{
   if (digit < 0 || digit > 9)
   {
       throw new ArgumentOutOfRangeException(
           "digit", 
           "digit must be between 0 and 9");
   }

   switch(digit)
   {
       case 0:
           return "zero";

       case 1:
           return "one";

       case 2:
           return "two";

       case 3:
           return "three";

       case 4:
           return "four";

       case 5:
           return "five";

       case 6:
           return "six";

       case 7:
           return "seven";

       case 8:
           return "eight";

       default:
           return "nine";
   }
}

Using a switch statement will save lots uf unnesscessary instantiation of arrays and although this may look verbose I think the resulting IL will be efficient.

Jodrell
  • 34,946
  • 5
  • 87
  • 124
0

The code you found there works irrelevant of the type of application. Just add the class there to your project and use it.

Joey
  • 344,408
  • 85
  • 689
  • 683