# [Solved]Numbers to Words Converter

• Hi Everyone.
Is their any method which converts integer number to word, i.e.,
2 ---> Two,
23 ---> Twenty Three,
150 ---> One Hundred and Fifty etc.

Thank you in advance.

• not really Qt related, but you can try to find something "here":https://www.google.com/search?q=Numbers+to+Words+Converter+c++ :)

• Had to do this a while ago, here's my Qt based interpretation of what I found to be the an elegant solution.

@
//Based on https://github.com/icelander/number_to_text
static QString numberToText(uint number)
{
static QMap<uint, QString> numbers;

//Only initialize once
if (numbers.isEmpty())
{
numbers = "zero";
numbers = "one";
numbers = "two";
numbers = "three";
numbers = "four";
numbers = "five";
numbers = "six";
numbers = "seven";
numbers = "eight";
numbers = "nine";
numbers = "ten";
numbers = "eleven";
numbers = "twelve";
numbers = "thirteen";
numbers = "fourteen";
numbers = "fifteen";
numbers = "sixteen";
numbers = "seventeen";
numbers = "eighteen";
numbers = "nineteen";
numbers = "twenty";
numbers = "thirty";
numbers = "forty";
numbers = "fifty";
numbers = "sixty";
numbers = "seventy";
numbers = "eighty";
numbers = "ninety";
}

static QMap<uint, QString> powers;

//Only initialize once
if (powers.isEmpty())
{
powers = "hundred";
powers = "thousand";
powers = "million";
powers = "billion";
}

QString output;

if (number < 21)
{
output = numbers[number];
}
else if (number < 100)
{
output = numbers[10 * qFloor(number / 10)];
uint remainder = number % 10;

if (remainder > 0)
output += "-" + numberToText(remainder);
}
else
{
uint power = 2;
uint place = 0;
QString powerString;

//QMap::keys is ordered
foreach (uint pow, powers.keys())
{
uint place_value = qPow(10, pow);
uint tmp_place = qFloor(number / place_value);
if (tmp_place < 1)
break;

place = tmp_place;
power = pow;
powerString = powers[pow];
}

if (power > 0)
{
output = numberToText(place) + " " + powerString;
uint remainder = number % uint(qPow(10, power));

if (remainder > 0)
output += " " + numberToText(remainder);
}
}

return output;
}
@

• nice job :)

• Thank you Noah, marcoB for your response...

• Nice implementation