C# 数值转换中文大写

发票支票都要用到数值转换为中文大写,这里提供一个代码解决方案,此方法到兆为止,之前那个如果到亿以后当中很多个零的话就错了(亿以前是对的),现在改了,不过在纠结到底是“陆亿伍仟零叁拾贰圆整”还是“陆亿伍仟零叁拾贰圆整”现在是前一钟

 

 

        public readonly string[] RMBUNIT = { "圆", "角", "分", "整" };
        public readonly string[] CHINANUM = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
        //个位因为没有单位所以是空字符
        public readonly string[] CHINABIT = { string.Empty, "拾", "佰", "仟", "万", "亿", "兆" };
        // 主方法将钱的数额传入返回大写中文 
        public string ToMoneyCNString(decimal money)
        {
            string[] unit = RMBUNIT;
            string[] moneySplit = money.ToString().Split('.');
            string intValue = string.Empty, minValue = string.Empty;

            //转换小数点前“元”部分 
            if (moneySplit.Length > 0)
                intValue = ToNumCNString(moneySplit[0].ToCharArray());
            if (intValue.Length > 0)
                intValue += unit[0];
            //转换小数点后“角”和“分”部分
            if (moneySplit.Length > 1)
                for (int i = 0; i < moneySplit[1].Length; i++)
                    minValue += CHINANUM[Convert.ToInt16(moneySplit[1][i].ToString())] + unit[i + 1];
            return intValue + minValue + unit[3];
        }

        /// 将数字转换为中文大写数字 
        public string ToNumCNString(long num)
        {
            return ToNumCNString(num.ToString().ToCharArray());
        }

        public string ToNumCNString(char[] num)
        {
            int index = 0;
            string strBig = string.Empty;

            do
            {
                strBig += ToNumCNString(num, ref index);
            } while (num.Length > 1 && index < num.Length - 1);

            return strBig;
        }

        public string ToNumCNString(char[] num, ref int index)
        {
            char[] numChr = num;
            string strBig = string.Empty;
            string toChrZero = CHINANUM[0];
            string bit = CHINABIT[firstCNStringIndex(minLen(numChr.Length - index))];
            List<int> maxBit = readMaxBit(num);
            bool blBit = false;
            for (int i = index; i < numChr.Length; i++)
            {
                //如果当前数字是0,就要判断0是不是在最靠近他的单位位置
                string cnBit = firstCNString(numChr.Length - i);
                int bitIndex = firstCNStringIndex(minLen(numChr.Length - index));
                index = i;
                if (bit != CHINABIT[bitIndex])
                    return strBig;

                //判断如果是0同时又\是本位最大位或者等于万位要加单位(因为都是万位向上进)
                if ('0' == numChr[i] && 
                    !string.IsNullOrEmpty(strBig) &&
                    (cnBit == bit || isBit(maxBit, cnBit)))
                {
                    blBit = true;
                    strBig += cnBit;
                }
                else if ('0' != numChr[i])
                {
                    strBig += CHINANUM[Convert.ToInt16(numChr[i].ToString())] + cnBit;
                }

                if ('0' == numChr[i] && !strBig.EndsWith(toChrZero))
                    if (cnBit == bit && !string.IsNullOrEmpty(strBig) ||
                        string.IsNullOrEmpty(cnBit) ||
                        i + 1 < numChr.Length && '0' != numChr[i + 1])
                        if (!blBit)
                            strBig += toChrZero;

                blBit = false;
            }
            strBig = removeLastZero(strBig);
            return strBig;
        }

        private bool isBit(List<int> list, string bit)
        {
            foreach (int node in list)
                if (bit == CHINABIT[node])
                    return true;

            return false;
        }

        private List<int> readMaxBit(char[] num)
        {
            List<int> list = new List<int>();
            for (int i = 0; i < num.Length; i++)
            {
                int max = firstCNStringIndex(minLen(num.Length - i));
                max = max > 4 ? max - 1 : 4;
                list.Add(max);
            }

            return list;
        }

        private string removeLastZero(string strBig)
        {
            if (strBig.EndsWith(CHINANUM[0]))
                strBig = strBig.Remove(strBig.Length - 1);

            return strBig;
        }

        private int firstCNStringIndex(int index)
        {
            switch (index)
            {
                case 1:
                    return 0;
                case 2:
                    return 1;
                case 3:
                    return 2;
                case 4:
                    return 3;
                case 5:
                    return 4;
                case 9:
                    return 5;
                case 17:
                    return 6;
            }

            return -1;
        }

        private int firstCNStringLen(int len)
        {
            int index = firstCNStringIndex(len);
            if (index < 0)
                index = firstCNStringLen(len - minLen(len) + 1);

            return index;
        }

        /// 查找单位是什么(佰,仟 等)传入的参数就是需要转换数字的位数长度(如100就是3) 
        private string firstCNString(int len)
        {
            if (0 == len)
                return string.Empty;
            int index = firstCNStringLen(len);

            return CHINABIT[index];
        }

        /// 查找最靠近的单位的长度        
        private int minLen(int len)
        {
            int fontValue = 5;
            int returnValue = 5;
            while (true)
            {
                returnValue += (returnValue - 1);
                if (returnValue > len)
                    return fontValue;
                fontValue = returnValue;
            }
        }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值