Advertisement
Guest User

Digit as Number

a guest
Apr 2nd, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.15 KB | None | 0 0
  1. using System;
  2.  
  3. class DigitAsWord
  4. {
  5.     static void Main()
  6.     {
  7.         // intro
  8.         string intro = @"This program asks for a number (0-9),
  9. including 2.8 (rounded to one digit after the decimal point)
  10. and depending on the input, shows the digits as a words.";
  11.         Console.WriteLine(intro);
  12.  
  13.     // declarations        
  14.         string input;
  15.         double number;
  16.  
  17.     // input
  18.         do // validating input for a type double number, between 0 and 9
  19.         {
  20.             Console.Write("\nPlease, enter a number (0-9): ");
  21.             input = Console.ReadLine();
  22.  
  23.         } while (!double.TryParse(input, out number) || number < 0 || number > 9);
  24.    
  25.     // logic        
  26.         // rounding number to 1 digit after the decimal point
  27.         // so that we can work with digits only on both sides of the decimal point
  28.         number = Math.Round(number, 1, MidpointRounding.AwayFromZero);
  29.  
  30.         // now we are positive that we have exactly 3 symbols:
  31.         // one digit before the decimal point, one decimal point, one digit after the decimal point
  32.         string temp = input.ToString(); // converting back to string, so that we can name the decimal point as well
  33.  
  34.         Console.WriteLine();
  35.  
  36.         // applying the DigitName() method to the first digit (index 0 in the string)
  37.         DigitName(temp[0] - '0'); // char - '0' converts the respective char to int (digit)
  38.         Console.Write("point "); // naming the decimal point
  39.         DigitName(temp[2] - '0'); // naming the digit after the decimal point
  40.  
  41.         Console.WriteLine("\n");        
  42.     }
  43.  
  44.     private static void DigitName(int digit)
  45.     {
  46.  
  47.         switch (digit)
  48.         {
  49.             case 0:
  50.                 {
  51.                     Console.Write("zero ");
  52.                     break;
  53.                 }
  54.  
  55.             case 1:
  56.                 {
  57.                     Console.Write("one ");
  58.                     break;
  59.                 }
  60.  
  61.             case 2:
  62.                 {
  63.                     Console.Write("two ");
  64.                     break;
  65.                 }
  66.  
  67.             case 3:
  68.                 {
  69.                     Console.Write("three ");
  70.                     break;
  71.                 }
  72.  
  73.             case 4:
  74.                 {
  75.                     Console.Write("four ");
  76.                     break;
  77.                 }
  78.  
  79.             case 5:
  80.                 {
  81.                     Console.Write("five ");
  82.                     break;
  83.                 }
  84.  
  85.             case 6:
  86.                 {
  87.                     Console.Write("six ");
  88.                     break;
  89.                 }
  90.  
  91.             case 7:
  92.                 {
  93.                     Console.WriteLine("seven ");
  94.                     break;
  95.                 }
  96.  
  97.             case 8:
  98.                 {
  99.                     Console.Write("eight ");
  100.                     break;
  101.                 }
  102.  
  103.             case 9:
  104.                 {
  105.                     Console.Write("nine ");
  106.                     break;
  107.                 }
  108.  
  109.             default:
  110.                 {
  111.                     Console.WriteLine("not a digit ");
  112.                     break;
  113.                 }
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement