白开心

  IT博客 :: 首页 ::  :: 联系 :: 聚合  :: 管理 ::
  9 随笔 :: 76 文章 :: 28 评论 :: 0 Trackbacks

 

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4
 5namespace CW
 6{
 7    public class Cash
 8    {
 9        private static string MoneyNum = "零一二三四五六七八九";
10        private static string MoneyUnit = "十百千万十百千亿";
11        private static string CashUnit = "元角分整";
12        private static string[] BeforeReplace = new string[] {  MoneyNum[0].ToString() + MoneyNum[0].ToString(),
13                                                                MoneyUnit[7].ToString() + MoneyNum[0].ToString() + MoneyUnit[3].ToString() ,
14                                                                MoneyNum[0].ToString() + MoneyUnit[3].ToString(),
15                                                                MoneyNum[0].ToString() + MoneyUnit[7].ToString(),
16                                                                MoneyUnit[7].ToString() + MoneyUnit[3].ToString(),
17                                                                MoneyNum[0].ToString() + MoneyNum[0].ToString(),
18                                                                MoneyNum[0].ToString() + CashUnit[1].ToString() + MoneyNum[0].ToString() + CashUnit[2].ToString(),
19                                                                MoneyNum[0].ToString() + CashUnit[2].ToString()
20                                                             }
;
21        private static string[] AfterReplace = new string[] {  MoneyNum[0].ToString(),
22                                                                MoneyUnit[7].ToString() + MoneyNum[0].ToString(),
23                                                                MoneyUnit[3].ToString() + MoneyNum[0].ToString(),
24                                                                MoneyUnit[7].ToString() + MoneyNum[0].ToString(),
25                                                                MoneyUnit[7].ToString() + MoneyNum[0].ToString(),
26                                                                MoneyNum[0].ToString(),
27                                                                String.Empty,
28                                                                String.Empty
29                                                             }
;
30
31        /**//// <summary>
32        /// 字符串替换方法,另外一种是以 4 个数字为基础的递归方法
33        /// Create By HJ 2007-10-21
34        /// </summary>
35        /// <param name="moneyNum"></param>
36        /// <returns></returns>

37        public static string getCash(decimal moneyNum)
38        {
39            string intNum, point;
40            /**////取整数部分
41            intNum = Convert.ToString(Math.Floor(moneyNum));       
42            /**////取小数部分
43            point = Convert.ToString(Math.Floor(moneyNum * 100));  
44            point = point.Substring(point.Length - 2);
45
46            /**////计算整数部分
47            for (int i = 0; i < MoneyNum.Length; i++)
48            {
49                intNum = intNum.Replace(i.ToString(), MoneyNum[i].ToString());
50            }

51            int intNumLength = intNum.Length;
52            for (int i = intNumLength - 1; i > 0; i--)
53            {   /**////根据位数把单位加上
54                ///如果是零则不加单位,但是 万  和  亿 需要加上

55                if (intNum[i - 1== MoneyNum[0&& (intNumLength - i + 7% 8 != 3 && (intNumLength - i + 7% 8 != 7continue;
56                intNum = intNum.Insert(i, MoneyUnit[(intNumLength - i + 7% 8].ToString());
57            }

58
59            /**////加上 角 和 分 的单位
60            intNum += CashUnit[0].ToString() + MoneyNum.Substring(Convert.ToInt16(point[0].ToString()), 1+ CashUnit[1].ToString() + MoneyNum.Substring(Convert.ToInt16(point[1].ToString()), 1+ CashUnit[2].ToString();
61
62            /**////替换  零零 -> 零  亿零万  ->  亿零,零万  ->  万零,零亿 -> 亿零,亿万 -> 亿零,零角零分 - > "",零分 - > "",零零 -> 零,再调用一次,确保 亿零万 替换后的情况
63            for (int i = 0; i < BeforeReplace.Length; i++)
64            
65                while(intNum.IndexOf(BeforeReplace[i])>-1)
66                {
67                    intNum = intNum.Replace(BeforeReplace[i], AfterReplace[i]);
68                }

69            }

70
71            /**////最后的 零 去掉
72            if (intNum.EndsWith(MoneyNum[0].ToString())) intNum = intNum.Substring(0, intNum.Length - 1);
73
74            return intNum + CashUnit[3].ToString();
75        }

76
77    }

78}

posted on 2005-12-12 11:59 白开心 阅读(4751) 评论(0)  编辑 收藏 引用 所属分类: .Net(学习ing...)
只有注册用户登录后才能发表评论。