Advertisement
Guest User

Numbers To Words

a guest
Feb 20th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace NumToWords
  6. {
  7.     class Program
  8.     {
  9.         // PROGRAM HANDLES NEGATIVE AND POSITIVE DOUBLES
  10.  
  11.  
  12.         static String NumWordsWrapper(double n)
  13.         {
  14.             string words = "";
  15.             double intPart;
  16.             double decPart = 0;
  17.             if (n == 0)
  18.                 return "zero";
  19.             try
  20.             {
  21.                 string[] splitter = n.ToString().Split('.');
  22.                 intPart = double.Parse(splitter[0]);
  23.                 decPart = double.Parse(splitter[1]);
  24.             }
  25.             catch
  26.             {
  27.                 intPart = n;
  28.             }
  29.  
  30.             words = NumWords(intPart);
  31.  
  32.             if (decPart > 0)
  33.             {
  34.                 if (words != "")
  35.                     words += " and ";
  36.                 int counter = decPart.ToString().Length;
  37.                 switch (counter)
  38.                 {
  39.                     case 1: words += NumWords(decPart) + " tenths"; break;
  40.                     case 2: words += NumWords(decPart) + " hundredths"; break;
  41.                     case 3: words += NumWords(decPart) + " thousandths"; break;
  42.                     case 4: words += NumWords(decPart) + " ten-thousandths"; break;
  43.                     case 5: words += NumWords(decPart) + " hundred-thousandths"; break;
  44.                     case 6: words += NumWords(decPart) + " millionths"; break;
  45.                     case 7: words += NumWords(decPart) + " ten-millionths"; break;
  46.                 }
  47.             }
  48.             return words;
  49.         }
  50.  
  51.         static String NumWords(double n) //converts double to words
  52.         {
  53.             string[] numbersArr = new string[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
  54.             string[] tensArr = new string[] { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninty" };
  55.             string[] suffixesArr = new string[] { "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "Quattuordecillion", "Quindecillion", "Sexdecillion", "Septdecillion", "Octodecillion"  , "Novemdecillion", "Vigintillion" };
  56.             string words = "";
  57.  
  58.             bool tens = false;
  59.  
  60.             if (n < 0)
  61.             {
  62.                 words += "negative ";
  63.                 n *= -1;
  64.             }
  65.  
  66.             int power = (suffixesArr.Length + 1) * 3;
  67.  
  68.             while (power > 3)
  69.             {
  70.                 double pow = Math.Pow(10, power);
  71.                 if (n >= pow)
  72.                 {
  73.                     if (n % pow > 0)
  74.                     {
  75.                         words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1] + ", ";
  76.                     }
  77.                     else if (n % pow == 0)
  78.                     {
  79.                         words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1];
  80.                     }
  81.                     n %= pow;
  82.                 }
  83.                 power -= 3;
  84.             }
  85.             if (n >= 1000)
  86.             {
  87.                 if (n % 1000 > 0) words += NumWords(Math.Floor(n / 1000)) + " thousand, ";
  88.                 else words += NumWords(Math.Floor(n / 1000)) + " thousand";
  89.                 n %= 1000;
  90.             }
  91.             if (0 <= n && n <= 999)
  92.             {
  93.                 if ((int)n / 100 > 0)
  94.                 {
  95.                     words += NumWords(Math.Floor(n / 100)) + " hundred";
  96.                     n %= 100;
  97.                 }
  98.                 if ((int)n / 10 > 1)
  99.                 {
  100.                     if (words != "")
  101.                         words += " ";
  102.                     words += tensArr[(int)n / 10 - 2];
  103.                     tens = true;
  104.                     n %= 10;
  105.                 }
  106.  
  107.                 if (n < 20 && n > 0)
  108.                 {
  109.                     if (words != "" && tens == false)
  110.                         words += " ";
  111.                     words += (tens ? "-" + numbersArr[(int)n - 1] : numbersArr[(int)n - 1]);
  112.                     n -= Math.Floor(n);
  113.                 }
  114.             }
  115.  
  116.             return words;
  117.  
  118.         }
  119.         static void Main(string[] args)
  120.         {
  121.             Console.Write("Enter a number to convert to words: ");
  122.             Double n = Double.Parse(Console.ReadLine());
  123.  
  124.             Console.WriteLine("{0}", NumWordsWrapper(n));
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement