编程语言
首页 > 编程语言> > C#中小写人民币转大写

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

 

标签:shu,SHU1,C#,中小,大写,ZIFU,Length,SHUZI,tmp1
来源: https://www.cnblogs.com/wangjp-1233/p/10548993.html