1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
string numToString(long long n)
{
static string ones[] =
{"one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine "};
static string teens[] =
{"ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "};
static string tens[] =
{"twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety "};
struct Limit {
long long limit;
string name;
};
static Limit limits[] = {
{1000000000000000000LL, " quintillion " },
{ 1000000000000000LL, " qadrillion " },
{ 1000000000000LL, " trillion " },
{ 1000000000, " billion " },
{ 1000000, " million " },
{ 1000, " thousand " },
{ 100, " hundred " },
{ 0, "" }
};
string result;
if (n < 0) {
n = -n;
}
if (n == 0) {
result = "zero ";
}
for (Limit *p = limits; p->limit; ++p) {
if (n >= p->limit) {
result += numToString(n / p->limit);
result += p->name;
n %= p->limit;
}
}
if (n >= 20) {
result += tens[(n / 10) - 2];
n %= 10;
}
if (n >= 10) {
result += teens[n-10];
n = 0;
}
if (n > 0) {
result += ones[n-1];
}
// The value ends with a space, trim it off.
result.resize(result.size()-1);
return result;
}
|