zoukankan      html  css  js  c++  java
  • 金额转为大写人民币

     /// <summary>
        /// 转为大写人民币
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public static string ToRMB(string x)
        {
            string ret = "";
            int nnum;
            x = x.Replace("-", "");
            if (x.IndexOf(".") > -1)
            {
                if (x.Length == (x.IndexOf(".") + 2)) x = x + "0";
                nnum = int.Parse(x.Substring(0, x.IndexOf(".")));
                if (x.Substring(x.IndexOf(".") + 1, 2) == "00")
                    ret = ToInt(nnum.ToString()) + "元整";
                else ret = ToInt(nnum.ToString()) + "元" + ToDot(x.Substring(x.IndexOf(".") + 1, 2));
            }
            else
            {
                ret = ToInt(x) + "元整";
            }
            return ret;
        }
        private static char ToNum(char x)
        {
            string strChnNames = "零壹贰叁肆伍陆柒捌玖";
            string strNumNames = "0123456789";
            return strChnNames[strNumNames.IndexOf(x)];
        }
        private static string ChangeInt(string x)
        {
            string[] strArrayLevelNames = new string[4] { "", "拾", "佰", "仟" };
            string ret = "";
            int i;
            for (i = x.Length - 1; i >= 0; i--)
                if (x[i] == '0')
                    ret = ToNum(x[i]) + ret;
                else
                    ret = ToNum(x[i]) + strArrayLevelNames[x.Length - 1 - i] + ret;
            while ((i = ret.IndexOf("零零")) != -1)
                ret = ret.Remove(i, 1);
            if (ret[ret.Length - 1] == '零' && ret.Length > 1)
                ret = ret.Remove(ret.Length - 1, 1);
            if (ret.Length >= 2 && ret.Substring(0, 2) == "壹拾")
                ret = ret.Remove(0, 1);
            return ret;
        }
        private static string ToInt(string x)
        {
            int len = x.Length;
            string ret, temp;
            if (len <= 4)
                ret = ChangeInt(x);
            else if (len <= 8)
            {
                ret = ChangeInt(x.Substring(0, len - 4)) + "万";
                temp = ChangeInt(x.Substring(len - 4, 4));
                if (temp.IndexOf("仟") == -1 && temp != "")
                    ret += "零" + temp;
                else
                    ret += temp;
            }
            else
            {
                ret = ChangeInt(x.Substring(0, len - 8)) + "亿";
                temp = ChangeInt(x.Substring(len - 8, 4));
                if (temp.IndexOf("仟") == -1 && temp != "")
                    ret += "零" + temp;
                else
                    ret += temp;
                ret += "万";
                temp = ChangeInt(x.Substring(len - 4, 4));
                if (temp.IndexOf("仟") == -1 && temp != "")
                    ret += "零" + temp;
                else
                    ret += temp;
            }
            int i;
            if ((i = ret.IndexOf("零万")) != -1)
                ret = ret.Remove(i + 1, 1);
            while ((i = ret.IndexOf("零零")) != -1)
                ret = ret.Remove(i, 1);
            if (ret[ret.Length - 1] == '零' && ret.Length > 1)
                ret = ret.Remove(ret.Length - 1, 1);
            return ret;
        }
        private static string ToDot(string x)
        {
            string ret = "";
            string sdot = "角分";
            if (x.Length > 2) x = x.Substring(0, 2);
            for (int i = 0; i < x.Length; i++)
            {
                ret += ToNum(x[i]) + sdot.Substring(i, 1);
            }
            if (ret.IndexOf("零角") > -1) ret = ret.Substring(2, 2);
            if (ret.IndexOf("零分") > -1) ret = ret.Substring(0, 2);
            return ret;
        }
  • 相关阅读:
    CentOS6.4运维知识点1
    《C#入门详解》刘铁猛——Lesson10-11-12 操作符
    《C#入门详解》刘铁猛——Lesson8-9 方法的定义、调用与调试
    《C#入门详解》刘铁猛——Lesson3-4-5名称空间、类、对象、类成员以及C#基本元素
    《C#入门详解》刘铁猛——Lesson1-2 IDE、各种应用程序
    linq行转列
    json转dataset的另外一种解析方式自动生成guid强关联
    C#缓存
    大json直接序列化成dataset
    数据库中根据仓库数量拆分单据--通过游标实现
  • 原文地址:https://www.cnblogs.com/star250/p/634310.html
Copyright © 2011-2022 走看看