Numbers To Words

This algorithm converts decimal numbers to its English word representation.



									/*****Please include following header files*****/
// string
// vector
/***********************************************/

/*****Please use following namespaces*****/
// std
/*****************************************/

static string NumbersToWords(uint64_t num) {
	string words = "";
	vector<string> singles = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
	vector<string> teens = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
	vector<string> tens = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
	vector<string> powers = { "", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion" };

	if (num >= 1000)
	{
		int powersLength = powers.size();
		for (int i = powersLength - 1; i >= 0; i--)
		{
			uint64_t power = (uint64_t)pow(1000, i);
			uint64_t count = (num - (num % power)) / power;

			if (num > power)
			{
				words += NumbersToWords(count) + " " + powers[i];
				num -= count * power;
			}
		}
	}

	if (num >= 100)
	{
		uint64_t count = (num - (num % 100)) / 100;
		words += NumbersToWords(count) + " hundred";
		num -= count * 100;
	}

	if (num >= 20 && num < 100)
	{
		uint64_t count = (num - (num % 10)) / 10;
		words += " " + tens[count];
		num -= count * 10;
	}

	if (num >= 10 && num < 20)
	{
		words += " " + teens[num - 10];
		num = 0;
	}

	if (num > 0 && num < 10)
	{
		words += " " + singles[num];
	}

	return words;
}
								


Example

									uint64_t data = 5478775544879599;
string value = NumbersToWords(data);
								


Output

									five quadrillion four hundred seventy eight trillion seven hundred seventy five billion five hundred forty four million eight hundred seventy nine thousand five hundred ninty nine