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;
        }

posted @ 2014-12-18 17:52  范永强  阅读(2119)  评论(0编辑  收藏  举报