Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class DigitAsWord
- {
- static void Main()
- {
- // intro
- string intro = @"This program asks for a number (0-9),
- including 2.8 (rounded to one digit after the decimal point)
- and depending on the input, shows the digits as a words.";
- Console.WriteLine(intro);
- // declarations
- string input;
- double number;
- // input
- do // validating input for a type double number, between 0 and 9
- {
- Console.Write("\nPlease, enter a number (0-9): ");
- input = Console.ReadLine();
- } while (!double.TryParse(input, out number) || number < 0 || number > 9);
- // logic
- // rounding number to 1 digit after the decimal point
- // so that we can work with digits only on both sides of the decimal point
- number = Math.Round(number, 1, MidpointRounding.AwayFromZero);
- // now we are positive that we have exactly 3 symbols:
- // one digit before the decimal point, one decimal point, one digit after the decimal point
- string temp = input.ToString(); // converting back to string, so that we can name the decimal point as well
- Console.WriteLine();
- // applying the DigitName() method to the first digit (index 0 in the string)
- DigitName(temp[0] - '0'); // char - '0' converts the respective char to int (digit)
- Console.Write("point "); // naming the decimal point
- DigitName(temp[2] - '0'); // naming the digit after the decimal point
- Console.WriteLine("\n");
- }
- private static void DigitName(int digit)
- {
- switch (digit)
- {
- case 0:
- {
- Console.Write("zero ");
- break;
- }
- case 1:
- {
- Console.Write("one ");
- break;
- }
- case 2:
- {
- Console.Write("two ");
- break;
- }
- case 3:
- {
- Console.Write("three ");
- break;
- }
- case 4:
- {
- Console.Write("four ");
- break;
- }
- case 5:
- {
- Console.Write("five ");
- break;
- }
- case 6:
- {
- Console.Write("six ");
- break;
- }
- case 7:
- {
- Console.WriteLine("seven ");
- break;
- }
- case 8:
- {
- Console.Write("eight ");
- break;
- }
- case 9:
- {
- Console.Write("nine ");
- break;
- }
- default:
- {
- Console.WriteLine("not a digit ");
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement