Code:
/*
NumbersToWord.cpp
Copyright © 2005 JaGameDevelopers®
by crosswire
*/
/*
Desired presentation format
0 = Zero
1 = One
10 = Ten
11 = Eleven
100 = One Hundred
101 = One Hundred and One
110 = One Hundred and Ten
111 = One Hundred and Eleven
1000 = One Thousand
1001 = One Thousand, and One
1010 = One Thousand, and Ten
1011 = One Thousand, and Eleven
1100 = One Thousand, and One Hundred*
1101 = One Thousand, One Hundred and One
1110 = One Thousand, One Hundred and Ten
1111 = One Thousand, One Hundred and Eleven
10000 = Ten Thousand
10001 = Ten Thousand, and One
10010 = Ten Thousand, and Ten
10011 = Ten Thousand, and Eleven
10100 = Ten Thousand, and One Hundred
10101 = Ten Thousand, One Hundred and One
10110 = Ten Thousand, One Hundred and Ten
10111 = Ten Thousand, One Hundred and Eleven
11000 = Eleven Thousand
*If desired Eleven Hundred could be written using special logic could be used for this range.
Same pattern is desired for higher numbers
101011 = One Hundred and One Thousand, and Eleven
101101 = One Hundred and One Thousand, One Hundred and One
...
101000001 = One Hundred and One Million, and One
101000010 = One Hundred and One Million, and Ten
101000011 = One Hundred and One Million, and Eleven
101000101 = One Hundred and One Million, One Hundred and One
101000110 = One Hundred and One Million, One Hundred and One
...
101101101 = One Hundred and One Million, One Hundred and One Thousand, One Hundred and One
...
111111111 = One Hundred and Eleven Million, One Hundred and Eleven Thousand, One Hundred and Eleven
Therefore 1,100,100 would be written One Million, One Hundred Thousand, and One Hundred
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#define THOUSANDS_LIMIT 4
//A limit of 4 represents the Billionth quantity max
namespace utility
{
//Get the word for numbers 0-19
char words_one_to_nineteen[20][64] = {
"",
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen"
};
//Get the word for 20, 30, 40, ... 90
char words_twenty_to_ninety[10][64] = {
"",
"",
"Twenty",
"Thirty",
"Forty",
"Fifty",
"Sixty",
"Seventy",
"Eighty",
"Ninety"
};
char word_hundred[64] = "Hundred";
//Get the word for 100, 1000, 1000000
char words_thousand_million_billion[THOUSANDS_LIMIT][64] = {
"",
"Thousand",
"Million",
"Billion"
};
//Get a digit from Number given the index of the digit. Eg, return 2 when Number = 12345 and DigitIndex = 3
int GetDigitFromNumber(int Number, int DigitIndex)
{
return (Number%(int)pow(10,DigitIndex+1))/(int)pow(10,DigitIndex);
}
//Get the word for 1, 10, 11, 100, 101, 110, 111, but the Number must be less than 1000
char * NumberToWordLessThanThousand(int Number, char * Word)
{
strcpy(Word, "");
int HundredDigit = utility::GetDigitFromNumber(Number, 2);
if(HundredDigit) //If there is a hundreds digit
{
strcat(Word, utility::words_one_to_nineteen[HundredDigit]); //Write One, Two, Three, ... or Nine
strcat(Word, " ");
strcat(Word, utility::word_hundred); //Write Hundred
//if(HundredDigit > 1)
// strcat(Word, "s"); //Write s
}
int TensAndOnesDigits = Number % 100;
if(TensAndOnesDigits)
{
if(HundredDigit)
strcat(Word, " and ");
if(TensAndOnesDigits < 20)
strcat(Word, utility::words_one_to_nineteen[TensAndOnesDigits]);//Write One, Two, Three, ... or Nineteen
else
{
strcat(Word, utility::words_twenty_to_ninety[TensAndOnesDigits/10]);//Write Twenty, Thirty, ...
if(TensAndOnesDigits%10) //If the last digit is not zero
strcat(Word, "-"); //Write a dash
strcat(Word, utility::words_one_to_nineteen[TensAndOnesDigits%10]); //Write One, Two, Three... This is the last digit
}
}
return Word;
}
//Get the word for a Number, eg 101230019
char * NumberToWord(int Number, char * Word)
{
int NThousandQuantites = 0; //The number of times Billion, Million, or Thousand need to be written. A Thousand quantity is like Thousand, Million, Billion.
int ThousandQuantitesAmount[THOUSANDS_LIMIT]; //The amount of Billions associated with the Billionth word, eg Ten in Ten Billion. Literally, ThousandQuantitesAmount is the amount of Billions.
int ThousandQuantitesIndex[THOUSANDS_LIMIT]; //The index to make a Billionth, Millionth, or Thousand quantity, eg the indices 0,1,2 mark Thousand, Million, Billion respectively.
for(int i = 0; i < THOUSANDS_LIMIT; i++) //Check the amount of Thousands, then Millions, then Billions, ...
{
int Quantity = (Number%(__int64)pow(1000,i+1))/(__int64)pow(1000,i); //Get the number of Billions, Millions, or Thousands. Here __int64 is used to count Billions.
//Eg for Number = 123,456,789
//when i = 1, then Quantity = 456
//when i = 2, then Quantity = 123
if(Quantity > 0)
{
ThousandQuantitesAmount[NThousandQuantites] = Quantity;
ThousandQuantitesIndex[NThousandQuantites] = i; //i represents the index of which Thousand quantity is being stored
NThousandQuantites++;
}
}
bool ThousandQuantityWasWritten = false;
strcpy(Word, "");
for(i = NThousandQuantites - 1; i >= 0; i--) //Write all the Thousand Quantity Amounts then the Thousand Quantity starting from the largest Thousand Quantity
{
if(ThousandQuantityWasWritten) //If something was written before
{
strcat(Word, ", ");
//if(i == 0) //TODO : And if this is the last quantity to be written which itself does not have an 'and' in it
// strcat(Word, "and ");
}
char buffer[1024];
strcat(Word, utility::NumberToWordLessThanThousand(ThousandQuantitesAmount[i], buffer)); //Write something like One Hundred and Twenty One
strcat(Word, " ");
strcat(Word, utility::words_thousand_million_billion[ThousandQuantitesIndex[i]]); //Write something like Million
ThousandQuantityWasWritten = true;
}
return Word;
}
}
int main(int argc, char* argv[])
{
char * test = utility::words_one_to_nineteen[0];
while(true)
{
printf("Please enter your number (without commas and not greater than 2,147,483,647)\n:");
int Number;
scanf("%d", &Number);
if(Number == 0)
return 0;
if(Number > 2147483647)
{
printf("No conversion made on '%d'\n", Number);
printf("Reason : the number it too big\n");
}
else
{
char Word[128];
utility::NumberToWord(Number, Word);
printf("\n'%d' = %s\n\n", Number, Word);
}
}
return 0;
}