Advertisement
Braber01

Help I'm tearing my hair out

Sep 15th, 2016
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.51 KB | None | 0 0
  1. sing System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Humanizer; //@#$! PROJECT EULER!!!!
  7.                  /*
  8.                   * Write a program that prints out check amounts in words:
  9.                   * For example $1,234 would print as:
  10.                   * “one thousand two hundred thirty four dollars”.
  11.                   * $9,017 would print as: "nine thousand seventeen dollars".
  12.                   * Handle amounts up to $9,999.  Disregard printing cents.
  13.                   * Suggestion: try using arrays instead of lengthy "if" statements.
  14.                   * Remember, the less code you write, the less to debug.
  15.                  */
  16.  
  17.  
  18.  
  19.  
  20. namespace ConvertDigitsToWords {
  21.     public class Program {
  22.  
  23.         public static void Main(string[] args) {
  24.             //TODO: Add error handleing here, or change to Int32.TryParse
  25.             //If I can remember how the @#$%! to use it.
  26.  
  27.             string input;
  28.             uint number;
  29.             bool isValid;
  30.             Console.WriteLine("\nEnter 0 to quit the program at any time\n");
  31.  
  32.             do {
  33.                 Console.WriteLine("Enter integer greater than 0");
  34.                 input = Console.ReadLine();
  35.                 isValid = uint.TryParse(input, out number);
  36.  
  37.                 if (!isValid) {
  38.                     Console.WriteLine("\n Not an Integer, Please try again\n");
  39.                 }
  40.                 else {
  41.                     //Console.WriteLine("\n {0}\n", NumberToText(number));
  42.                     Humanizer.NumberToWordsExtension.ToWords((int)number);
  43.                     //Yes I cheated, Kept getting an Index out of bounds
  44.                     //exception explanation below where the excepton has happened.
  45.                 }//end if
  46.             } while (!(isValid && number == 0));
  47.  
  48.             Console.ReadLine();
  49.         } //End Main
  50.  
  51.         public static string NumberToText(uint number) {
  52.             if (number == 0)
  53.                 return "Zero";
  54.             uint[] num = new uint[4];
  55.             uint first = 0;
  56.             uint u, h, t;
  57.             StringBuilder sb = new StringBuilder();
  58.             string[] words0 = { "", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine " };
  59.             string[] words1 = { "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Ninteen " };
  60.             string[] words2 = { "Twenty ", "Thirty ", "Fourty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety " };
  61.             string[] words3 = { "Thousand ", "Million ", "Billion " };
  62.  
  63.             num[0] = number % 1000; //units
  64.             num[1] = number / 1000;
  65.             num[2] = number / 1000000;
  66.             num[1] = num[1] - 1000 * num[2]; //thousands
  67.             num[3] = number / 1000000000; //billions
  68.             num[2] = num[2] - 1000 * num[3]; //millions
  69.  
  70.             for (uint i = 3; i > 0; i--) {
  71.                 if (num[1] != 0) {
  72.                     /*
  73.                      *
  74.                      * Getting IndexOutOutOfBounds
  75.                      * Exception Here, if you could please
  76.                      * Help explain the logic behind this
  77.                      * I would apprecaiate it
  78.                      * I've used Step into several
  79.                      * times on the
  80.                      * debugger and it seems like
  81.                      * for some reason first
  82.                      * gets set to number
  83.                      * instead of "i"
  84.                      */
  85.                     first = i;
  86.                     break;
  87.                 }//end if
  88.             }//end for
  89.  
  90.             for (uint i = first; i >= 0; i--) {
  91.                 if (num[i] == 0) { continue; }
  92.                 u = num[i] % 10; // ones
  93.                 t = num[i] / 10;
  94.                 h = num[i] / 100; //hundreds
  95.                 t = t - 10 * h; //tens
  96.                 if (h > 0) {
  97.                     sb.Append(words0[h] + "Hundred ");
  98.                 }//end if
  99.                 if (u > 0 || t > 0) {
  100.                     if (t == 0)
  101.                         sb.Append(words0[u]);
  102.                     else if (t == 1)
  103.                         sb.Append(words1[u]);
  104.                     else
  105.                         sb.Append(words2[t - 2] + words0[u]);
  106.                 } //end if
  107.                 if (i != 0) {
  108.                     sb.Append(words3[i - 1]);
  109.                 }//end if
  110.             }//End For
  111.             return sb.ToString().TrimEnd();
  112.         } //end Number to Text
  113.     } //end Class
  114. } //end Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement