Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert amount to rupees and paise in words format in c#

Tags:

c#

windows

I have windows application in which i need to convert amount entered in text-box into the words in ruppes and paise format. for e.g My amount is 2356.54 then it should display two thousand three hundred fifty six ruppes and fifty four paise only however, i have code that convert amt to words but i am not able to show paise.i am including my code for reference purpose.

private void btntowords_Click(object sender, EventArgs e)
        {
           MessageBox.Show( words(Convert.ToInt32(textBox1.Text)));
        }

        public string words(int numbers)
        {
            int number = numbers;

            if (number == 0) return "Zero";
            if (number == -2147483648) return "Minus Two Hundred and Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred and Forty Eight";
            int[] num = new int[4];
            int first = 0;
            int u, h, t;
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            if (number < 0)
            {
                sb.Append("Minus ");
                number = -number;
            }
            string[] words0 = {"" ,"One ", "Two ", "Three ", "Four ",
"Five " ,"Six ", "Seven ", "Eight ", "Nine "};
            string[] words1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ",
"Fifteen ","Sixteen ","Seventeen ","Eighteen ", "Nineteen "};
            string[] words2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ",
"Seventy ","Eighty ", "Ninety "};
            string[] words3 = { "Thousand ", "Lakh ", "Crore " };
            num[0] = number % 1000; // units
            num[1] = number / 1000;
            num[2] = number / 100000;
            num[1] = num[1] - 100 * num[2]; // thousands
            num[3] = number / 10000000; // crores
            num[2] = num[2] - 100 * num[3]; // lakhs
            for (int i = 3; i > 0; i--)
            {
                if (num[i] != 0)
                {
                    first = i;
                    break;
                }
            }
            for (int i = first; i >= 0; i--)
            {
                if (num[i] == 0) continue;
                u = num[i] % 10; // ones
                t = num[i] / 10;
                h = num[i] / 100; // hundreds
                t = t - 10 * h; // tens
                if (h > 0) sb.Append(words0[h] + "Hundred ");
                if (u > 0 || t > 0)
                {
                    if (h > 0 || i == 0) sb.Append("and ");
                    if (t == 0)
                        sb.Append(words0[u]);
                    else if (t == 1)
                        sb.Append(words1[u]);
                    else
                        sb.Append(words2[t - 2] + words0[u]);
                }
                if (i != 0) sb.Append(words3[i - 1]);
            }
            return sb.ToString().TrimEnd();
        }

It should not show pasie if it has amount like this 2356.00 so i have tried by many ways to get paise but not succeed. i have tried ggogle but not getting exactly what i want.

like image 951
sp_m Avatar asked Feb 06 '12 06:02

sp_m


1 Answers

You need to separate the decimal number and get two separate values one before decimal place and one after it. For example in 56.2 you get 56 separately and 2 separately and call you words() function for both of them. You'll get two strings one "Fifty six" and second "two". You can join these strings to say "Fifty six rupee 2 paisas".

like image 136
Mujtaba Hassan Avatar answered Oct 26 '22 11:10

Mujtaba Hassan