金额转换成大写(从Sql Server版转换成的C#版)

namespace AA
{
    public class BB
    {
        public static string L2U(decimal lowerMoney)
        {
            string lowerStr = string.Empty;
            string upperPart = string.Empty;
            string upperStr = string.Empty;

            lowerStr = lowerMoney.ToString("F2");

            for (int index = 0; index < lowerStr.Length; index++)
            {
                upperPart = GetUpperNumber(lowerStr.Substring(lowerStr.Length - index-1, 1))
                + GetUnit(index+1);
                upperStr = upperPart + upperStr;
            }

            upperStr = upperStr.Replace("零拾", "零");
            upperStr = upperStr.Replace("零佰", "零");
            upperStr = upperStr.Replace("零仟", "零");
            upperStr = upperStr.Replace("零零零", "零");
            upperStr = upperStr.Replace("零零", "零");
            upperStr = upperStr.Replace("零角零分", "整");
            upperStr = upperStr.Replace("零分", "整");
            upperStr = upperStr.Replace("零角", "零");
            upperStr = upperStr.Replace("零亿零万零元", "亿元");
            upperStr = upperStr.Replace("亿零万零元", "亿元");
            upperStr = upperStr.Replace("零亿零万", "亿");
            upperStr = upperStr.Replace("零万零元", "万元");
            upperStr = upperStr.Replace("万零元", "万元");
            upperStr = upperStr.Replace("零亿", "亿");
            upperStr = upperStr.Replace("零万", "万");
            upperStr = upperStr.Replace("零元", "元");
            upperStr = upperStr.Replace("零零", "零");

            if ("元" == upperStr.Substring(0, 1))
            {
                upperStr = upperStr.Substring(1, upperStr.Length - 1);
            }
            if ("零" == upperStr.Substring(0, 1))
            {
                upperStr = upperStr.Substring(1, upperStr.Length - 1);
            }
            if ("角" == upperStr.Substring(0, 1))
            {
                upperStr = upperStr.Substring(1, upperStr.Length - 1);
            }
            if ("分" == upperStr.Substring(0, 1))
            {
                upperStr = upperStr.Substring(1, upperStr.Length - 1);
            }
            if ("整" == upperStr.Substring(0, 1))
            {
                upperStr = "零元整";
            }

            if ("壹拾" == upperStr.Substring(0, 2))
            {
                upperStr = upperStr.Substring(1, upperStr.Length - 1);
            }

            return upperStr;
        }

        private static string GetUpperNumber(string str)
        {
            string cstr = string.Empty;
            switch (str)
            {
                case ".": cstr = "元"; break;
                case "0": cstr = "零"; break;
                case "1": cstr = "壹"; break;
                case "2": cstr = "贰"; break;
                case "3": cstr = "叁"; break;
                case "4": cstr = "肆"; break;
                case "5": cstr = "伍"; break;
                case "6": cstr = "陆"; break;
                case "7": cstr = "柒"; break;
                case "8": cstr = "捌"; break;
                case "9": cstr = "玖"; break;
            }

            return cstr;
        }

        private static string GetUnit(int index)
        {
            string cstr;
            switch (index)
            {
                case 1: cstr = "分"; break;
                case 2: cstr = "角"; break;
                case 3: cstr = ""; break;
                case 4: cstr = ""; break;
                case 5: cstr = "拾"; break;
                case 6: cstr = "佰"; break;
                case 7: cstr = "仟"; break;
                case 8: cstr = "万"; break;
                case 9: cstr = "拾"; break;
                case 10: cstr = "佰"; break;
                case 11: cstr = "仟"; break;
                case 12: cstr = "亿"; break;
                case 13: cstr = "拾"; break;
                case 14: cstr = "佰"; break;
                case 15: cstr = "仟"; break;
                case 16: cstr = "万"; break;
                default: cstr = "";   break;
            }

            return cstr;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值