Click here to Skip to main content
Click here to Skip to main content
Go to top

Convert Number to Text

, 7 May 2012
Rate this:
Please Sign up or sign in to vote.
How to convert number to word, number to eqviualent word, change number to word , how to convert number to text

Introduction

This tip will explain how to convert a number to its equivalent word. For example, the number 1000 returns "One Thousand Only".

Background

Here, I have given the .NET code for conversion. I have tried to simplify the code for easy understanding.

Using the Code

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

How It Works

There will be a dictionary object for tens and hundreds represent ten digits and hundred digit and list for number 1 to 19. The method is named 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 exists in the hundred digit dictionary. If the position exists, it will get the single equivalent number from oneTo19List and add hundred or thousand, etc. If position does not exist, then it will increment the position and get the number from oneTo19List or the tendigit dictionary and add hundred or thousand, etc. Like that, it constructs the word in loop and finally returns the equivalent 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 only 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. Just add one more entry to the HundredDigit dictionary collection below in the code.

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 equal to hundred,
        // thousand and Lakhs 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)

Share

About the Author

No Biography provided

Comments and Discussions

 
QuestionRetrieve the number from database Pinprofessionalsern891-Aug-13 18:52 
GeneralExcellent PinmemberPrabu ram9-May-12 20:44 
GeneralWe don't need more articles/tips like this [modified] PinmemberPIEBALDconsult7-May-12 18:39 
GeneralRe: We don't need more articles/tips like this PinmemberPrabu ram9-May-12 20:40 
GeneralRe: We don't need more articles/tips like this PinmemberMossmyr14-May-12 6:23 
Questionhi Pinmemberjeta5457-May-12 5:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 7 May 2012
Article Copyright 2012 by Sankar Subramanian
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid