Click here to Skip to main content
Click here to Skip to main content

Convert Numeric Currency into Words (For INDIAN Currency Only) (Optimized)

By , 6 Sep 2011
 

Introduction

This is a simple program or a complicated one too that converts numeric value into words from 0 to 1039;... Great!!!

A Short introduction may be:

It can convert 0 to Zero, can also convert to 12345 to "twelve thousand three hundred forty five" and can also covert 1039 to 1 Adant-Shinghar. (Courtesy: Wikipedia for providing me with the notations.)

Before you move further, let me inform you first that this class will give you a returned value in string with HTML tags attached, so please use it for web application so that you will find neat and clean results and to use it for Windows Forms, please make the necessary changes.

So guys, let's dive into the logic !!!

Background

Below is a snap shot provided to get the following results.

Using the Code

Let's dig into the code step by step.

Step 1

Take three static List<DictionaryEntry> Type objects for static data storage and load that in the constructor of class.

Have a look at the code snippets.

Constructor of the class:

namespace Hirs.IndianNumericToWordConverter
{
    public class IndiaCurrencyConverter
    {
        private Hashtable htPunctuation;
        private Dictionary<int> DictStaticSuffix;
        private Dictionary<int> DictStaticPrefix;
        private Dictionary<int> DictHelpNotation;
        private System.Drawing.Color color;

        public IndiaCurrencyConverter()
        {
            htPunctuation = new Hashtable();
            DictStaticPrefix = new Dictionary<int>();
            DictStaticSuffix = new Dictionary<int>();
            DictHelpNotation = new Dictionary<int>();
            Native = native;
            LoadStaticPrefix();
            LoadStaticSuffix();
            LoadHelpofNotation();
        }

Here is the method that loads static data with values.

private void LoadStaticSuffix()
        {
            DictStaticSuffix.Add(1, "One ");
            DictStaticSuffix.Add(2, "Two ");
            DictStaticSuffix.Add(3, "Three ");
            DictStaticSuffix.Add(4, "Four ");
            DictStaticSuffix.Add(5, "Five ");
            DictStaticSuffix.Add(6, "Six ");
            DictStaticSuffix.Add(7, "Seven ");
            DictStaticSuffix.Add(8, "Eight ");
            DictStaticSuffix.Add(9, "Nine ");
            DictStaticSuffix.Add(10, "Ten ");
            DictStaticSuffix.Add(11, "Eleven ");
            DictStaticSuffix.Add(12, "Twelve ");
            DictStaticSuffix.Add(13, "Thirteen ");
            DictStaticSuffix.Add(14, "Fourteen ");
            DictStaticSuffix.Add(15, "Fifteen ");
            DictStaticSuffix.Add(16, "Sixteen ");
            DictStaticSuffix.Add(17, "Seventeen ");
            DictStaticSuffix.Add(18, "Eighteen ");
            DictStaticSuffix.Add(19, "Nineteen ");
            DictStaticSuffix.Add(20, "Twenty ");
            DictStaticSuffix.Add(30, "Thirty ");
            DictStaticSuffix.Add(40, "Forty ");
            DictStaticSuffix.Add(50, "Fifty ");
            DictStaticSuffix.Add(60, "Sixty ");
            DictStaticSuffix.Add(70, "Seventy ");
            DictStaticSuffix.Add(80, "Eighty ");
            DictStaticSuffix.Add(90, "Ninety ");
        }
        private void LoadStaticPrefix()
        {
                DictStaticPrefix.Add(2, "Thousand ");
                DictStaticPrefix.Add(3, "Lac ");
                DictStaticPrefix.Add(4, "Crore ");
                DictStaticPrefix.Add(5, "Arab ");
                DictStaticPrefix.Add(6, "Kharab ");
                DictStaticPrefix.Add(7, "Neel ");
                DictStaticPrefix.Add(8, "Padma ");
                DictStaticPrefix.Add(9, "Shankh ");
                DictStaticPrefix.Add(10, "Maha-shankh ");
                DictStaticPrefix.Add(11, "Ank ");
                DictStaticPrefix.Add(12, "Jald ");
                DictStaticPrefix.Add(13, "Madh ");
                DictStaticPrefix.Add(14, "Paraardha ");
                DictStaticPrefix.Add(15, "Ant ");
                DictStaticPrefix.Add(16, "Maha-ant ");
                DictStaticPrefix.Add(17, "Shisht ");
                DictStaticPrefix.Add(18, "Singhar ");
                DictStaticPrefix.Add(19, "Maha-singhar ");
                DictStaticPrefix.Add(20, "Adant-singhar ");
        }
        private void LoadHelpofNotation()
        {
            DictHelpNotation.Add(2, "=1,000 (3 Trailing Zeros)");
                DictHelpNotation.Add(3, "=1,00,000 (5 Trailing Zeros)");
                DictHelpNotation.Add(4, "=1,00,00,000 (7 Trailing Zeros)");
                DictHelpNotation.Add(5, "=1,00,00,00,000 (9 Trailing Zeros)");
                DictHelpNotation.Add(6, "=1,00,00,00,00,000 (11 Trailing Zeros)");
                DictHelpNotation.Add(7, "=1,00,00,00,00,00,000 (13 Trailing Zeros)");
                DictHelpNotation.Add(8, "=1,00,00,00,00,00,00,000 (15 Trailing Zeros)");
                DictHelpNotation.Add
		(9, "=1,00,00,00,00,00,00,00,000 (17 Trailing Zeros)");
                DictHelpNotation.Add(10, "=1,00,00,00,00,00,00,00,00,000 
					(19 Trailing Zeros)");
                DictHelpNotation.Add(11, "=1,00,00,00,00,00,00,00,00,00,000 
					(21 Trailing Zeros)");
                DictHelpNotation.Add(12, "=1,00,00,00,00,00,00,00,00,00,00,000 
					(23 Trailing Zeros)");
                DictHelpNotation.Add(13, "=1,00,00,00,00,00,00,00,00,00,00,00,000 
					(25 Trailing Zeros)");
                DictHelpNotation.Add(14, "=1,00,00,00,00,00,00,00,00,00,00,00,00,000 
					(27 Trailing Zeros)");
                DictHelpNotation.Add(15, "=1,00,00,00,00,00,00,00,00,00,00,00,00,00,000
					(29 Trailing Zeros)");
                DictHelpNotation.Add
		(16, "=1,00,00,00,00,00,00,00,00,00,00,00,00,00,00,000
					(31 Trailing Zeros)");
                DictHelpNotation.Add
		(17, "=1,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,000
					(33 Trailing Zeros)");
                DictHelpNotation.Add
		(18, "=1,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,000
					(35 Trailing Zeros)");
                DictHelpNotation.Add
		(19, "=1,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,000
					(37 Trailing Zeros)");
                DictHelpNotation.Add
		(20, "=1,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,000
					(39 Trailing Zeros)");
            }

and I got comments on what is needed of List<DictionaryEntry> listHelpNotation so, the reason behind that is on Hovering you can get help its unit values.

Step 2

Now insert Input value (Let me notify you that input value will be in string because no datatype supports 1039).

Assume input value is 123456789.

First reverse the input string:

(987654321)

Then split and insert reversed string into hashtable (first 3 digit, and then after 2 till end):

(987,65,43,21)

and then reverse every splitted value inserted into hashtable so that it makes real sense.

(789,56,34,12)

and insert that to hashtable with key value pair using the following code:

private void InsertToPunctuationTable(string strValue)
        {
            htPunctuation.Add(1, strValue.Substring(0, 3).ToString());
            int j = 2;
            for (int i = 3; i < strValue.Length; i = i + 2)
            {
                if (strValue.Substring(i).Length > 0)
                {
                    if (strValue.Substring(i).Length >= 2)
                        htPunctuation.Add(j, strValue.Substring(i, 2).ToString());
                    else
                        htPunctuation.Add(j, strValue.Substring(i, 1).ToString());
                }
                else
                    break;
                j++;

            }
        }

Now get the word notation for every value stored in hashtable descending.

Have a look at the code.

Here's the function:

GetWordConversion(string inputNumber)

It takes Stringconverted numeric value and gives its Word Conversation, It will take input which will have a maximum of 3 digits. You may remember from the upper hashtable that Entry has value which is maximum of 3 digits.

Explanation of Condition Check

if (int.Parse(inputNumber.Substring(0, 1)) > 0)

Checks if the first digit is not zero, then return Hundreds notation. E.g. 434 then it returns Four Hundred

string TempString = StaticSuffixFind(inputNumber.Substring(1, 2));

This condition checks that if the two digit value is already stored in List. E.g., it will return twelve with input of 12. Rather than returning One Two, and if it is 34 then No stored value will be found and it will pass the condition if(tempString == "") and will move on. The different condition will be applied to if the number is Two digit, or 1 digit.

private string GetWordConversion(string inputNumber)
        {
            string ToReturnWord = String.Empty;
            if (inputNumber.Length <= 3 && inputNumber.Length > 0)
            {
                if (inputNumber.Length == 3)
                {
                    if (int.Parse(inputNumber.Substring(0, 1)) > 0)
                        ToReturnWord = ToReturnWord +
			StaticSuffixFind(inputNumber.Substring(0, 1)) + "Hundred ";

                    string TempString = StaticSuffixFind(inputNumber.Substring(1, 2));

                    if (TempString == "")
                    {
                        ToReturnWord = ToReturnWord +
			StaticSuffixFind(inputNumber.Substring(1, 1) + "0");
                        ToReturnWord = ToReturnWord +
			StaticSuffixFind(inputNumber.Substring(2, 1));
                    }
                    ToReturnWord = ToReturnWord + TempString;
                }
                if (inputNumber.Length == 2)
                {
                    string TempString = StaticSuffixFind(inputNumber.Substring(0, 2));
                    if (TempString == "")
                    {
                        ToReturnWord = ToReturnWord +
			StaticSuffixFind(inputNumber.Substring(0, 1) + "0");
                        ToReturnWord = ToReturnWord +
			StaticSuffixFind(inputNumber.Substring(1, 1));
                    }
                    ToReturnWord = ToReturnWord + TempString;
                }
                if (inputNumber.Length == 1)
                {
                    ToReturnWord = ToReturnWord +
			StaticSuffixFind(inputNumber.Substring(0, 1));
                }
            }
            return ToReturnWord;
        }

After that, attach the following splitted words from the splitted hashtable value and their notation and make the one big string that will depict the whole notation:

private string ReturnHashtableValue()
        {
            string strFinalString = String.Empty;
            for (int i = htPunctuation.Count; i > 0; i--)
            {
                if (GetWordConversion((htPunctuation[i]).ToString()) != "")
                    strFinalString = strFinalString +
			GetWordConversion((htPunctuation[i]).ToString()) +
			StaticPrefixFind((i).ToString());
            }
            return strFinalString;
        }

Points of Interest

For now, I have submitted code for Indian currency only. Now for moving further in part-II, I will also depict how to covert in international currency as suggested by Indian mentors from Indian forums.

FYI, Part-II is published. Have a look:

And last but not the least, thanks to the audience for giving your votes and valuable time.

For further details, please see the attached class.

History

  • This is Part-I. Part-II has been submitted for international currency.

License

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

About the Author

Hiren solanki
Software Developer
India India
He is a Smart IT devloper with Few years of Expeariance But having Great command on ASP.net,C#,SQL Query,SSRS,Crystal Reports
 
Apart from that He Loves multimedia work too, Master of Adobe photoshop, Illustrator, CSS , HTML and all things.
 
He is Currently working in Microsoft Dynamics CRM and Having Nice Expearince with CRM. CRM Rocks!!!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: My vote of 2memberHiren Solanki10-Sep-10 17:12 
Thanks Both of you,
You are really helpful.
Hiren Solanki
"Good judgment comes from experience, and experience comes from bad judgment"

My Latest Article
Convert Numeric Currency into words ( For INDIAN Currency Only )

GeneralRe: My vote of 2 [modified]memberHiren Solanki10-Sep-10 19:11 
Hello Nagy,
Nagy Vilmos wrote:
Now I've read the code. Pretty pants really. If you're going to use dictionaries then at least take advantage of the fact that they CAN LOOK UP BY KEY.
Clue - use TryGetValue rather then stupid loops.

 
I have updated my code on your pointed things.
Now could you please Revote article or Provide more suggestions. ?
 
Thanks.
Hiren Solanki
"Good judgment comes from experience, and experience comes from bad judgment"

My Latest Article
Convert Numeric Currency into words ( For INDIAN Currency Only )
modified on Tuesday, September 14, 2010 4:51 AM

GeneralMy vote of 3memberkarabax1-Sep-10 6:00 
hmmm...
GeneralRe: My vote of 3memberHiren Solanki5-Sep-10 19:30 
Thanks for you vote,
But what is this 'hmmm'
Please be Descriptive.
 
Thanks,
Hiren Solanki
"Good judgment comes from experience, and experience comes from bad judgment"

My Latest Article
Convert Numeric Currency into words ( For INDIAN Currency Only )

GeneralRe: My vote of 3memberS M P9-Sep-10 2:26 
He dont read your article and vote. just testing the site thats why he say "hmmmmmmmmmmmmmmmmmmmmmmm" Laugh | :laugh:
 
He must be newbie.. Big Grin | :-D

GeneralRe: My vote of 3memberHiren Solanki9-Sep-10 2:51 
I think so,
But there is no meaning of giving vote of 3 without reading article,
and just saying 'hmmm'
Hiren Solanki
"Good judgment comes from experience, and experience comes from bad judgment"

My Latest Article
Convert Numeric Currency into words ( For INDIAN Currency Only )

GeneralRe: My vote of 3mentorKunalChowdhury9-Sep-10 19:56 
Don't worry on that.

 
Don't forget to Click on [Vote] and [Good Answer] on the posts that helped you.


Regards - Kunal Chowdhury | Software Developer | Chennai | India | My Blog | My Tweets | Silverlight Tutorial

GeneralRe: My vote of 3memberHiren Solanki9-Sep-10 20:01 
What to do Kunal For That.
Hiren Solanki
"Good judgment comes from experience, and experience comes from bad judgment"

My Latest Article
Convert Numeric Currency into words ( For INDIAN Currency Only )

GeneralRe: My vote of 3memberAbhinav S9-Sep-10 20:05 
If a good enough number "vote to remove message" it will go away anyway,
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
 

My latest tip/trick
 
Visit the Hindi forum here.

GeneralRe: My vote of 3mentorKunalChowdhury9-Sep-10 20:06 
Nothing. His vote is already downvoted by 5. Possibility of delete automatically.
And above all, he is a newbie and will not hamper your reputation a big.
 
Don't worry. There are some people who love to vote 1. Laugh | :laugh: I don't care on such person. They vote without reading.
 
Keep up your good work. Thumbs Up | :thumbsup:

 
Don't forget to Click on [Vote] and [Good Answer] on the posts that helped you.


Regards - Kunal Chowdhury | Software Developer | Chennai | India | My Blog | My Tweets | Silverlight Tutorial

GeneralRe: My vote of 3memberHiren Solanki9-Sep-10 20:08 
How many downvote it takes for message to be removed. ?
Hiren Solanki
"Good judgment comes from experience, and experience comes from bad judgment"

My Latest Article
Convert Numeric Currency into words ( For INDIAN Currency Only )

GeneralRe: My vote of 3mentorKunalChowdhury9-Sep-10 20:10 
Depends on the reputation of the voters. Generally it is 3-5. Don't look for it.

 
Don't forget to Click on [Vote] and [Good Answer] on the posts that helped you.


Regards - Kunal Chowdhury | Software Developer | Chennai | India | My Blog | My Tweets | Silverlight Tutorial

GeneralRe: My vote of 3mentorKunalChowdhury9-Sep-10 20:11 
Waiting for your next article... Smile | :)

 
Don't forget to Click on [Vote] and [Good Answer] on the posts that helped you.


Regards - Kunal Chowdhury | Software Developer | Chennai | India | My Blog | My Tweets | Silverlight Tutorial

GeneralRe: My vote of 3memberHiren Solanki9-Sep-10 20:13 
Preparing for that.
Hiren Solanki
"Good judgment comes from experience, and experience comes from bad judgment"

My Latest Article
Convert Numeric Currency into words ( For INDIAN Currency Only )

GeneralRe: My vote of 3memberNagy Vilmos10-Sep-10 3:14 
S M P wrote:
He must be newbie..

 
Member since Monday, November 24, 2003
 

My maths says that's nearly seven years. And how long have you been here? Right or wrong the vote was made, belittling someone for saying 'this sucks' is acceptible, but make the effort to get your facts right.
 
BTW, I think the article is poor and the code badly written. But it's not in my domain so if you guys want to vote it up go right ahead.


Panic, Chaos, Destruction.
My work here is done.

 
or "Drink. Get drunk. Fall over." - P O'H

GeneralRe: My vote of 3memberHiren Solanki10-Sep-10 3:19 
Nagy Vilmos wrote:
My maths says that's nearly seven years. And how long have you been here? Right or wrong the vote was made, belittling someone for saying 'this sucks' is acceptible, but make the effort to get your facts right

 

Whatever your maths says,
But your maths doesnt count the activity he has done in code project.
Your math just counts the joining date.
So Polish your math first.
 
Thanks anyway.
Hiren Solanki
"Good judgment comes from experience, and experience comes from bad judgment"

My Latest Article
Convert Numeric Currency into words ( For INDIAN Currency Only )

GeneralNice onememberHimanshuJoshi1-Sep-10 3:53 
voted 5. Waiting for the universal version

GeneralRe: Nice onememberHiren Solanki5-Sep-10 19:30 
Thanks,
Will come back soon.
Thanks
Hiren Solanki
"Good judgment comes from experience, and experience comes from bad judgment"

My Latest Article
Convert Numeric Currency into words ( For INDIAN Currency Only )

GeneralRe: Nice onememberNagy Vilmos10-Sep-10 3:23 
HimanshuJoshi wrote:
voted 5

 
Why? It's badly written.


Panic, Chaos, Destruction.
My work here is done.

 
or "Drink. Get drunk. Fall over." - P O'H

GeneralRe: Nice onememberHiren Solanki10-Sep-10 3:26 
I think this Guy have no idea.
trying to ruin my article.
 
Get them out of here.
Hiren Solanki
"Good judgment comes from experience, and experience comes from bad judgment"

My Latest Article
Convert Numeric Currency into words ( For INDIAN Currency Only )

GeneralRe: Nice onememberHimanshuJoshi10-Sep-10 4:04 
Hiren Solanki wrote:
I think this Guy have no idea.
trying to ruin my article.
 
Get them out of here.

 
One thing I should tell you that don't get angry if people dowmvote your article/question/answer. It is their opinion. And senior people here will always provide you some information on why they downvoted your article/question/answer but with some amount of sarcasm.

GeneralRe: Nice onememberNagy Vilmos10-Sep-10 4:14 
Hiren Solanki wrote:
I think this Guy have no idea.

 
So wrong.
 
I know how to use a dictionary by looking up with the key.
I know not to build up strings by appending to an existing string.
 
Sorry bubba, I've been writting SUCCESSFUL comercial code for 20+ years and I've forgotten more than you'e learnt. Your idea is good, but the implimentation poor. I've seen this article before and ignored it as it's "INDIAN ONLY" - code for badly written - but your moaning resulted in a response.
 
Do you understand? I'e down voted your article because it is not very good.


Panic, Chaos, Destruction.
My work here is done.

 
or "Drink. Get drunk. Fall over." - P O'H

GeneralRe: Nice onememberHiren Solanki10-Sep-10 4:21 
I am accepting your comments and taking your vote of 2 seriously and working it to next level to optimize.
 
I am not againist your vote. If you have given me 1 only then also i could have understand because you are Very Much experianced,
 
But I made the statement because without knowing you were supporting the person who have given me vote of 3 ( still i am not against the vote )
 
But could you just look to his profile the way he had posted 18 msg in 6 year.( you will not support to his comment )
 
Thanks Anyway (and sorry for my bad words for you )
Hiren Solanki
"Good judgment comes from experience, and experience comes from bad judgment"

My Latest Article
Convert Numeric Currency into words ( For INDIAN Currency Only )

GeneralRe: Nice onememberHimanshuJoshi10-Sep-10 3:58 
Nagy Vilmos wrote:
Why? It's badly written.

 
You don't know this but we had a discussion in Indian lounge about this one, the purpose is to encouorage new people to get involved and get better at writing articles, posting forum messages. The author is really getting useful help here, so why vote him down.

GeneralRe: Nice onememberNagy Vilmos10-Sep-10 4:19 
The CODE is poor. It doesn't matter how good or bad the English is, the code is poor and is a bad example.
 
Maybe he's God's Gift to code as far as you're concerned. AS far as my opinion goes [and I am well known on CP] bad grammer can be ignored, but bad code equals a bad article.


Panic, Chaos, Destruction.
My work here is done.

 
or "Drink. Get drunk. Fall over." - P O'H

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 6 Sep 2011
Article Copyright 2010 by Hiren solanki
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid