如何将123.12数字等十进制数转换为单词卢比并使用C#进行调整 [英] How to convert decimal amount like 123.12 number to word rupees and paise using C#

查看:77
本文介绍了如何将123.12数字等十进制数转换为单词卢比并使用C#进行调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void textWeight_TextChanged(object sender, EventArgs e)
        {
            try
            {

                //textWeight.Text = Double.Parse(textWeight.Text).ToString("N3");

                string s = "select Max(SrNo)+1 From TounchData";
                OleDbCommand csm = new OleDbCommand(s, connection);

                connection.Open();
                csm.ExecuteNonQuery();

                OleDbDataReader dd = csm.ExecuteReader();

                while (dd.Read())
                {
                    int n = dd.GetInt32(0);
                    textSrNo.Text = n.ToString();
                }


                connection.Close();
                //string NumericValue = textTounch.Text;
                //string getString = changeToWords(NumericValue);
                //LabelNoToWord.Text = getString;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex);
                //textTounch.Text = "";
                //LabelNoToWord.Text = "";
            }
            {
                LabelNoToWord.Text = "0";
            }
            if (textWeight.Text != "")
                LabelNoToWord.Text = (AmountInWords(Convert.ToInt32(textWeight.Text)));

        }


        public string AmountInWords(decimal Num)
        {
            string returnValue;
            //I have created this function for converting amount in indian rupees (INR).
            //You can manipulate as you wish like decimal setting, Doller (any currency) Prefix.


            string strNum;
            string strNumDec;
            string StrWord;
            strNum = Num.ToString();


            if (strNum.IndexOf(".") + 1 != 0)
            {
                strNumDec = strNum.Substring(strNum.IndexOf(".") + 2 - 1);


                if (strNumDec.Length == 1)
                {
                    strNumDec = strNumDec + "0";
                }
                if (strNumDec.Length > 2)
                {
                    strNumDec = strNumDec.Substring(0, 2);
                }


                strNum = strNum.Substring(0, strNum.IndexOf(".") + 0);
                StrWord = ((double.Parse(strNum) == 1) ? " Rupee " : " Rupees ") + NumToWord((decimal)(double.Parse(strNum))) + ((double.Parse(strNumDec) > 0) ? (" and Paise" + cWord3((decimal)(double.Parse(strNumDec)))) : "");
            }
            else
            {
                StrWord = ((double.Parse(strNum) == 1) ? " Rupee " : " Rupees ") + NumToWord((decimal)(double.Parse(strNum)));
            }
            returnValue = StrWord + " Only";
            return returnValue;
        }
        static public string NumToWord(decimal Num)
        {
            string returnValue;


            //I divided this function in two part.
            //1. Three or less digit number.
            //2. more than three digit number.
            string strNum;
            string StrWord;
            strNum = Num.ToString();


            if (strNum.Length <= 3)
            {
                StrWord = cWord3((decimal)(double.Parse(strNum)));
            }
            else
            {
                StrWord = cWordG3((decimal)(double.Parse(strNum.Substring(0, strNum.Length - 3)))) + " " + cWord3((decimal)(double.Parse(strNum.Substring(strNum.Length - 2 - 1))));
            }
            returnValue = StrWord;
            return returnValue;
        }
        static public string cWordG3(decimal Num)
        {
            string returnValue;
            //2. more than three digit number.
            string strNum = "";
            string StrWord = "";
            string readNum = "";
            strNum = Num.ToString();
            if (strNum.Length % 2 != 0)
            {
                readNum = System.Convert.ToString(double.Parse(strNum.Substring(0, 1)));
                if (readNum != "0")
                {
                    StrWord = retWord(decimal.Parse(readNum));
                    readNum = System.Convert.ToString(double.Parse("1" + strReplicate("0", strNum.Length - 1) + "000"));
                    StrWord = StrWord + " " + retWord(decimal.Parse(readNum));
                }
                strNum = strNum.Substring(1);
            }
            while (!System.Convert.ToBoolean(strNum.Length == 0))
            {
                readNum = System.Convert.ToString(double.Parse(strNum.Substring(0, 2)));
                if (readNum != "0")
                {
                    StrWord = StrWord + " " + cWord3(decimal.Parse(readNum));
                    readNum = System.Convert.ToString(double.Parse("1" + strReplicate("0", strNum.Length - 2) + "000"));
                    StrWord = StrWord + " " + retWord(decimal.Parse(readNum));
                }
                strNum = strNum.Substring(2);
            }
            returnValue = StrWord;
            return returnValue;
        }
        static public string cWord3(decimal Num)
        {
            string returnValue;
            //1. Three or less digit number.
            string strNum = "";
            string StrWord = "";
            string readNum = "";
            if (Num < 0)
            {
                Num = Num * -1;
            }
            strNum = Num.ToString();


            if (strNum.Length == 3)
            {
                readNum = System.Convert.ToString(double.Parse(strNum.Substring(0, 1)));
                StrWord = retWord(decimal.Parse(readNum)) + " Hundred";
                strNum = strNum.Substring(1, strNum.Length - 1);
            }


            if (strNum.Length <= 2)
            {
                if (double.Parse(strNum) >= 0 && double.Parse(strNum) <= 20)
                {
                    StrWord = StrWord + " " + retWord((decimal)(double.Parse(strNum)));
                }
                else
                {
                    StrWord = StrWord + " " + retWord((decimal)(System.Convert.ToDouble(strNum.Substring(0, 1) + "0"))) + " " + retWord((decimal)(double.Parse(strNum.Substring(1, 1))));
                }
            }


            strNum = Num.ToString();
            returnValue = StrWord;
            return returnValue;
        }
        static public string retWord(decimal Num)
        {
            string returnValue;
            //This two dimensional array store the primary word convertion of number.
            returnValue = "";
            object[,] ArrWordList = new object[,] { { 0, "" }, { 1, "One" }, { 2, "Two" }, { 3, "Three" }, { 4, "Four" }, { 5, "Five" }, { 6, "Six" }, { 7, "Seven" }, { 8, "Eight" }, { 9, "Nine" }, { 10, "Ten" }, { 11, "Eleven" }, { 12, "Twelve" }, { 13, "Thirteen" }, { 14, "Fourteen" }, { 15, "Fifteen" }, { 16, "Sixteen" }, { 17, "Seventeen" }, { 18, "Eighteen" }, { 19, "Nineteen" }, { 20, "Twenty" }, { 30, "Thirty" }, { 40, "Forty" }, { 50, "Fifty" }, { 60, "Sixty" }, { 70, "Seventy" }, { 80, "Eighty" }, { 90, "Ninety" }, { 100, "Hundred" }, { 1000, "Thousand" }, { 100000, "Lakh" }, { 10000000, "Crore" } };


            int i;
            for (i = 0; i <= (ArrWordList.Length - 1); i++)
            {
                if (Num == System.Convert.ToDecimal(ArrWordList[i, 0]))
                {
                    returnValue = (string)(ArrWordList[i, 1]);
                    break;
                }
            }
            return returnValue;
        }
        static public string strReplicate(string str, int intD)
        {
            string returnValue;
            //This fucntion padded "0" after the number to evaluate hundred, thousand and on....
            //using this function you can replicate any Charactor with given string.
            int i;
            returnValue = "";
            for (i = 1; i <= intD; i++)
            {
                returnValue = returnValue + str;
            }
            return returnValue;
        }





我的尝试:



我创建了这个函数用于转换印度卢比(INR)的金额,所以你可以帮助我。

但是我想把金额换成卢比并用c#oledb中的单词格式换算服务器



What I have tried:

I have created this function for converting amount in Indian Rupees (INR) so can you help me.
but i want to convert amount to rupees and paise in words format in c# oledb Server

推荐答案

Conver编号为单词 [ ^ ]



如何在C#中将数字转换成单词 [ ^ ]



这似乎是常年的第一年课堂作业。然后所有的学生都为他们的创作感到骄傲,他们在这里发布。



同样,请考虑老师知道这个网站,并知道你是否只是复制(剽窃)任何这些实现​​。

此外,这些实现都不是完美的,但您可以阅读每个实现并使用您喜欢的概念来形成真正属于您自己的解决方案。





我们有这么多人甚至有恶搞:将数字转换为等价的单词。 [ ^ ]



如何将数值转换为文本(字符串) [ ^ ]



将货币转换为Lakh,Crores等中的单词 [ ^ ]



转换使用C#.NET以孟加拉语货币格式用逗号表示单词和单独金额 [ ^ ]



XSLT Number to String Transformation II [ ^ ]



将数字货币转换为国际货币的单词 - 第二部分(优化) [ ^ ]





将数字转换为英语 [ ^ ]



如何使用C#将数值或货币转换为英文单词 [ ^ ] < br $> b $ b

将数字转换为文本 [ ^ ]



Number to Word(阿拉伯语版) [ ^ ]



http://www.codeproject.com/script/Articles/ArticleVersion.aspx?waid=156342&aid=867081 [ ^ ]
Conver number into word[^]

how to convert number into words in C#[^]

It seems this is a perennial first year class assignment. And then all the students are so proud of their creations that they post them here.

Similarly, please consider that teachers know about this site and will know if you simply copied (plagiarized) any of these implementations.
Furthermore, none of these implementations is perfect, but you can read each and use the concepts you like to form a solution that is truly your own.


We have so many we even have a spoof: Converting numbers to the word equivalent. [^]

How to Convert Numeric Value to Text(String)[^]

Convert Currency To Words in Lakh, Crores, etc.[^]

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

XSLT Number to String Transformation II[^]

Convert Numeric Currency into Words for International Currency - Part - II (Optimized)[^]


Converting numbers into English[^]

How to Convert a Numeric Value or Currency to English Words Using C#[^]

Convert Number to Text[^]

Number To Word (Arabic Version)[^]

http://www.codeproject.com/script/Articles/ArticleVersion.aspx?waid=156342&aid=867081[^]


嘿,你需要解决你的电子邮件地址,我将用库发送整个代码。



hey you need solution your email address , i will send the whole code with library.

private void button2_Click(object sender, EventArgs e)
        {
            GenerateWordsinRs();

        }

        private void GenerateWordsinRs()
        {
            decimal numberrs = Convert.ToDecimal(txtNumber.Text);
            CultureInfo ci = new CultureInfo("en-IN");
            string aaa = String.Format("{0:#,##0.##}", numberrs);
            aaa = aaa + " " + ci.NumberFormat.CurrencySymbol.ToString();
            label6.Text = aaa;


            string input = txtNumber.Text;
            string a = "";
            string b = "";

            // take decimal part of input. convert it to word. add it at the end of method.
            string decimals = "";

            if (input.Contains("."))
            {
                decimals = input.Substring(input.IndexOf(".") + 1);
                // remove decimal part from input
                input = input.Remove(input.IndexOf("."));

            }
            string strWords = NUMBERTOWORDS.NumbersToWords(Convert.ToInt32(input));

            if (!txtNumber.Text.Contains("."))
            {
                a = strWords + " Rupees Only";
            }
            else
            {
                a = strWords + " Rupees";
            }

            if (decimals.Length > 0)
            {
                // if there is any decimal part convert it to words and add it to strWords.
                string strwords2 = NUMBERTOWORDS.NumbersToWords(Convert.ToInt32(decimals));
                b = " and " + strwords2 + " Paisa Only ";
            }

            label7.Text = a + b;
        }   
//NUMBERTOWORDS.NumbersToWords(Convert.ToInt32(input)); //NUMBERTOWORDS IS A CLASS

//NUMBERTOWORDS CLASS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace rounfoffmath
{
    class NUMBERSTOWORDS
    {
        public static string NumbersToWords(int inputNumber)
        {
            int inputNo = inputNumber;

            if (inputNo == 0)
                return "Zero";

            int[] numbers = new int[4];
            int first = 0;
            int u, h, t;
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            if (inputNo < 0)
            {
                sb.Append("Minus ");
                inputNo = -inputNo;
            }

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

            numbers[0] = inputNo % 1000; // units
            numbers[1] = inputNo / 1000;
            numbers[2] = inputNo / 100000;
            numbers[1] = numbers[1] - 100 * numbers[2]; // thousands
            numbers[3] = inputNo / 10000000; // crores
            numbers[2] = numbers[2] - 100 * numbers[3]; // lakhs

            for (int i = 3; i > 0; i--)
            {
                if (numbers[i] != 0)
                {
                    first = i;
                    break;
                }
            }
            for (int i = first; i >= 0; i--)
            {
                if (numbers[i] == 0) continue;
                u = numbers[i] % 10; // ones
                t = numbers[i] / 10;
                h = numbers[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("");
                    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();
        }
    }
}
// enJOY tHIS code, have fun, try it to convert amount into indian currency and words into indian formats. :) developed by shaan


I found that,

Calling two functions can convert decimal amount like 123.12 number to word rupees and paise.





this is the code for conversion



give any value to amount and it will convert





I found that,
Calling two functions can convert decimal amount like 123.12 number to word rupees and paise.


this is the code for conversion

give any value to amount and it will convert


public conversion(string amount)
        {
double m = Convert.ToInt64(Math.Floor(Convert.ToDouble(amount)));
            double l = Convert.ToDouble(amount);
            
            double j = ( l - m ) * 100;
           

            var beforefloating = ConvertNumbertoWords(Convert.ToInt64(m));
            var afterfloating = ConvertNumbertoWords(Convert.ToInt64(j));



            var Content = beforefloating + ' ' + " RUPEES" + ' ' + afterfloating + ' ' + " PAISE only";

}









main code of this Program







main code of this Program

public string ConvertNumbertoWords(long number)
     {
         if (number == 0) return "ZERO";
         if (number < 0) return "minus " + ConvertNumbertoWords(Math.Abs(number));
         string words = "";
         if ((number / 1000000) > 0)
         {
             words += ConvertNumbertoWords(number / 100000) + " LAKES ";
             number %= 1000000;
         }
         if ((number / 1000) > 0)
         {
             words += ConvertNumbertoWords(number / 1000) + " THOUSAND ";
             number %= 1000;
         }
         if ((number / 100) > 0)
         {
             words += ConvertNumbertoWords(number / 100) + " HUNDRED ";
             number %= 100;
         }
         //if ((number / 10) > 0)  
         //{  
         // words += ConvertNumbertoWords(number / 10) + " RUPEES ";  
         // number %= 10;  
         //}  
         if (number > 0)
         {
             if (words != "") words += "AND ";
             var unitsMap = new[]   
        {  
            "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN"  
        };
             var tensMap = new[]   
        {  
            "ZERO", "TEN", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY"  
        };
             if (number < 20) words += unitsMap[number];
             else
             {
                 words += tensMap[number / 10];
                 if ((number % 10) > 0) words += " " + unitsMap[number % 10];
             }
         }
         return words;
     }







like this two time calling the same function can generate words for decimal numbers




like this two time calling the same function can generate words for decimal numbers


这篇关于如何将123.12数字等十进制数转换为单词卢比并使用C#进行调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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