Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- import std.string;
- import std.conv;
- //Splits the number where the comma would be
- //123,456 -> [123, 456]
- //1,234 -> [1, 234]
- string[] splitNum(long toSplit)
- {
- string[] ret;
- while(toSplit)
- {
- ret ~= to!string(toSplit % 1000);
- toSplit /= 1000;
- }
- return ret.reverse;
- }
- string wordify(long num)
- {
- writeln(num);
- string[] digits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
- if(num >= 0 && num <= 9)
- {
- return digits[num];
- }
- string[] mods = ["", "thousand", "million", "billion", "trillion"];
- string[] teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
- string[] tens = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];
- string[] stringedNum = splitNum(num);
- string ret;
- int count = cast(int) stringedNum.length - 1;
- foreach(int i, string word; stringedNum)
- {
- //wordifies first digit of three digit number
- //123 -> one hundred and
- if(word.length == 3)
- {
- ret ~= digits[word[0] - '0'] ~ " hundred and ";
- word = word[1..$];
- }
- //for two digit numbers
- if(word.length == 2)
- {
- int index = word[1] - '0';
- //for num >= 10 && num <= 19
- if(word[0] != '0' && word[0] == '1')
- {
- ret ~= teens[index] ~ " ";
- }
- //for num >= 20 && <= 99
- else if(word[0] != '0' && word[0] != '1')
- {
- ret ~= tens[word[0] - '0' - 2];
- if(word[1] - '0')
- {
- ret ~= "-" ~ digits[index];
- }
- ret ~= " ";
- }
- //for num is a three digit number, and the middle digit is a zero
- else
- {
- ret ~= digits[index] ~ " ";
- }
- }
- //single digit numbers
- else
- {
- ret ~= digits[word[0] - '0'] ~ " ";
- }
- if(i + 1 == stringedNum.length) { break; }
- ret ~= mods[count--] ~ ", ";
- }
- return ret;
- }
- void main(string[] args)
- {
- writeln(wordify(1));
- writeln(wordify(123));
- writeln(wordify(1765));
- writeln(wordify(9843421));
- writeln(wordify(594956191569));
- writeln(wordify(9844513561589));
- writeln(wordify(10960506329090));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement