Software & Finance





Visual C++ - Convert Numbers to Text (Words)





There might be some cases where we need to display the numbers as text. For example, given the input as 115 and output would be One Hundred Fifteen.

I have given here the code to display the numbers upto 100,000. If you want more limit, you can keep extending the program. 


Source Code


#include <iostream>

 

 

bool HelperConvertNumberToText(int num, char *buf, int len)

{

    static char *strones[] = {

      "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",

      "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",

      "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen",

    };

 

    static char *strtens[] = {

        "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty",

        "Seventy", "Eighty", "Ninety", "Hundred"

    };

 

    int single, tens, hundreds;

 

    if(num > 1000)

        return false;

 

    hundreds = num / 100;

    num = num - hundreds * 100;

    if( num < 20)

    {

        tens = 0; // special case

        single = num;

    }

    else

    {

        tens = num / 10;

        num = num - tens * 10;

        single = num;

    }

 

    char result[1024];

    memset(result, 0, 1024);

   

    if(hundreds > 0)

    {

        strcat(result, strones[hundreds-1]);

        strcat(result, " Hundred ");

    }

    if(tens > 0)

    {

        strcat(result, strtens[tens-1]);

        strcat(result, " ");

    }

    if(single > 0)

    {

        strcat(result, strones[single-1]);

        strcat(result, " ");

    }

 

    if(len > strlen(result))

        strcpy(buf, result);

 

    return true;

}

 

bool ConvertNumberToText(int num, char *buf, int len)

{

    if(num < 0 || num > 100000)

    {

        std::cout << num << " \t Not Supported\n";

        return false;

    }

 

    if( num == 0)

    {

        std::cout << num << " \t Zero\n";

        return false;

    }

 

    char tres[1024];

    char result[1024];

    memset(result, 0, 1024);

 

    if(num < 1000)

    {  

        HelperConvertNumberToText(num, (char*) &tres, 1024);

        strcat(result, tres);

    }

    else

    {

        int thousands;

        thousands = num / 1000;

        int temp = num - thousands * 1000;

 

        HelperConvertNumberToText(thousands, (char*) &tres, 1024);

        strcat(result, tres);

        strcat(result, "Thousand ");

        HelperConvertNumberToText(temp, (char*) &tres, 1024);

        strcat(result, tres);

    }

   

    if(len > strlen(result))

        strcpy(buf, result);

    return true;

}

 

int main()

{

    int len = 1024;

    char result[1024];

 

    static int arrNum[] =

    {

      -1, 0, 5, 10, 15, 19, 20, 21, 25, 33, 49, 50, 72,

      99, 100, 101, 117, 199, 200, 214, 517, 589, 999,

      1000, 1010, 1018, 1200, 9890, 10119, 13535, 57019,

      99999, 100000, 100001

    };

 

    for(int i = 0; i < sizeof(arrNum) / sizeof(int); i++)

    {

        int num = arrNum[i];

        if( ConvertNumberToText(num, result, len) == true)

            std::cout << num << " \t " << result << "\n";

    }

    return 0;

}

Output


-1       Not Supported

0        Zero

5        Five

10       Ten

15       Fifteen

19       Nineteen

20       Twenty

21       Twenty One

25       Twenty Five

33       Thirty Three

49       Fourty Nine

50       Fifty

72       Seventy Two

99       Ninety Nine

100      One Hundred

101      One Hundred One

117      One Hundred Seventeen

199      One Hundred Ninety Nine

200      Two Hundred

214      Two Hundred Fourteen

517      Five Hundred Seventeen

589      Five Hundred Eighty Nine

999      Nine Hundred Ninety Nine

1000     One Thousand

1010     One Thousand Ten

1018     One Thousand Eighteen

1200     One Thousand Two Hundred

9890     Nine Thousand Eight Hundred Ninety

10119    Ten Thousand One Hundred Nineteen

13535    Thirteen Thousand Five Hundred Thirty Five

57019    Fifty Seven Thousand Nineteen

99999    Ninety Nine Thousand Nine Hundred Ninety Nine

100000   One Hundred Thousand

100001   Not Supported