将数字金额转换成大写金额dll

我是研究僧i 提交于 2020-01-23 19:26:29

  public class MoneyConvertChinese
    {
        /// <summary>
        ///金额转换成大写
        /// </summary>
        /// <param name="LowerMoney">要转换的金额</param>
        /// <returns>大写金额</returns>
        public string MoneyToChinese(string LowerMoney)
        {
            string functionReturnValue = null;
            bool IsNegative = false;//是否是负数
            if (LowerMoney.Trim().Substring(0, 1) == "-")
            {
                LowerMoney = LowerMoney.Trim().Remove(0, 1);
                IsNegative = true;
            }
            try
            {
                string strLower = null;
                string strUPart = null;
                string strUpper = null;
                int iTemp = 0;
                LowerMoney = Math.Round(double.Parse(LowerMoney), 2).ToString();
                //保留到两位小数
                if (LowerMoney.IndexOf(".") > 0)
                {
                    if (LowerMoney.IndexOf(".") == LowerMoney.Length - 2)
                    {
                        LowerMoney = LowerMoney + "0";
                    }
                }
                else
                {
                    LowerMoney = LowerMoney + ".00";
                }
                strLower = LowerMoney;

                iTemp = 1;
                strUpper = "";
                while (iTemp <= strLower.Length)
                {
                    switch (strLower.Substring(strLower.Length - iTemp, 1))
                    {
                        case ".":
                            strUPart = "圆";
                            break;
                        case "0":
                            strUPart = "零";
                            break;
                        case "1":
                            strUPart = "壹";
                            break;
                        case "2":
                            strUPart = "贰";
                            break;
                        case "3":
                            strUPart = "叁";
                            break;
                        case "4":
                            strUPart = "肆";
                            break;
                        case "5":
                            strUPart = "伍";
                            break;
                        case "6":
                            strUPart = "陆";
                            break;
                        case "7":
                            strUPart = "柒";
                            break;
                        case "8":
                            strUPart = "捌";
                            break;
                        case "9":
                            strUPart = "玖";
                            break;
                    }
                    switch (iTemp)
                    {
                        case 1:
                            strUPart = strUPart + "分";
                            break;
                        case 2:
                            strUPart = strUPart + "角";
                            break;
                        case 3:
                            strUPart = strUPart + "";
                            break;
                        case 4:
                            strUPart = strUPart + "";
                            break;
                        case 5:
                            strUPart = strUPart + "拾";
                            break;
                        case 6:
                            strUPart = strUPart + "佰";
                            break;
                        case 7:
                            strUPart = strUPart + "仟";
                            break;
                        case 8:
                            strUPart = strUPart + "万";
                            break;
                        case 9:
                            strUPart = strUPart + "拾";
                            break;
                        case 10:
                            strUPart = strUPart + "佰";
                            break;
                        case 11:
                            strUPart = strUPart + "仟";
                            break;
                        case 12:
                            strUPart = strUPart + "亿";
                            break;
                        case 13:
                            strUPart = strUPart + "拾";
                            break;
                        case 14:
                            strUPart = strUPart + "佰";
                            break;
                        case 15:
                            strUPart = strUPart + "仟";
                            break;
                        case 16:
                            strUPart = strUPart + "万";
                            break;
                        default:
                            strUPart = strUPart + "";
                            break;
                    }
                    strUpper = strUPart + strUpper;
                    iTemp = iTemp + 1;
                }

                strUpper = strUpper.Replace("零拾", "零");
                strUpper = strUpper.Replace("零佰", "零");
                strUpper = strUpper.Replace("零仟", "零");
                strUpper = strUpper.Replace("零零零", "零");
                strUpper = strUpper.Replace("零零", "零");
                strUpper = strUpper.Replace("零角零分", "整");
                strUpper = strUpper.Replace("零分", "整");
                strUpper = strUpper.Replace("零角", "零");
                strUpper = strUpper.Replace("零亿零万零圆", "亿圆");
                strUpper = strUpper.Replace("亿零万零圆", "亿圆");
                strUpper = strUpper.Replace("零亿零万", "亿");
                strUpper = strUpper.Replace("零万零圆", "万圆");
                strUpper = strUpper.Replace("万零圆", "万圆");
                strUpper = strUpper.Replace("零亿", "亿");
                strUpper = strUpper.Replace("零万", "万");
                strUpper = strUpper.Replace("零圆", "圆");
                strUpper = strUpper.Replace("零零", "零");
                //对壹圆以下的金额的处理
                if (strUpper.Substring(0, 1) == "圆")
                {
                    strUpper = strUpper.Substring(1, strUpper.Length - 1);
                }

                if (strUpper.Substring(0, 1) == "零")
                {
                    strUpper = strUpper.Substring(1, strUpper.Length - 1);
                }

                if (strUpper.Substring(0, 1) == "角")
                {
                    strUpper = strUpper.Substring(1, strUpper.Length - 1);
                }

                if (strUpper.Substring(0, 1) == "分")
                {
                    strUpper = strUpper.Substring(1, strUpper.Length - 1);
                }

                if (strUpper.Substring(0, 1) == "整")
                {
                    strUpper = "零圆整";
                }
                functionReturnValue = strUpper;
            }
            catch
            {
                functionReturnValue = "零圆整";
            }
            if (IsNegative == true)
            {
                return "负" + functionReturnValue;
            }
            else
            {
                return functionReturnValue;
            }
        }
    }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!