Advertisement
Guest User

numToWord.d

a guest
Sep 6th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.23 KB | None | 0 0
  1. import std.stdio;
  2. import std.string;
  3. import std.conv;
  4.  
  5. //Splits the number where the comma would be
  6. //123,456 -> [123, 456]
  7. //1,234 -> [1, 234]
  8. string[] splitNum(long toSplit)
  9. {
  10.     string[] ret;
  11.     while(toSplit)
  12.     {
  13.         ret ~= to!string(toSplit % 1000);
  14.         toSplit /= 1000;
  15.     }
  16.  
  17.     return ret.reverse;
  18. }
  19.  
  20. string wordify(long num)
  21. {
  22.     writeln(num);
  23.  
  24.     string[] digits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
  25.     if(num >= 0 && num <= 9)
  26.     {
  27.         return digits[num];
  28.     }
  29.    
  30.     string[] mods = ["", "thousand", "million", "billion", "trillion"];
  31.     string[] teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
  32.     string[] tens = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];
  33.    
  34.     string[] stringedNum = splitNum(num);
  35.     string ret;
  36.     int count = cast(int) stringedNum.length - 1;
  37.    
  38.     foreach(int i, string word; stringedNum)
  39.     {
  40.         //wordifies first digit of three digit number
  41.         //123 -> one hundred and
  42.         if(word.length == 3)
  43.         {
  44.             ret ~= digits[word[0] - '0'] ~ " hundred and ";
  45.             word = word[1..$];
  46.         }
  47.  
  48.         //for two digit numbers
  49.         if(word.length == 2)
  50.         {
  51.             int index = word[1] - '0';
  52.             //for num >= 10 && num <= 19
  53.             if(word[0] != '0' && word[0] == '1')
  54.             {
  55.                 ret ~= teens[index] ~ " ";
  56.             }
  57.             //for num >= 20 && <= 99
  58.             else if(word[0] != '0' && word[0] != '1')
  59.             {  
  60.                 ret ~= tens[word[0] - '0' - 2];
  61.                 if(word[1] - '0')
  62.                 {
  63.                     ret ~= "-" ~ digits[index];
  64.                 }
  65.                 ret ~= " ";
  66.             }
  67.             //for num is a three digit number, and the middle digit is a zero
  68.             else
  69.             {
  70.                 ret ~= digits[index] ~ " ";
  71.             }
  72.         }
  73.         //single digit numbers
  74.         else
  75.         {
  76.             ret ~= digits[word[0] - '0'] ~ " ";
  77.         }
  78.  
  79.         if(i + 1 == stringedNum.length) { break; }
  80.  
  81.         ret ~= mods[count--] ~ ", ";
  82.     }
  83.  
  84.     return ret;
  85. }
  86.  
  87. void main(string[] args)
  88. {
  89.     writeln(wordify(1));
  90.     writeln(wordify(123));
  91.     writeln(wordify(1765));
  92.     writeln(wordify(9843421));
  93.     writeln(wordify(594956191569));
  94.     writeln(wordify(9844513561589));
  95.     writeln(wordify(10960506329090));
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement