Convert Number to word - CSharp Language Basics

CSharp examples for Language Basics:switch

Description

Convert Number to word

Demo Code

using System;//from ww  w .j  a va  2 s. c o  m
class NumberProcessor
{
   public static void NumberToWord(int number)
   {
      if(number >= 10)
         NumberToWord(number / 10);
      Console.Write(DigitToText(number % 10) + " ");
   }
   public static string DigitToText(int digit)
   {
      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";
         case 9:
             return "nine";
         default:
             Console.WriteLine("Can only process single digits");
             return "Error";
      }
   }
}
class Tester
{
   public static void Main()
   {
      NumberProcessor.NumberToWord(3426);
   }
}

Result


Related Tutorials