Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#
Tip/Trick

Convert number to its equivalent word

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
7 May 2012CPOL1 min read 46.3K   6  
how to convert number to word , number to eqviualent word , change number to word , how to convert number to text
This is an old version of the currently published tip/trick.

Introduction 

This article will explain as how to convert numbers to its equivalent word. For example a number 1000 returns as "One Thousand Only".

Background

Here I given .NET code  for conversion. I try to simplify the code for your easy understanding to convert number to word

Using the code 

This code is written in .NET. Simply copy and paste the full code in the new .NET class.

How it works

There will be dictionary object for  tens and hundreds represent ten digits and hungred digit  and list for number 1 to 19. The method is named as ConvertNumberAsText. This accepts a parameter of type int. First, the method will convert the number into string and loop the string from its starting position. From its start position, it checks whether  the position is exist hundred digit dictionary. If the position exist it will get single eqviualent number from oneTo19List and add hundred or thousand etc. If position not exist then it will increment it position and get then number from oneTo19List  or tendigit dictonary  and add  hundred or thousand etc . Like that it construct word in loop and finally it return eqviualent word.

Implementation

Here is a complete class file which consists of all the code involved in converting the numbers to word. However, it has a limitation that it can handle up to the number 10 crore which I think will cover most of your requirements. If you require more than 10 crore, then you need to modify the code yourself. Smile and it is not difficult though. Just add one more entry to the HundredDigit dictionary collection below in the code.

C#
static class Extension
{
    public static int ToInt(this string str)
    {

        int output;
        int.TryParse(str, out output);
        return output;
    }
}

static List<string> oneTo19Text = new List<string> {
"Zero", "One" , "Two", "Three", "Four", 
  "Five", "Six", "Seven", "Eight", 
  "Nine", "Ten" , "Eleven" , "Twelve",
  "Thirteen", "Fourteen", "Fifteen" , 
  "Sixteen" , "Seventeen", "Eighteen" , "Nineteen"
};

static Dictionary<int, string> tensDigit = 
       new Dictionary<int, string>() 
{  
    { 2,"Twenty"},
    { 3,"Thirty"},
    { 4,"Fourty"},
    { 5,"Fifty"},
    { 6,"Sixty"},
    { 7,"Seventy"},
    { 8,"Eighty"},
    { 9,"NineTy"}
};

static Dictionary<int, string> HundredDigit = 
       new Dictionary<int, string>() 
{  
    { 3,"Hundred"},
    { 4,"Thousand"},
    { 6,"Lakhs"},
    { 8,"Crore"}
    
};

public static string ConvertNumberAsText(int num)
{
    int numberLength = num.ToString().Length;
    string numberString = num.ToString();
    int position = numberLength;
    if (numberLength == 1)
        return oneTo19Text[num];
    string result = string.Empty;
    int number = 0;

    // loop the position in number string
    for (int startPosition = 0; startPosition < numberLength; startPosition++)
    {
        // check the position is equalent to hundred,
        // thousand and lakks or its hundred ,tenthousand .....
        Dictionary<int,string> getHundtedWord = HundredDigit.Where(
           p => p.Key == position).ToDictionary(p => p.Key, p => p.Value);

        if (getHundtedWord.Count == 0)
        {
            number = numberString.Substring(startPosition, 1).ToInt();
            if (number < 2)
            {
                number = numberString.Substring(startPosition, 2).ToInt();
                startPosition++;
                position--;
            }
            else
            {
                result += " " + tensDigit[number];
                startPosition++;
                position--;
                number = numberString.Substring(startPosition, 1).ToInt();
            }

            result += " " + oneTo19Text[number];
            if (position > 2)
                result += " " + HundredDigit[position];
        }
        else
        {
            number = numberString.Substring(startPosition, 1).ToInt();
            result += " " + oneTo19Text[number];
            if (position > 2)
            result += " " + HundredDigit[position];
        }

        position--;
    }

    return result;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.