C# Tool Class: Renminbi Amount Converted to Chinese Capital

Keywords: C#

In the design of the system involving financial business, we often encounter the need to automatically convert the amount of RMB in Arabic numerals into capital amount in Chinese. Now there are many websites on the Internet to provide such services, as long as you enter the amount of Arabic numerals in RMB, and then automatically convert it into Chinese capitals. C

Define a tool class RmbHelper, whose main function is to convert the size of RMB. The specific implementation code is as follows:

  /// <summary> 
    /// RmbHelper Converting the amount of RMB in Arabic numeral form into Chinese capitals
    /// </summary> 
    public class RmbHelper
    {
        /// <summary> 
        /// Conversion of Renminbi Value 
        /// </summary> 
        /// <param name="num">Amount of money</param> 
        /// <returns>Return capitalization</returns> 
        public static string CmycurD(decimal num)
        {
            string str1 = "One hundred and fifty-five people were enlisted by Lu Xu.";            //0-9 Corresponding Chinese Characters 
            string str2 = "Ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand and ten thousand"; //Chinese Characters Corresponding to Number Bits 
            string str3 = "";    //From the original num The value taken out of the value 
            string str4 = "";    //String Form of Numbers 
            string str5 = "";  //Renminbi Capital Amount Form 
            int i;    //Loop variable 
            int j;    //num Multiply the value of 100 by the length of the string 
            string ch1 = "";    //Chinese Reading of Numbers 
            string ch2 = "";    //Chinese Character Reading with Number Bits 
            int nzero = 0;  //There are several consecutive zeros to calculate. 
            int temp;            //From the original num The value taken out of the value 
            num = Math.Round(Math.Abs(num), 2);    //take num Take absolute values and round them to two decimal places 
            str4 = ((long)(num * 100)).ToString();        //take num Multiply 100 and convert to string form 
            j = str4.Length;      //Find the highest position 
            if (j > 15) { return "overflow"; }
            str2 = str2.Substring(15 - j);   //Remove the corresponding digit str2 The value. Such as: 200.55,j For 5, so str2=Divide all the money in one's pocket 
            //Loop out each value that needs to be converted 
            for (i = 0; i < j; i++)
            {
                str3 = str4.Substring(i, 1);          //Remove the value of a bit to be converted 
                temp = Convert.ToInt32(str3);      //Converting to Digital 
                if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))
                {
                    //When the number of digits taken is not more than the number of yuan, tens of thousands, billions, trillions 
                    if (str3 == "0")
                    {
                        ch1 = "";
                        ch2 = "";
                        nzero = nzero + 1;
                    }
                    else
                    {
                        if (str3 != "0" && nzero != 0)
                        {
                            ch1 = "Zero" + str1.Substring(temp * 1, 1);
                            ch2 = str2.Substring(i, 1);
                            nzero = 0;
                        }
                        else
                        {
                            ch1 = str1.Substring(temp * 1, 1);
                            ch2 = str2.Substring(i, 1);
                            nzero = 0;
                        }
                    }
                }
                else
                {
                    //This position is the key position of trillion, billion, million, yuan, etc. 
                    if (str3 != "0" && nzero != 0)
                    {
                        ch1 = "Zero" + str1.Substring(temp * 1, 1);
                        ch2 = str2.Substring(i, 1);
                        nzero = 0;
                    }
                    else
                    {
                        if (str3 != "0" && nzero == 0)
                        {
                            ch1 = str1.Substring(temp * 1, 1);
                            ch2 = str2.Substring(i, 1);
                            nzero = 0;
                        }
                        else
                        {
                            if (str3 == "0" && nzero >= 3)
                            {
                                ch1 = "";
                                ch2 = "";
                                nzero = nzero + 1;
                            }
                            else
                            {
                                if (j >= 11)
                                {
                                    ch1 = "";
                                    nzero = nzero + 1;
                                }
                                else
                                {
                                    ch1 = "";
                                    ch2 = str2.Substring(i, 1);
                                    nzero = nzero + 1;
                                }
                            }
                        }
                    }
                }
                if (i == (j - 11) || i == (j - 3))
                {
                    //If the bit is a billion or a bit, it must be written 
                    ch2 = str2.Substring(i, 1);
                }
                str5 = str5 + ch1 + ch2;
                if (i == j - 1 && str3 == "0")
                {
                    //When the last place (score) is 0, add "whole" 
                    str5 = str5 + 'whole';
                }
            }
            if (num == 0)
            {
                str5 = "Zero integer";
            }
            return str5;
        }
        /**/
        /// <summary> 
        /// An overload that converts a string to a number before calling CmycurD(decimal num) 
        /// </summary> 
        /// <param name="num">User input amount, string form not converted to decimal</param> 
        /// <returns></returns> 
        public static string CmycurD(string numstr)
        {
            try
            {
                decimal num = Convert.ToDecimal(numstr);
                return CmycurD(num);
            }
            catch
            {
                return "Non-digital form!";
            }
        }
    } 

 

Note: The original text is reproduced from C# Tool Class: Renminbi Amount Converted to Chinese Capital.

Posted by Rayne on Sun, 31 Mar 2019 18:12:29 -0700