.net 金额中文大写 日期转中文

金额中文大写

        #region 中文大写
        /// <summary>
        /// 返回中文数字 ,如壹佰元整
        /// </summary>
        /// <param name="valIn"></param>
        /// <param name="strType">0返回金额写法,1返回数量写法</param>
        /// <returns></returns>
        public static string GetChineseNum(decimal valIn, int strType = 0)
        {

            string m_1, m_2, m_3, m_4, m_5, m_6, m_7, m_8, m_9;

            string numNum = "0123456789.";
            string numChina = "零壹贰叁肆伍陆柒捌玖点";
            string numChinaWeigh = "个拾佰仟万拾佰仟亿拾佰仟万";


            //m_1.Format("%.2f",   atof(m_1)); 
            m_1 = valIn.ToString("f2");
            m_2 = m_1;
            m_3 = m_4 = "";
            //m_2:1234-> 壹贰叁肆 
            for (int i = 0; i < 11; i++)
            {
                //m_2=m_2.Replace(numNum.Mid(i,   1),   numChina.Mid(i   *   2,   2)); 
                m_2 = m_2.Replace(numNum.Substring(i, 1), numChina.Substring(i, 1));
            }

            //m_3:佰拾万仟佰拾个 
            int iLen = m_1.Length;
            if (iLen > 16)
            {
                m_8 = m_9 = "越界错!";
                throw new Exception("数字太大,超出处理范围");
            }
            if (m_1.IndexOf('.') > 0)
                iLen = m_1.IndexOf('.');

            for (int j = iLen; j >= 1; j--)
            {
                m_3 += numChinaWeigh.Substring(j - 1, 1);
            }
            //m_4:2行+3行 

            for (int i = 0; i < m_3.Length; i++)
            {
                m_4 += m_2.Substring(i, 1) + m_3.Substring(i, 1);
            }
            //m_5:4行去"0"后拾佰仟 

            m_5 = m_4;
            m_5 = m_5.Replace("零拾", "");
            m_5 = m_5.Replace("零佰", "");
            m_5 = m_5.Replace("零仟", "");
            //m_6:00-> 0,000-> 0 

            m_6 = m_5;
            for (int i = 0; i < iLen; i++)
                m_6 = m_6.Replace("零零", "");

            //m_7:6行去亿,万,个位"0" 
            m_7 = m_6;
            m_7 = m_7.Replace("亿零万零", "亿零");
            m_7 = m_7.Replace("亿零万", "亿零");
            m_7 = m_7.Replace("零亿", "亿");
            m_7 = m_7.Replace("零万", "");
            if (m_7.Length > 2)
                m_7 = m_7.Replace("零个", "");

            //m_8:7行+2行小数-> 数目 
            m_8 = m_7;
            m_8 = m_8.Replace("", "");
            if (m_2.Substring(m_2.Length - 3, 3) != "点零零")
                m_8 += m_2.Substring(m_2.Length - 3, 3);

            //m_9:7行+2行小数-> 价格 
            m_9 = m_7;
            m_9 = m_9.Replace("", "");
            if (m_2.Substring(m_2.Length - 3, 3) != "点零零")
            {
                m_9 += m_2.Substring(m_2.Length - 2, 2);
                m_9 = m_9.Insert(m_9.Length - 1, "");
                m_9 += "";
            }
            else m_9 += "";
            if (m_9 != "零元整")
                m_9 = m_9.Replace("零元", "");
            m_9 = m_9.Replace("零分", "");
            if (strType == 1) //数量
                return m_8;
            else
                return m_9;

        }
        #endregion
View Code

 

日期转中文

 

        #region 日期转中文

        /// <summary>
        /// 日期转中文
        /// </summary>
        /// <param name="strDate">日期</param>
        /// <returns></returns>
        public static string GetChineseDate(string strDate)
        {
            char[] strChinese = new char[] {
                 '','','','','','','','','','',''
             };
            StringBuilder result = new StringBuilder();

            //// 依据正则表达式判断参数是否正确
            //Regex theReg = new Regex(@"(d{2}|d{4})(/|-)(d{1,2})(/|-)(d{1,2})");

            if (!string.IsNullOrEmpty(strDate))
            {
                // 将数字日期的年月日存到字符数组str中
                string[] str = null;
                if (strDate.Contains("-"))
                {
                    str = strDate.Split('-');
                }
                else if (strDate.Contains("/"))
                {
                    str = strDate.Split('/');
                }
                // str[0]中为年,将其各个字符转换为相应的汉字
                for (int i = 0; i < str[0].Length; i++)
                {
                    result.Append(strChinese[int.Parse(str[0][i].ToString())]);
                }
                result.Append("");

                // 转换月
                int month = int.Parse(str[1]);
                int MN1 = month / 10;
                int MN2 = month % 10;

                if (MN1 > 1)
                {
                    result.Append(strChinese[MN1]);
                }
                if (MN1 > 0)
                {
                    result.Append(strChinese[10]);
                }
                if (MN2 != 0)
                {
                    result.Append(strChinese[MN2]);
                }
                result.Append("");

                // 转换日
                int day = int.Parse(str[2]);
                int DN1 = day / 10;
                int DN2 = day % 10;

                if (DN1 > 1)
                {
                    result.Append(strChinese[DN1]);
                }
                if (DN1 > 0)
                {
                    result.Append(strChinese[10]);
                }
                if (DN2 != 0)
                {
                    result.Append(strChinese[DN2]);
                }
                result.Append("");
            }
            else
            {
                throw new ArgumentException();
            }

            return result.ToString();
        }

        #endregion
View Code

 

posted @ 2018-08-24 09:33  bxzjzg  阅读(340)  评论(0编辑  收藏  举报