[ create a new paste ] login | about

Link: http://codepad.org/3qVrddCd    [ raw code | output | fork ]

BCS - D, pasted on May 13:
    string Number(ulong i)
    {
        static string[] names = [
          ""[],
          " thousand ",
          " million ",
          " billion ",
          " trillion ",
          " quadrillion ",
          ];
        string ret = "";
        foreach(mult; names)
        {
           if(i%1000 != 0)
               ret = Cent(i%1000) ~ mult ~ ret;
           i /= 1000;
        }
        return ret;
    }

    string Cent(int i)
    {
       static string[] v = 
            [""[], "one", "two", "three", "four", 
            "five", "six", "seven", "eight", "nine"];

       static string[] tens = 
            ["!"[], "!", "twenty", "thirty", "forty", 
            "fifty", "sixty", "seventy", "eighty", "ninety"];

       string p1, p2, p3;
       

       if(i >= 100)
       {
          p1 = v[i/100] ~ " hundred";
          p3 = (i % 100 != 0) ? " and " : "";
       }
       else
          p1 = "";

       i %= 100;
       switch(i)
       {
           case 0, 1, 2, 3, 4, 5, 6, 7, 8, 9:
              p2 = v[i];
              break;

           case 10: p2 = "ten"; break;
           case 11: p2 = "eleven"; break;
           case 12: p2 = "twelve"; break;
           case 13: p2 = "thirteen"; break;
           case 14: p2 = "fourteen"; break;
           case 15: p2 = "fifteen"; break;
           case 16: p2 = "sixteen"; break;
           case 17: p2 = "seventeen"; break;
           case 18: p2 = "eighteen"; break;
           case 19: p2 = "nineteen"; break;

           default:
               p2 = tens[i/10] ~ " " ~ v[i%10];
               break;

       }

       return p1 ~ p3 ~ p2;
    }

import std.stdio;
void main()
{
  writef("%s\n", Number(8_000_400_213));
}


Output:
1
eight billion four hundred thousand two hundred and thirteen


Create a new paste based on this one


Comments: