C#中小写人民币转大写

  /// <summary>
        /// 转换成大写人民币
        /// </summary>
        /// <param name="myMoney"></param>
        /// <returns></returns>
        public static string toRmb(System.Decimal myMoney)
        {
            string SHUZI;  //保存數字小寫
            string ZIFU;   //保存數字轉換后的結果
            int tmp1;  //臨時變量
            string[] shu = new String[14];
            string[] SHU1 = new String[10];

            shu[13] = "";
            shu[12] = ""; shu[11] = ""; shu[10] = "亿";
            shu[9] = ""; shu[8] = ""; shu[7] = "";
            shu[6] = ""; shu[5] = ""; shu[4] = "";
            shu[3] = ""; shu[2] = ""; shu[1] = "";
            shu[0] = "";

            SHU1[0] = ""; SHU1[1] = ""; SHU1[2] = "";
            SHU1[3] = ""; SHU1[4] = ""; SHU1[5] = "";
            SHU1[6] = ""; SHU1[7] = ""; SHU1[8] = "";
            SHU1[9] = "";

            bool flag;
            //            bool zeroFlag;
            //            zeroFlag=false;

            //最大值為9999,9999,9999.99
            if (myMoney > Convert.ToDecimal(999999999999.99))//過大時返回原來值
            {
                return Convert.ToString(myMoney) + "元整";
            }
            else if (myMoney == Convert.ToDecimal(0))
            {
                return "零元整";
            }
            else
            {
                flag = false;
                SHUZI = Convert.ToString(myMoney * 100);//先將數值轉化
                if (SHUZI.Substring(0, 1) == "-")//為負數時去掉負號
                {
                    SHUZI = SHUZI.Substring(1);
                }

                if (SHUZI.IndexOf(".") > 0)//當小數位過兩位時,去掉小數位
                {
                    SHUZI = SHUZI.Substring(0, SHUZI.IndexOf("."));
                }

                ZIFU = "";
                tmp1 = 0;  //從最首位開始轉化
                while (tmp1 < SHUZI.Length)
                {
                    if (SHUZI.Substring(tmp1, 1) != "0")//當數字位不為零時,得到當前的 漢數 和 幣字
                    {
                        if (flag)
                            ZIFU += "";
                        ZIFU = ZIFU + SHU1[Convert.ToInt32(SHUZI.Substring(tmp1, 1))] + shu[SHUZI.Length - tmp1 - 1];
                        flag = false;
                    }

                    else//當數字位為零時
                    {
                        flag = true;
                        if (tmp1 == SHUZI.Length - 3)//最未位的單位為 元
                        {
                            ZIFU = ZIFU + shu[2];
                        }

                        if (tmp1 == SHUZI.Length - 7) //以 萬 記
                        {
                            if (ZIFU.Substring(ZIFU.Length - 1, 1) != shu[10])
                            {
                                ZIFU = ZIFU + shu[6];
                                flag = false;
                            }
                        }

                        if (tmp1 == SHUZI.Length - 11)
                        {
                            ZIFU = ZIFU + shu[10];
                            flag = false;
                        }
                    }

                    tmp1 = tmp1 + 1;
                }
            }

            int dotIndex = myMoney.ToString().IndexOf(".");
            if (dotIndex > 0)        //判断是否包含小数点
            {
                String partStr = myMoney.ToString().Substring(dotIndex + 1);
                if (partStr.TrimEnd('0').Length > 1)    //如小数位数大于1(角、分),则不包含最后的“整”。
                {
                    return ZIFU;
                }
                else
                {
                    return ZIFU + "";
                }
            }
            else
            {
                return ZIFU + "";
            }
        }

 

posted @ 2019-03-17 21:27  王小鹏  阅读(421)  评论(0编辑  收藏  举报