将金额转换为卢比并在 C# 中以文字格式转换为 paise [英] convert amount to rupees and paise in words format in c#

查看:41
本文介绍了将金额转换为卢比并在 C# 中以文字格式转换为 paise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Windows 应用程序,我需要将在文本框中输入的金额转换为 ruppes 和 paise 格式的单词.例如 我的金额是 2356.54 那么它应该显示 2356 卢比和 54 paise 但是,我有代码可以转换amt to words 但我无法显示 paise.我包含我的代码以供参考.

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();
        }

如果它有这样的数量,它不应该显示 pasie 2356.00所以我尝试了很多方法来获得 paise 但没有成功.我试过 ggogle 但没有得到我想要的.

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.

推荐答案

您需要将十进制数分开并得到两个单独的值,一个在小数位前,一个在小数位后.例如,在 56.2 中,您分别获得 56 个和 2 个,并为它们调用 words() 函数.你会得到两个字符串,一个是五十六",第二个是二".您可以将这些字符串连接起来说56 卢比 2 派萨".

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".

这篇关于将金额转换为卢比并在 C# 中以文字格式转换为 paise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆