博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

人民币大小写转换(C#)

Posted on 2006-12-04 00:41  sashow  阅读(3180)  评论(2编辑  收藏  举报

    今天写了一个C#版本的大小写转换类,想着也要更新一下我的日记了,便把它传了上来。 哪位路过的高手,请指点。
    这个类提供了一个静态方法叫做 Convert ( double ),传入一个9千万亿以内的数值它都能正确的转换为中文的大写。基本的思路是将传入的数据分为整数部分和小数部分分别来处理,小数部分的处理比较简单,整数部分的处理我作了相应的注释。

  1using System;
  2
  3namespace Tools{
  4    /// <summary>
  5    /// DigitalToChinese :实现将人民币的数字形式转换为中文格式。
  6    /// </summary>

  7    public class DigitalToChinese
  8    {
  9        public DigitalToChinese(){
 10        }

 11
 12        public static string Convert ( double digital ){
 13            if ( digital == 0.00f )
 14                return "零元整";
 15
 16            string buf = "";                        /* 存放返回结果 */
 17            string strDecPart = "";                    /* 存放小数部分的处理结果 */
 18            string strIntPart = "";                    /* 存放整数部分的处理结果 */
 19
 20            /* 将数据分为整数和小数部分 */
 21            char [] cDelim = {'.'};
 22            string [] tmp = null;
 23            string strDigital = digital.ToString ();
 24            if ( strDigital[0=='.'){                /* 处理数据首位为小数点的情况 */
 25                strDigital = "0" + strDigital;
 26            }

 27            tmp = strDigital.Split ( cDelim,2);
 28
 29            /* 整数部分的处理 */
 30            if ( tmp[0].Length > 15 ) {
 31                throw new Exception ("数值超出处理范围。");
 32            }

 33            bool flag = true;        /* 标示整数部分是否为零 */
 34            if ( digital >= 1.00f ) {
 35                strIntPart = ConvertInt( tmp[0] );
 36                flag = false;
 37            }

 38
 39            /* 存在小数部分,则处理小数部分 */
 40            if ( tmp.Length == 2 )    
 41                strDecPart = ConvertDecimal ( tmp[1],flag);
 42            else
 43                strDecPart = "";
 44
 45            buf = strIntPart + strDecPart;
 46            return buf;
 47        }

 48
 49
 50
 51        private static string ConvertInt ( string intPart ) {
 52            string buf = "";
 53            int length = intPart.Length ;
 54            int curUnit = length;
 55
 56            /* 处理除个位以上的数据 */
 57            string tmpValue = "";                    /* 记录当前数值的中文形式 */
 58            string tmpUnit = "";                    /* 记录当前数值对应的中文单位 */
 59            int i ;
 60            for ( i = 0 ; i < length - 1 ; i ++ ,curUnit --){
 61                if ( intPart [ i ] != '0' ){
 62                    tmpValue = DigToCC ( intPart [ i ] );
 63                    tmpUnit = GetUnit ( curUnit -1 );
 64                }

 65                else {
 66                    /* 如果当前的单位是"万、亿",则需要把它记录下来 */
 67                    if ( (curUnit -1 ) % 4 == 0 ) {
 68                        tmpValue = "";
 69                        tmpUnit = GetUnit ( curUnit -1 );
 70                    }

 71                    else {
 72                        tmpUnit = "";
 73                        /* 如果当前位是零,则需要判断它的下一位是否为零,再确定是否记录'零' */
 74                        if (   intPart [i+1!= '0' ){
 75                            tmpValue = "";
 76                        }

 77                        else {
 78                            tmpValue = "";
 79                        }

 80                    }

 81                }

 82                buf += tmpValue + tmpUnit;
 83            }

 84
 85            /* 处理个位数据 */
 86            if ( intPart [i] != '0' )
 87                buf += DigToCC ( intPart [i] );
 88            buf += "";
 89
 90            return buf;
 91        }

 92
 93
 94        /// <summary>
 95        /// 小数部分的处理
 96        /// </summary>
 97        /// <param name="decPart">需要处理的小数部分</param>
 98        /// <param name="flag">标示整数部分是否为零</param>
 99        /// <returns></returns>

100        private static string ConvertDecimal ( string decPart,bool flag ) {
101            string buf = "";
102            if ( ( decPart [0== '0'&& (decPart.Length  > 1 )){
103                if ( flag == false ) {
104                    buf += "";
105                }

106                if ( decPart[1!= '0'){
107                    buf += DigToCC( decPart[1] )+"";
108                }

109            }

110            else{
111                buf += DigToCC ( decPart[0] ) +"";
112                if ( ( decPart.Length >1&& (decPart [1!= '0' ) ){
113                    buf += DigToCC( decPart[1] )+"";
114                }

115            }

116            buf += "";
117            return buf;
118        }

119
120
121        /// <summary>
122        /// 获取人民币中文形式的对应位置的单位标志
123        /// </summary>
124        /// <param name="n"></param>
125        /// <returns></returns>

126        private static string GetUnit( int n ) {
127            switch( n ) {
128                case 1:
129                    return "";
130                case 2:
131                    return "";
132                case 3:
133                    return "";
134                case 4:
135                    return "";
136                case 5:
137                    return "";
138                case 6:
139                    return "";
140                case 7:
141                    return "";
142                case 8:
143                    return "亿";
144                case 9:
145                    return "";
146                case 10:
147                    return "";
148                case 11:
149                    return "";
150                case 12:
151                    return "";
152                case 13:
153                    return "";
154                case 14:
155                    return "";
156                case 15:
157                    return "";
158                default:
159                    return " ";
160            }

161        }

162
163
164
165        /// <summary>
166        /// 数字转换为相应的中文字符 ( Digital To Chinese Char )
167        /// </summary>
168        /// <param name="c">以字符形式存储的数字</param>
169        /// <returns></returns>

170        private static string DigToCC(char c) {
171            switch( c ) {
172                case '1':
173                    return "";
174                case '2':
175                    return "";
176                case '3':
177                    return "";
178                case '4':    
179                    return "";
180                case '5':
181                    return "";
182                case '6':
183                    return "";
184                case '7':
185                    return "";
186                case '8':
187                    return "";
188                case '9':
189                    return "";
190                case '0':
191                    return "";
192                default:
193                    return "  ";
194            }

195        }

196    }

197}

198


下面是一个测试代码:
 1using System;
 2
 3namespace Tools{
 4    /// <summary>
 5    /// Class1 的摘要说明。
 6    /// </summary>

 7    class Class1
 8    {
 9        /// <summary>
10        /// 应用程序的主入口点。
11        /// </summary>

12        [STAThread]
13        static void Main(string[] args)
14        {
15            string read = null;
16            Console.WriteLine ("输入数值,'q'推出!");
17
18            do{
19                
20                read = Console.ReadLine ();
21                if ( read =="q" || read == "Q")
22                    return;
23                try{
24                    Console.WriteLine ( DigitalToChinese.Convert ( Convert.ToDouble ( read )));
25                }

26                catch ( Exception  ){
27                    Console.WriteLine ("输入的数据不符合要求,请输入正确的数值.");
28                }

29            }
while ( 1 == 1 );
30        }

31    }

32}

33