Click here to Skip to main content
15,884,064 members
Articles / Programming Languages / C# 3.5

Convert Amount in Words and Separate Amount with comma in Bangladeshi Currency Format using C#.NET

Rate me:
Please Sign up or sign in to vote.
4.88/5 (19 votes)
26 Feb 2012CPOL3 min read 60.4K   3.5K   12   15
Convert amount in word for Bangladeshi currency

Introduction

This article will help you to spell amount in Bangladeshi currency format, and also help to separate amount in Bangladeshi format.

Background

I am from Bangladesh, and as a programmer I have spent lots of time to spell amount in Bangladeshi currency format as well as amount separator. For this reason, I have created a DLL file from which anyone can spell amount only in Bangladeshi format. I have attached my DLL file with this article.

Using the Code

I have attached an example project named spell_amount.zip with this article which can help you know how it works. I am going to describe what I have done in the example project.

In this project, I have taken two textboxes named txtAmt and txtComma, a label named Label1 and a button named btnConvert. In txtAmt textbox, I will give any amount, and press the btnConvert button, then it shows the amount spelt in the label in red color and also amount separated by comma in the txtComma textbox.

C#
private void btnConvert_Click(object sender, EventArgs e)
      {
          // for spell amount
          label1.Text = Spell.SpellAmount.InWrods(Convert.ToDecimal(txtAmt.Text));
          label1.ForeColor = System.Drawing.Color.Red;
          // for comma
          txtComma.Text = Spell.SpellAmount.comma(Convert.ToDecimal(txtAmt.Text));
      }

Now I will describe how my DLL file works. I have used a class file with several methods here and it is nothing so hard but just a play with String. At first, I have a class named "SpellAmount" and I have given a namespace named "Spell". Now my first method is "InWrods" which is used to collect the given amount in textbox and check the length of the amount, and using the length it passes the amount to the appropriate method like (F_crores, F_Lakh, etc.). "Inwords" also checks whether there is any (.) for "paisa". Here is the code:

C#
public static String InWrods(decimal amount)
      {
          string amt = "";
          string amt_paisa = "";
          string spell = "";
          amt = amount.ToString();
          int aaa = amount.ToString().IndexOf(".", 0);
          amt_paisa = amount.ToString().Substring(aaa + 1);

          if (amt == amt_paisa)
          {
              amt_paisa = "";
          }
          else
          {
              amt = amount.ToString().Substring(0, amount.ToString().IndexOf(".", 0));
              amt = (amt.Replace(",", "")).ToString();
          }

          switch (amt.Length)
          {
              case 9:
                  spell = F_Crores(amt, amt_paisa);
                  break;
              case 8:
                  spell = F_Crore(amt, amt_paisa);
                  break;
              case 7:
                  spell = F_Lakhs(amt, amt_paisa);
                  break;
              case 6:
                  spell = F_Lakh(amt, amt_paisa);
                  break;
              case 5:
                  spell = F_Thousands(amt, amt_paisa);
                  break;
              case 4:
                  spell = F_Thousand(amt, amt_paisa);
                  break;
              case 3:
                  spell = F_Hundred(amt, amt_paisa);
                  break;
              case 2:
                  spell = F_Number(amt, amt_paisa);
                  break;
              case 1:
                  spell = F_Number("0" + amt, amt_paisa);
                  break;
          }
          return spell;
      }

Below methods are actually using the main work, these methods are used to convert the amount in words or you can say spell the amount. The method "Tens" is converting the amount in word, and the method "Word_Spell_Tens" is used to join the word. Just suppose, we have an amount between 1 to 20, when the value passes in the "Tens" method it will return a word very easily, but when we have a value like 21 the method "Tens" can't convert it, and so "Word_Spell_Tens" is needed. This method first checks whether the amount value is greater than 20 and if it is, then it divides the value in two parts, one is 20 and the other is 1, and sends both values in "Tens" method. Now when the "Tens" value returns the similar word for 20 and 1 then "Word_Spell_Tens" used to join them and return a word like "Twenty One". Here is the sample code:

C#
public static String Tens(String s_amt)
        {
            string r_amt = "";
            switch (s_amt)
            {
                case "0":
                    r_amt = "";
                    break;
                case "1":
                    r_amt = "One";
                    break;
                case "2":
                    r_amt = "Two";
                    break;
                case "3":
                    r_amt = "Three";
                    break;
                case "4":
                    r_amt = "Four";
                    break;
                case "5":
                    r_amt = "Five";
                    break;
                case "6":
                    r_amt = "Six";
                    break;
                case "7":
                    r_amt = "Seven";
                    break;
                case "8":
                    r_amt = "Eight";
                    break;
                case "9":
                    r_amt = "Nine";
                    break;
                case "10":
                    r_amt = "Ten";
                    break;
                case "11":
                    r_amt = "Eleven";
                    break;
                case "12":
                    r_amt = "Twelve";
                    break;
                case "13":
                    r_amt = "Thirteen";
                    break;
                case "14":
                    r_amt = "Forteen";
                    break;
                case "15":
                    r_amt = "Fifteen";
                    break;
                case "16":
                    r_amt = "Sixteen";
                    break;
                case "17":
                    r_amt = "Seventeen";
                    break;
                case "18":
                    r_amt = "Eighteen";
                    break;
                case "19":
                    r_amt = "Nineteen";
                    break;
                case "20":
                    r_amt = "Twenty";
                    break;
                case "30":
                    r_amt = "Thirty";
                    break;
                case "40":
                    r_amt = "Forty";
                    break;
                case "50":
                    r_amt = "Fifty";
                    break;
                case "60":
                    r_amt = "Sixty";
                    break;
                case "70":
                    r_amt = "Seventy";
                    break;
                case "80":
                    r_amt = "Eighty";
                    break;
                case "90":
                    r_amt = "Ninety";
                    break;
                default:
                    r_amt = "Nothing";
                    break;
            }
            return r_amt;
        }
        public static String Word_Spell_Tens(string amt)
        {
            string a_amt = null;
            string b_amt = null;
            string r1_amt = null;
            int c_amt = 0;
            c_amt = Convert.ToInt32(amt.Substring(0, 2));
            if (c_amt > 20)
            {
                a_amt = amt.Substring(0, 1) + "0";
                b_amt = amt.Substring(1, 1);
                r1_amt = Tens(a_amt) + " " + Tens(b_amt);
            }
            else
            {
                r1_amt = Tens(c_amt.ToString());
            }
            return r1_amt;
        }  

Now the final part, this part will join all the words together and join a word "Taka" in front of every sentence and "Only" at the end of a sentence. Suppose we have a amount like 12003000, the InWrods() will count the length and pass it to the F_Crore(), then it will return "Taka One Crore Twenty Lakhs and Three Thousands Only". For simplifying, I have shown all the techniques in a method.

C#
public static String F_Crore(string amt, string amt_paisa)
{
    string crores = "";
    string lakhs = "";
    string thous = "";
    string hund = "";
    string num = "";
    string paisa = "";
    int s_crores = 0;
    int s_lakhs = 0;
    int s_thou = 0;
    int s_hundred = 0;
    int s_number = 0;
    // ------------------------------------IF THERE IS NO PAISA--------------------------------
    if (amt_paisa == "")
    {
        s_crores = Convert.ToInt32(amt.Substring(0, 1));
        if (s_crores > 1)
        {
            crores = Tens(s_crores.ToString()) + " Crores";
        }
        else
        {
            crores = Tens(s_crores.ToString()) + " Crore";
        }
        if (amt.Substring(1, 7) != "0000000")
        {
            // For Lakh
            if (amt.Substring(1, 2) != "00")
            {
                if (amt.Substring(1, 1) != "0")
                {
                    s_lakhs = Convert.ToInt32(amt.Substring(1, 2));
                    if (amt.Substring(3, 5) == "00000")
                    {
                        lakhs = " and " + Word_Spell_Tens(s_lakhs.ToString()) + " Lakhs";
                    }
                    else
                    {
                        lakhs = " " + Word_Spell_Tens(s_lakhs.ToString()) + " Lakhs";
                    }
                }
                else
                {
                    s_lakhs = Convert.ToInt32(amt.Substring(2, 1));
                    if (amt.Substring(3, 5) == "00000")
                    {
                        lakhs = " and " + Tens(s_lakhs.ToString());
                    }
                    else
                    {
                        lakhs = " " + Tens(s_lakhs.ToString());
                    }
                    if (s_lakhs > 1)
                    {
                        lakhs = lakhs + " Lakhs";
                    }
                    else
                    {
                        lakhs = lakhs + " Lakh";
                    }
                }
            }
            
            // For Thousand
            if (amt.Substring(3, 2) != "00")
            {
                if (amt.Substring(3, 1) != "0")
                {
                    s_thou = Convert.ToInt32(amt.Substring(3, 2));
                    if (amt.Substring(5, 3) == "000")
                    {
                        thous = " and " + Word_Spell_Tens(s_thou.ToString()) + " Thousands";
                    }
                    else
                    {
                        thous = " " + Word_Spell_Tens(s_thou.ToString()) + " Thousands";
                    }
                }
                else
                {
                    s_thou = Convert.ToInt32(amt.Substring(4, 1));
                    if (amt.Substring(5, 3) == "000")
                    {
                        thous = " and " + Tens(s_thou.ToString());
                    }
                    else
                    {
                        thous = " " + Tens(s_thou.ToString());
                    }
                    if (s_thou > 1)
                    {
                        thous = thous + " Thousands";
                    }
                    else
                    {
                        thous = thous + " Thousand";
                    }
                }
            }
            //For Hundred
            if (amt.Substring(5, 3) != "000")
            {
                if (amt.Substring(5, 1) != "0")
                {
                    s_hundred = Convert.ToInt32(amt.Substring(5, 1));
                    if (s_hundred > 1)
                    {
                        if (amt.Substring(6, 2) == "00")
                        {
                            hund = " and" + Tens(s_hundred.ToString()) + " Hundreds";
                        }
                        else
                        {
                            hund = " " + Tens(s_hundred.ToString()) + " Hundreds";
                        }
                    }
                    else
                    {
                        if (amt.Substring(6, 2) == "00")
                        {
                            hund = " and" + Tens(s_hundred.ToString()) + " Hundred";
                        }
                        else
                        {
                            hund = " " + Tens(s_hundred.ToString()) + " Hundred";
                        }
                    }
                }
                // Single Number
                if (amt.Substring(6, 2) != "00")
                {
                    s_number = Convert.ToInt32(amt.Substring(6, 2));
                    if (Convert.ToInt32(amt.Substring(6, 1)) != 0)
                    {
                        num = " and " + Word_Spell_Tens(s_number.ToString());
                    }
                    else
                    {
                        num = " and " + Tens(s_number.ToString());
                    }
                }
            }
        }
    }
    else if (amt_paisa != "")
    {
        // --------------------------------- IF THERE IS PAISA ----------------------
        s_crores = Convert.ToInt32(amt.Substring(0, 1));
        if (s_crores > 1)
        {
            crores = Tens(s_crores.ToString()) + " Crores";
        }
        else
        {
            crores = Tens(s_crores.ToString()) + " Crore";
        }
        
        if (amt.Substring(1, 7) != "0000000")
        {
            // For Lakh
            if (amt.Substring(1, 2) != "00")
            {
                if (amt.Substring(1, 1) != "0")
                {
                    s_lakhs = Convert.ToInt32(amt.Substring(1, 2));
                    lakhs = " " + Word_Spell_Tens(s_lakhs.ToString()) + " Lakhs";
                }
                else
                {
                    s_lakhs = Convert.ToInt32(amt.Substring(2, 1));
                    if (s_lakhs > 1)
                    {
                        lakhs = " " + Tens(s_lakhs.ToString()) + " Lakhs";
                    }
                    else
                    {
                        lakhs = " " + Tens(s_lakhs.ToString()) + " Lakh";
                    }
                }
            }
            // For Thousand
            if (amt.Substring(3, 2) != "00")
            {
                if (amt.Substring(3, 1) != "0")
                {
                    s_thou = Convert.ToInt32(amt.Substring(3, 2));
                    thous = " " + Word_Spell_Tens(s_thou.ToString()) + " Thousands";
                }
                else
                {
                    s_thou = Convert.ToInt32(amt.Substring(4, 1));
                    if (s_thou > 1)
                    {
                        thous = " " + Tens(s_thou.ToString()) + " Thousands";
                    }
                    else
                    {
                        thous = " " + Tens(s_thou.ToString()) + " Thousand";
                    }
                }
            }
            //For Hundred
            if (amt.Substring(5, 3) != "000")
            {
                if (amt.Substring(5, 1) != "0")
                {
                    s_hundred = Convert.ToInt32(amt.Substring(5, 1));
                    if (s_hundred > 1)
                    {
                        hund = " " + Tens(s_hundred.ToString()) + " Hundreds";
                    }
                    else
                    {
                        hund = " " + Tens(s_hundred.ToString()) + " Hundred";
                    }
                }
                
                if (amt.Substring(6, 2) != "00")
                {
                    s_number = Convert.ToInt32(amt.Substring(6, 2));
                    if (amt.Substring(6, 1) != "0")
                    {
                        num = " " + Word_Spell_Tens(s_number.ToString());
                    }
                    else
                    {
                        num = " " + Tens(s_number.ToString());
                    }
                }
            }
        }
        if (amt_paisa.Substring(0, 2) != "00")
        {
            if (amt_paisa.Substring(0, 1) != "0")
            {
                paisa = " and " + Word_Spell_Tens(amt_paisa.Substring(0, 2)) + " Paisa";
            }
            else
            {
                paisa = " " + Tens(amt_paisa.Substring(0, 2)) + " Paisa";
            }
        }
    }
    return "Taka " + crores + lakhs + thous + hund + num + paisa + " Only";
}  

Now I am going to use how to separate amount using comma (,) in Bangladeshi Currency Format. I Googled a lot and when I couldn't find any suitable solution, I made my own method named comma(). It also work as InWrods(), counts the length, then sets a comma where it should be. As for example, we have an amount like 12034567 and when it goes from Comma() it will return 1,20,34,567. Here is the code:

C#
public static String comma(decimal amount)
        {
            string result = "";
            string amt = "";
            string amt_paisa = "";
            
            amt = amount.ToString();
            int aaa = amount.ToString().IndexOf(".", 0);
            amt_paisa = amount.ToString().Substring(aaa + 1);

            if (amt == amt_paisa)
            {
                amt_paisa = "";
            }
            else
            {
                amt = amount.ToString().Substring(0, amount.ToString().IndexOf(".", 0));
                amt = (amt.Replace(",", "")).ToString();
            }
            switch (amt.Length)
            {
                case 9:
                    if (amt_paisa == "")
                    {
                        result = amt.Substring(0, 2) + "," + amt.Substring(2, 2) + "," + 
                                 amt.Substring(4, 2) + "," + amt.Substring(6, 3);
                    }
                    else
                    {
                        result = amt.Substring(0, 2) + "," + amt.Substring(2, 2) + "," + 
                                 amt.Substring(4, 2) + "," + amt.Substring(6, 3) + "." + 
                                 amt_paisa;
                    }
                    break;
                case 8:
                    if (amt_paisa == "")
                    {
                        result = amt.Substring(0, 1) + "," + amt.Substring(1, 2) + "," + 
                                 amt.Substring(3, 2) + "," + amt.Substring(5, 3);
                    }
                    else
                    {
                        result = amt.Substring(0, 1) + "," + amt.Substring(1, 2) + "," + 
                                 amt.Substring(3, 2) + "," + amt.Substring(5, 3) + "." + 
                                 amt_paisa;
                    }
                    break;
                case 7:
                    if (amt_paisa == "")
                    {
                        result = amt.Substring(0, 2) + "," + amt.Substring(2, 2) + "," + 
                                 amt.Substring(4, 3);
                    }
                    else
                    {
                        result = amt.Substring(0, 2) + "," + amt.Substring(2, 2) + "," + 
                                 amt.Substring(4, 3) + "." + amt_paisa;
                    }
                    break;
                case 6:
                    if (amt_paisa == "")
                    {
                        result = amt.Substring(0, 1) + "," + amt.Substring(1, 2) + "," + 
                                 amt.Substring(3, 3);
                    }
                    else
                    {
                        result = amt.Substring(0, 1) + "," + amt.Substring(1, 2) + "," + 
                                 amt.Substring(3, 3) + "." + amt_paisa;
                    }
                    break;
                case 5:
                    if (amt_paisa == "")
                    {
                        result = amt.Substring(0, 2) + "," + amt.Substring(2, 3);
                    }
                    else
                    {
                        result = amt.Substring(0, 2) + "," + amt.Substring(2, 3) + "." + 
                                 amt_paisa;
                    }
                    break;
                case 4:
                    if (amt_paisa == "")
                    {
                        result = amt.Substring(0, 1) + "," + amt.Substring(1, 3);
                    }
                    else
                    {
                        result = amt.Substring(0, 1) + "," + amt.Substring(1, 3) + "." + 
                                 amt_paisa;
                    }
                    break;
                default:
                    if (amt_paisa == "")
                    {
                        result = amt;
                    }
                    else
                    {
                        result = amt + "." + amt_paisa;
                    }
                    break;
            }
            return result;
        } 

Points of Interest

Most of our Bangladeshi customers want their amount in local format as well as convert currency amount in words. When I was developing a Point of Sale (POS) for my customer, I needed this very badly.

There are a lot of code examples for converting currency in words but unfortunately we don't have any example for our Bangladeshi currency. So, I have created this for all my Bangladeshi programmers so that they don't have to waste their time doing this again.

Good luck programmers.

License

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


Written By
Software Developer (Senior) Grameen Communications
Bangladesh Bangladesh
I have been working in .Net platform over 4 years, I am expert in both Desktop and Web Application. I used both C# and VB for my code. I love to face challenge in coding, because I always believe that "Failure doesn't kill you... it increases your desire to make something happen.

Comments and Discussions

 
QuestionThanking for Program Pin
Ghanshyam Vadher13-Feb-15 23:39
Ghanshyam Vadher13-Feb-15 23:39 
GeneralMy vote of 4 Pin
ridoy20-Aug-13 10:32
professionalridoy20-Aug-13 10:32 
GeneralMy vote of 5 Pin
Saumitra Kumar Paul16-Nov-12 5:49
Saumitra Kumar Paul16-Nov-12 5:49 
SuggestionComma separated string of amount Pin
ProEnggSoft2-Mar-12 23:50
ProEnggSoft2-Mar-12 23:50 
QuestionGood, but need some changes. Pin
Saleth Prakash26-Feb-12 19:22
Saleth Prakash26-Feb-12 19:22 
AnswerRe: Good, but need some changes. Pin
ProEnggSoft26-Feb-12 21:49
ProEnggSoft26-Feb-12 21:49 
GeneralRe: Good, but need some changes. Pin
Saleth Prakash2-Mar-12 23:49
Saleth Prakash2-Mar-12 23:49 
GeneralRe: Good, but need some changes. Pin
ProEnggSoft2-Mar-12 23:51
ProEnggSoft2-Mar-12 23:51 
SuggestionRe: Good, but need some changes. Pin
Gustav Brock27-Feb-12 3:28
professionalGustav Brock27-Feb-12 3:28 
GeneralMy vote of 5 Pin
Sergio Andrés Gutiérrez Rojas26-Feb-12 16:48
Sergio Andrés Gutiérrez Rojas26-Feb-12 16:48 
GeneralMy vote of 5 Pin
Mahmud Hasan26-Feb-12 16:32
Mahmud Hasan26-Feb-12 16:32 
Good work.
SuggestionInteresting, but where is the source? Pin
Roger Norling1-Jan-12 17:59
Roger Norling1-Jan-12 17:59 
GeneralMy vote of 5 Pin
Mukit, Ataul20-Dec-11 1:01
Mukit, Ataul20-Dec-11 1:01 
QuestionGood work, but need to tighten up.. PinPopular
Mukit, Ataul20-Dec-11 0:56
Mukit, Ataul20-Dec-11 0:56 
GeneralMy vote of 5 Pin
raihansazal19-Dec-11 18:02
raihansazal19-Dec-11 18:02 

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

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