C# 数字 转换 大写汉字

2021-01-02 21:28

阅读:418

标签:ram   throw   length   bool   i++   ret   div   char   this   

调用方法

  double m;
            try
            {
                m = double.Parse(GetCurrentColumnValue("总金额").ToString());
                Money money = new Money(m);
                label3.Text ="金额(大写):   "+ money.Convert(); 
            }
            catch (FormatException ex)
            { 
            }

 

 

 

 

public class Money
    {


        /// 
        /// 要转换的数字
        /// 
        private double j;
        /// 
        ///
        /// 
        private string[] NumChineseCharacter = new string[] { "", "", "", "", "", "", "", "", "", "" };

        public Money(double m)
        {
            this.j = m;
           
        }
        /// 
        /// 判断输入的数字是否大于double类型
        /// 
        private bool IsNumber
        {
            get
            {
                if (j > double.MaxValue || j 0)
                    return false;
                else
                    return true;
            }
        }
        /// 
        /// 数字转换成大写汉字主函数
        /// 
        /// 返回转换后的大写汉字
        public string Convert()
        { 
            string bb = "";
            if (IsNumber)
            {
                string str = j.ToString();
                string[] Num = str.Split(.);
                if (Num.Length == 1)
                {
                    bb = NumberString(Num[0]) + "元整";
                    bb = bb.Replace("零零", "");
                }
                else
                {
                    bb = NumberString(Num[0]) + "";
                    bb += FloatString(Num[1]);
                    bb = bb.Replace("零零", "");
                }
            }
            else
            {
                throw new FormatException("你输入的数字格式不正确或不是数字!");
            }
            return bb;
        }
        /// 
        /// 小数位转换只支持两位的小数
        /// 
        /// 转换的小数
        /// 小数转换成汉字
        private string FloatString(string Num)
        {
            string cc = "";
            if (Num.Length > 2)
            {
                throw new FormatException("小数位数过多.");
            }
            else
            {
                string bb = ConvertString(Num);
                int len = bb.IndexOf("");
                if (len != 0)
                {
                    bb = bb.Replace("", "");
                    if (bb.Length == 1)
                    {
                        cc = bb.Substring(0, 1) + "角整";
                    }
                    else
                    {
                        cc = bb.Substring(0, 1) + "";
                        cc += bb.Substring(1, 1) + "";
                    }
                }
                else
                    cc = bb + "";
            }
            return cc;
        }
        /// 
        /// 判断数字位数以进行拆分转换
        /// 
        /// 要进行拆分的数字
        /// 转换成的汉字
        private string NumberString(string Num)
        {
            string bb = "";
            if (Num.Length 4)
            {
                bb = Convert4(Num);
            }
            else if (Num.Length > 4 && Num.Length 8)
            {
                bb = Convert4(Num.Substring(0, Num.Length - 4)) + "";
                bb += Convert4(Num.Substring(Num.Length - 4, 4));
            }
            else if (Num.Length > 8 && Num.Length 12)
            {
                bb = Convert4(Num.Substring(0, Num.Length - 8)) + "亿";
                if (Convert4(Num.Substring(Num.Length - 8, 4)) == "")
                    if (Convert4(Num.Substring(Num.Length - 4, 4)) != "")
                        bb += "";
                    else
                        bb += "";
                else
                    bb += Convert4(Num.Substring(Num.Length - 8, 4)) + "";
                bb += Convert4(Num.Substring(Num.Length - 4, 4));
            }
            return bb;
        }
        /// 
        /// 四位数字的转换
        /// 
        /// 准备转换的四位数字
        /// 转换以后的汉字
        private string Convert4(string Num)
        {
            string bb = "";
            if (Num.Length == 1)
            {
                bb = ConvertString(Num);
            }
            else if (Num.Length == 2)
            {
                bb = ConvertString(Num);
                bb = Convert2(bb);
            }
            else if (Num.Length == 3)
            {
                bb = ConvertString(Num);
                bb = Convert3(bb);
            }
            else
            {
                bb = ConvertString(Num);
                string cc = "";
                string len = bb.Substring(0, 4);
                if (len != "零零零零")
                {
                    len = bb.Substring(0, 3);
                    if (len != "零零零")
                    {
                        bb = bb.Replace("零零零", "");
                        if (bb.Length == 1)
                        {
                            bb = bb.Substring(0, 1) + "";
                        }
                        else
                        {
                            if (bb.Substring(0, 1) != "" && bb.Substring(0, 2) != "")
                                cc = bb.Substring(0, 1) + "";
                            else
                                cc = bb.Substring(0, 1);
                            bb = cc + Convert3(bb.Substring(1, 3));
                        }
                    }
                    else
                    {
                        bb = bb.Replace("零零零", "");
                    }
                }
                else
                {
                    bb = bb.Replace("零零零零", "");
                }
            }
            return bb;
        }
        /// 
        /// 将数字转换成汉字
        /// 
        /// 需要转换的数字
        /// 转换后的汉字
        private string ConvertString(string Num)
        {
            string bb = "";
            for (int i = 0; i )
            {
                bb += NumChineseCharacter[int.Parse(Num.Substring(i, 1))];
            }
            return bb;
        }
        /// 
        /// 两位数字的转换
        /// 
        /// 两位数字
        /// 转换后的汉字
        private string Convert2(string Num)
        {
            string bb = ""; string cc = "";
            string len = Num.Substring(0, 1);
            if (len != "")
            {
                bb = Num.Replace("", "");
                if (bb.Length == 1)
                {
                    cc = bb.Substring(0, 1) + "";
                }
                else
                {
                    cc = bb.Substring(0, 1) + "";
                    cc += bb.Substring(1, 1);
                }
            }
            else
                cc = Num;
            return cc;
        }
        /// 
        /// 三位数字的转换
        /// 
        /// 三位数字
        /// 转换后的汉字
        private string Convert3(string Num)
        {
            string bb = ""; string cc = "";
            string len = Num.Substring(0, 2);
            if (len != "零零")
            {
                bb = Num.Replace("零零", "");
                if (bb.Length == 1)
                {
                    bb = bb.Substring(0, 1) + "";
                }
                else
                {
                    if (bb.Substring(0, 1) != "")
                        cc = bb.Substring(0, 1) + "";
                    else
                        cc = bb.Substring(0, 1);
                    bb = cc + Convert2(bb.Substring(1, 2));
                }
            }
            else
            {
                bb = Num.Replace("零零", "");
            }
            return bb;
        }
    }

 

C# 数字 转换 大写汉字

标签:ram   throw   length   bool   i++   ret   div   char   this   

原文地址:https://www.cnblogs.com/lanyubaicl/p/12468045.html


评论


亲,登录后才可以留言!