需要将用英语写成的金额转换成阿拉伯语。有没有可能使用C#的方法?

时间:2016-01-24 10:47:37

标签: c# asp.net webforms cultureinfo data-conversion

我正在尝试将数字输入的数量转换为英语语言,并且工作正常。现在我想将其转换为阿拉伯语语言。由于用户将在文本框中键入金额,因此会根据提供的文化代码将其转换为英语语言或阿拉伯语语言。 下面是我点击按钮时运行的代码。

 protected void btnConvert_Click(object sender, EventArgs e)
        {
           // CreateDatabase();
            //txtBoxs.Text = sb.ToString();
            string decimalPart = "";
            string fractionPart = "";
            string value = txtBoxs.Text;
            string value1 = txtBoxs.Text;
            string currency = "paisas";
            string cultureCode = "ar-SA";
            int index = 0;
            if (value.Contains("."))
            {
                decimalPart = value.Substring(0, value.IndexOf("."));
                fractionPart = value.Substring(value.IndexOf(".") + 1, value.Length - value.IndexOf(".") - 1);
                if (cultureCode == "ar-SA")
                {
                    //value = "";
                    //foreach (char c in decimalPart)
                    //{
                    //    value += ToIndicDigits(c.ToString());
                    //}
                    //if (fractionPart != "")
                    //{
                    //    value += ".";
                    //    foreach (char c in fractionPart)
                    //    {
                    //        value += ToIndicDigits(c.ToString());
                    //    }
                    //}
                }
                else
                {
                    value = words(Convert.ToInt32(decimalPart));

                    if (fractionPart != "")
                    {
                        sb.Clear();
                        value += " and " + words(Convert.ToInt32(fractionPart)) + " " + currency;
                    }
                }
            }
            else
            {
                if (cultureCode == "ar-SA")
                {
                    //foreach (char c in value1)
                    //{
                    //    value += ToIndicDigits(c.ToString());
                    //}
                }
                else
                {
                    value = words(Convert.ToInt32(value));
                    index = value.ToString().LastIndexOf(" ");
                    int index2 = index > 0 ? value.LastIndexOf(" ", index - 1) : -1;
                    value = value.Insert(index2, " and ");
                }

            }


            lblWords.InnerHtml = value;

        }

这是我用来将金额转换成英语的方法。

 public string words(int number)
        {
            //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;

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

0 个答案:

没有答案
相关问题