金额大小写转化类

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    /// <summary>
    /// 类    名:  MoneyHelper
    /// 版 本 号:  V1.0
    /// 作    者:  张远强
    /// HomePage:  http://www.mzwu.com/
    /// 创建日期:  2009-05-12
    /// 功能描述:  金额大小写转化类
    /// </summary>
    public sealed class MoneyHelper
    {
        /// <summary>
        /// 转为小写金额
        /// </summary>
        /// <param name="money">大写金额</param>
        /// <returns></returns>
        public static double ToLower(string money)
        {
            return ToLowerForInt(money) + ToLowerForDec(money);
        }

        /// <summary>
        /// 转为小写金额(整数)
        /// </summary>
        /// <param name="money">大写金额</param>
        /// <returns></returns>
        private static double ToLowerForInt(string money)
        {
            string[] units = new string[] { "亿", "万", "千", "百", "十", "元" };//金额单位数组
            int[] indexs = new int[] { 8, 4, 3, 2, 1, 0 };//金额单位对应位数,如亿位于第8位,百位于第2位,元位于第0位
            string[] items = new string[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };//十个基本数字(大写)
            double[] results = new double[indexs[0] + indexs[1] + 2];//保存结果,其长度为最大的两个单位位数之和+1


            //特殊处理:以十开头的大写金额前一律加上"一".我们平时说十万表示一十万,十表示一十,但不会有人说百万,千亿
            if (money.Substring(0, 1) == "十")
                money = "一" + money;

            //只处理整数部分
            if (money.IndexOf("元") != -1)
            {
                money = money.Split('元')[0] + "元";
            }
            else
            {
                money = money.TrimEnd('元') + "元";
            }

            //临时数组
            string[] temps = new string[2] { "", money };

            //按金额单位从大到小循环处理(精确到元)
            for (int i = 0; i < units.Length; i++)
            {
                if (string.IsNullOrEmpty(temps[1]))
                {
                    break;
                }
                else
                {
                    if (temps[1].IndexOf(units[i]) != -1)
                    {
                        //在单位处切割
                        temps = temps[1].Split(new string[] { units[i] }, StringSplitOptions.None);

                        //对左部分再按金额单位从大到小循环处理(精确到元)
                        temps[0] += "元";
                        for (int j = i; j < units.Length; j++)
                        {
                            Match match = Regex.Match(temps[0], string.Format("([{0}]{{1}}){1}", string.Concat(items), units[j]), RegexOptions.IgnoreCase);
                            if (match.Success)
                            {
                                results[results.Length-(indexs[i]+indexs[j]+1)] = Convert.ToDouble(string.Concat(items).IndexOf(match.Groups[1].ToString()));
                            }
                        }
                    }
                }
            }

            //将results数组元素按权相加为数字
            double r = 0;
            for (int i = results.Length - 1; i >= 0; i--)
            {
                r += results[i] * Math.Pow(10, results.Length - i - 1);
            }

            return r;
        }

        /// <summary>
        /// 转为小写金额(小数)
        /// </summary>
        /// <param name="money">大写金额</param>
        /// <returns></returns>
        private static double ToLowerForDec(string money)
        {
            string[] units = new string[] { "角", "分", "厘", "毫" };//金额单位数组
            int[] indexs = new int[] { 3, 2, 1, 0 };//金额单位对应位数
            string[] items = new string[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };//十个基本数字(大写)
            double[] results = new double[4];//保存结果


            //只处理小数部分
            if (money.IndexOf("元") != -1)
                money = money.Split('元')[1];

            //循环处理各位
            for (int i = 0; i < units.Length; i++)
            {
                Match match = Regex.Match(money, string.Format("([{0}]{{1}}){1}", string.Concat(items), units[i]), RegexOptions.IgnoreCase);
                if (match.Success)
                {
                    results[results.Length - (indexs[i] + 1)] = Convert.ToDouble(string.Concat(items).IndexOf(match.Groups[1].ToString()));
                }
            }

            //将results数组元素按权相加为数字
            double r = 0;
            for (int i = 0; i < results.Length;i++ )
            {
                r += results[i] * Math.Pow(10, -(i + 1));
            }

            return r;
        }
    }
}


评论: 0 | 引用: 0 | 查看次数: 3810
发表评论
登录后再发表评论!