It's not ideal code but it works ;):
/// See http://www.corollarytheorems.com/Grammar/numeral.htm
std::string Numeral(int);
namespace {
const int
BILLION = 1000000000,
MILLION = 1000000,
THOUSAND= 1000,
HUNDRED = 100,
TEN = 10
;
const char* tens[] = {
"", "", "twenty", "thirty", "fourty", "fifty",
"sixty", "seventy", "eighty", "ninety"
};
const char* teen[] = { // feel too lazy to glue ty/teen
"", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten",
"eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"
};
int trio(std::string& s, int sum, int unit, const char* what) {
int res = sum % unit;
int n = sum / unit;
if (n) {
s += Numeral(n);
s += what;
if (res)
s += ' ';
}
return res;
}
const std::string zero("zero");
}
/// For 32-bit integers only!
std::string Numeral(int n) {
if (n == 0)
return zero;
std::string s;
if (n < 0) {
n = -n;
s = "minus ";
}
// For 64-bit code insert corr here
n = trio(s,n,BILLION," billion"); // Why if n < ...
n = trio(s,n,MILLION," million");
n = trio(s,n,THOUSAND," thousand");
n = trio(s,n,HUNDRED," hundred");
if (n) { // < 100
int t = 0;
if (n >= 20) {
s += tens[t=n/TEN];
n %= TEN;
}
if (n) {
if (t)
s += '-';
s += teen[n];
}
}
return s;
}