C#中将数字金额转成英文大写金额的函数

<span style="white-space:pre">	</span>/// <summary>
        /// 数字转金额大写
        /// 调用示例:String desc = CommonUtils.convertEnDes(new Decimal("100.55"), "say", "RMB", "only"));
        /// 返回 desc为"say RMB one hundred  dolt five five only"
        /// </summary>
        /// <returns></returns>
        public static String convertEnDes(Object obj, String prefix, String currencyISO, String suffix)
        {
            if (obj == null)
            {
                return "";
            }
            Decimal val = Convert.ToDecimal(obj);
            StringBuilder sb = new StringBuilder();
            sb.Append(prefix).Append(" ");
            String[] amtEnDesc = getAmtEnDesc(val);
            sb.Append(currencyISO).Append(" ");
            sb.Append(amtEnDesc[0]).Append(" ");

            if (amtEnDesc[1].Trim().Length != 0 && !"zero".Equals(amtEnDesc[1].Trim(), StringComparison.OrdinalIgnoreCase))
            {
                sb.Append("point ").Append(amtEnDesc[1]);
            }
            sb.Append(" ").Append(suffix);
            return sb.ToString().ToUpper();
        }

        private static String convertLessThanOneThousand(long number)
        {
            String soFar;
            if (number % 100 < 20)
            {
                soFar = FTConst.numNames[(int)(number % 100)];
                number /= 100;
            }
            else
            {
                soFar = FTConst.numNames[(int)(number % 10)];
                number /= 10;

                soFar = FTConst.tensNames[(int)(number % 10)] + soFar;
                number /= 10;
            }
            if (number == 0)
                return soFar;
            return FTConst.numNames[(int)number] + "hundred " + soFar;
        }

        //转换整数部分
        public static String convert(long number)
        {
            string[] majorNames = { " ", "thousand ",
			"million ", "billion ", "trillion ", "quadrillion ",
			"quintillion " };
            if (number == 0)
            {
                return "zero ";
            }
            String prefix = " ";
            if (number < 0)
            {
                number = -number;
                prefix = "negative ";
            }
            String soFar = " ";
            int place = 0;
            do
            {
                long n = (number % 1000);
                if (n != 0)
                {
                    String s = convertLessThanOneThousand(n);
                    soFar = s + majorNames[place] + soFar;
                }
                place++;
                number /= 1000;
            } while (number > 0);
            return (prefix + soFar).Trim();
        }

        //转换小数部分
        public static String convertDot(string number)
        {
            if (number.Trim().Length == 0)
            {
                return " ";
            }
            String soFar = " ";
            long c = Convert.ToInt64(number);
            do
            {
                long n = (c % 10);
                if (n != 0)
                {
                    String s = convertLessThanOneThousand(n);
                    soFar = s + soFar;
                }

                c /= 10;
            } while (c > 0);
            return soFar.Trim();
        }


        private static String[] getAmtEnDesc(Decimal val)
        {
            String[] ret = new String[2];
            String valDes = val.ToString();
            String[] valSplit = valDes.Split('.');
            long part = Convert.ToInt64(valSplit[0]);
            ret[0] = convert(part);
            try
            {
                ret[1] = convertDot(valSplit[1]);
            }
            catch
            {
                ret[1] = "";
            }
            return ret;
        }

转载于:https://my.oschina.net/fanyongqiang/blog/654338

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值