Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sing System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Humanizer; //@#$! PROJECT EULER!!!!
- /*
- * Write a program that prints out check amounts in words:
- * For example $1,234 would print as:
- * “one thousand two hundred thirty four dollars”.
- * $9,017 would print as: "nine thousand seventeen dollars".
- * Handle amounts up to $9,999. Disregard printing cents.
- * Suggestion: try using arrays instead of lengthy "if" statements.
- * Remember, the less code you write, the less to debug.
- */
- namespace ConvertDigitsToWords {
- public class Program {
- public static void Main(string[] args) {
- //TODO: Add error handleing here, or change to Int32.TryParse
- //If I can remember how the @#$%! to use it.
- string input;
- uint number;
- bool isValid;
- Console.WriteLine("\nEnter 0 to quit the program at any time\n");
- do {
- Console.WriteLine("Enter integer greater than 0");
- input = Console.ReadLine();
- isValid = uint.TryParse(input, out number);
- if (!isValid) {
- Console.WriteLine("\n Not an Integer, Please try again\n");
- }
- else {
- //Console.WriteLine("\n {0}\n", NumberToText(number));
- Humanizer.NumberToWordsExtension.ToWords((int)number);
- //Yes I cheated, Kept getting an Index out of bounds
- //exception explanation below where the excepton has happened.
- }//end if
- } while (!(isValid && number == 0));
- Console.ReadLine();
- } //End Main
- public static string NumberToText(uint number) {
- if (number == 0)
- return "Zero";
- uint[] num = new uint[4];
- uint first = 0;
- uint u, h, t;
- StringBuilder sb = new StringBuilder();
- string[] words0 = { "", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine " };
- string[] words1 = { "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Ninteen " };
- string[] words2 = { "Twenty ", "Thirty ", "Fourty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety " };
- string[] words3 = { "Thousand ", "Million ", "Billion " };
- num[0] = number % 1000; //units
- num[1] = number / 1000;
- num[2] = number / 1000000;
- num[1] = num[1] - 1000 * num[2]; //thousands
- num[3] = number / 1000000000; //billions
- num[2] = num[2] - 1000 * num[3]; //millions
- for (uint i = 3; i > 0; i--) {
- if (num[1] != 0) {
- /*
- *
- * Getting IndexOutOutOfBounds
- * Exception Here, if you could please
- * Help explain the logic behind this
- * I would apprecaiate it
- * I've used Step into several
- * times on the
- * debugger and it seems like
- * for some reason first
- * gets set to number
- * instead of "i"
- */
- first = i;
- break;
- }//end if
- }//end for
- for (uint i = first; i >= 0; i--) {
- if (num[i] == 0) { continue; }
- u = num[i] % 10; // ones
- t = num[i] / 10;
- h = num[i] / 100; //hundreds
- t = t - 10 * h; //tens
- if (h > 0) {
- sb.Append(words0[h] + "Hundred ");
- }//end if
- if (u > 0 || t > 0) {
- if (t == 0)
- sb.Append(words0[u]);
- else if (t == 1)
- sb.Append(words1[u]);
- else
- sb.Append(words2[t - 2] + words0[u]);
- } //end if
- if (i != 0) {
- sb.Append(words3[i - 1]);
- }//end if
- }//End For
- return sb.ToString().TrimEnd();
- } //end Number to Text
- } //end Class
- } //end Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement