Monday, February 3, 2014
0 comments

Covert Number To Word(for BD)

12:52 PM
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_croresF_Lakh, etc.). "Inwords" also checks whether there is any (.) for "paisa". Here is the code:

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 then "Word_Spell_Tens" used to join them and return a word like "Twenty One". Here is the sample code:

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.

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:
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;
        }

Collected....


0 comments:

 
Toggle Footer