ASP.NET轉換人民幣大小金額

來源:互聯網
上載者:User

標籤:ar   os   sp   for   on   bs   amp   as   nbsp   

public class DecimalToRMB
    {
        /// <summary>
        /// 轉換人民幣大小金額
        /// </summary>
        /// <param name="num">金額</param>
        /// <returns>返回大寫形式</returns>
        public static string CmycurD(decimal num)
        {
            string str1 = "零壹貳三肆伍陸柒捌玖";            //0-9所對應的漢字
            string str2 = "萬仟佰拾億仟佰拾萬仟佰拾元角分"; //數字位所對應的漢字
            string str3 = "";    //從原num值中取出的值
            string str4 = "";    //數位字串形式
            string str5 = "";  //人民幣大寫金額形式
            int i;    //迴圈變數
            int j;    //num的值乘以100的字串長度
            string ch1 = "";    //數位漢語讀法
            string ch2 = "";    //數字位的漢字讀法
            int nzero = 0;  //用來計算連續的零值是幾個
            int temp;            //從原num值中取出的值

            num = Math.Round(Math.Abs(num), 2);    //將num取絕對值並四捨五入取2位小數
            str4 = ((long)(num * 100)).ToString();        //將num乘100並轉換成字串形式
            j = str4.Length;      //找出最高位
            if (j > 15) { return "溢出"; }
            str2 = str2.Substring(15 - j);   //取出對應位元的str2的值。如:200.55,j為5所以str2=佰拾元角分

            //迴圈取出每一位需要轉換的值
            for (i = 0; i < j; i++)
            {
                str3 = str4.Substring(i, 1);          //取出需轉換的某一位的值
                temp = Convert.ToInt32(str3);      //轉換為數字
                if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))
                {
                    //當所取位元不為元、萬、億、萬億上的數字時
                    if (str3 == "0")
                    {
                        ch1 = "";
                        ch2 = "";
                        nzero = nzero + 1;
                    }
                    else
                    {
                        if (str3 != "0" && nzero != 0)
                        {
                            ch1 = "零" + 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
                {
                    //該位是萬億,億,萬,元位等關鍵位
                    if (str3 != "0" && nzero != 0)
                    {
                        ch1 = "零" + 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))
                {
                    //如果該位是億位或元位,則必須寫上
                    ch2 = str2.Substring(i, 1);
                }
                str5 = str5 + ch1 + ch2;

                if (i == j - 1 && str3 == "0")
                {
                    //最後一位(分)為0時,加上“整”
                    str5 = str5 + ‘整‘;
                }
            }
            if (num == 0)
            {
                str5 = "零元整";
            }
            return str5;
        }

        /**/
        /// <summary>
        /// 一個重載,將字串先轉換成數字在調用CmycurD(decimal num)
        /// </summary>
        /// <param name="num">使用者輸入的金額,字串形式未轉成decimal</param>
        /// <returns></returns>
        public static string CmycurD(string numstr)
        {
            try
            {
                decimal num = Convert.ToDecimal(numstr);
                return CmycurD(num);
            }
            catch
            {
                return "非數字形式!";
            }
        }
    }
    }

ASP.NET轉換人民幣大小金額

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.