金额转换为人民币大写(C#)

 

  1using System;
  2
  3namespace TestUpperRMB
  4{
  5    /// <summary>
  6    /// 本类实现阿拉伯数字到大写中文的转换
  7    /// 该类没有对非法数字进行判别,请事先自己判断数字是否合法
  8    /// </summary>

  9    public class ChineseNum
 10    {
 11        public static string GetChineseNum(string p_num)
 12        {
 13            ChineseNum cn = new ChineseNum();
 14        
 15            return cn.NumToChn(p_num);
 16        }

 17
 18        public static string GetUpperMoney(double p_Money)
 19        {
 20            ChineseNum cn = new ChineseNum();
 21
 22            return cn.GetMoneyChinese(p_Money);
 23        }

 24
 25        //转换数字
 26        private char CharToNum(char x)
 27        {
 28            string stringChnNames="零一二三四五六七八九";
 29            string stringNumNames="0123456789";
 30            return stringChnNames[stringNumNames.IndexOf(x)];
 31        }

 32
 33        //转换万以下整数
 34        private string WanStrToInt(string x)
 35        {
 36            string[] stringArrayLevelNames=new string[4{"","","",""};
 37            string ret="";
 38            int i;
 39            for (i=x.Length-1;i>=0;i--)
 40                if (x[i] == '0')
 41                {
 42                    ret = CharToNum(x[i]) + ret;
 43                }

 44                else
 45                {
 46                    ret = CharToNum(x[i]) + stringArrayLevelNames[x.Length - 1 - i] + ret;
 47                }

 48            while ((i = ret.IndexOf("零零")) != -1)
 49            {
 50                ret = ret.Remove(i, 1);
 51            }

 52            if (ret[ret.Length - 1== '' && ret.Length > 1)
 53            {
 54                ret = ret.Remove(ret.Length - 11);
 55            }

 56            if (ret.Length >= 2 && ret.Substring(02== "一十")
 57            {
 58                ret = ret.Remove(01);
 59            }

 60            return ret;
 61        }

 62        //转换整数
 63        private string StrToInt(string x)
 64        {
 65            int len=x.Length;
 66            string ret,temp;
 67            if (len <= 4)
 68            {
 69                ret = WanStrToInt(x);
 70            }

 71            else if (len <= 8)
 72            {
 73                ret = WanStrToInt(x.Substring(0, len - 4)) + "";
 74                temp = WanStrToInt(x.Substring(len - 44));
 75                if (temp.IndexOf(""== -1 && temp != "")
 76                    ret += "" + temp;
 77                else
 78                    ret += temp;
 79            }

 80            else
 81            {
 82                ret = WanStrToInt(x.Substring(0, len - 8)) + "亿";
 83                temp = WanStrToInt(x.Substring(len - 84));
 84                if (temp.IndexOf(""== -1 && temp != "")
 85                {
 86                    ret += "" + temp;
 87                }

 88                else
 89                {
 90                    ret += temp;
 91                }

 92                ret += "";
 93                temp = WanStrToInt(x.Substring(len - 44));
 94                if (temp.IndexOf(""== -1 && temp != "")
 95                {
 96                    ret += "" + temp;
 97                }

 98                else
 99                {
100                    ret += temp;
101                }

102
103            }

104            int i;
105            if ((i = ret.IndexOf("零万")) != -1)
106            {
107                ret = ret.Remove(i + 11);
108            }

109            while ((i = ret.IndexOf("零零")) != -1)
110            {
111                ret = ret.Remove(i, 1);
112            }

113            if (ret[ret.Length - 1== '' && ret.Length > 1)
114            {
115                ret = ret.Remove(ret.Length - 11);
116            }

117            return ret;
118        }

119        //转换小数
120        private string StrToDouble(string x)
121        {
122            string ret="";
123            for (int i = 0; i < x.Length; i++)
124            {
125                ret += CharToNum(x[i]);
126            }

127            return ret;
128        }

129
130        private string NumToChn(string x)
131        {
132            if (x.Length == 0)
133            {
134                return "";
135            }

136            string ret="";
137            if (x[0]=='-')
138            {
139                ret="";
140                x=x.Remove(0,1);
141            }

142            if (x[0].ToString() == ".")
143            {
144                x = "0" + x;
145            }

146            if (x[x.Length - 1].ToString() == ".")
147            {
148                x = x.Remove(x.Length - 11);
149            }

150            if (x.IndexOf("."> -1)
151            {
152                ret += StrToInt(x.Substring(0, x.IndexOf("."))) + "" + StrToDouble(x.Substring(x.IndexOf("."+ 1));
153            }

154            else
155            {
156                ret += StrToInt(x);
157            }

158            return ret;
159        }

160
161
162        private string GetMoneyChinese(Double Money) 
163        {
164            int i;
165            string mstrSource;
166                            
167            if (Money == 0)
168            {
169                return "";
170            }

171            mstrSource = Money.ToString("#0.00");
172            i = mstrSource.IndexOf(".");
173            if (i > 0{mstrSource = mstrSource.Replace(".","");}
174            if (mstrSource.Substring(0,1== "0"{mstrSource = mstrSource.Remove(0,1);}
175             
176            mstrSource = NumstrToChinese(mstrSource);
177            if (mstrSource.Length == 0{return "";}
178            //
179            if (Money < 0
180            {
181                mstrSource = "" + mstrSource;
182            }
       
183             
184            mstrSource=mstrSource.Replace("0","");
185            mstrSource=mstrSource.Replace("1","");
186            mstrSource=mstrSource.Replace("2","");
187            mstrSource=mstrSource.Replace("3","");
188            mstrSource=mstrSource.Replace("4","");
189            mstrSource=mstrSource.Replace("5","");
190            mstrSource=mstrSource.Replace("6","");
191            mstrSource=mstrSource.Replace("7","");
192            mstrSource=mstrSource.Replace("8","");
193            mstrSource=mstrSource.Replace("9","");
194            mstrSource=mstrSource.Replace("M","亿");
195            mstrSource=mstrSource.Replace("W","");
196            mstrSource=mstrSource.Replace("S","");
197            mstrSource=mstrSource.Replace("H","");
198            mstrSource=mstrSource.Replace("T","");
199            mstrSource=mstrSource.Replace("Y","");
200            mstrSource=mstrSource.Replace("J","");
201            mstrSource=mstrSource.Replace("F","");
202            if (mstrSource.Substring(mstrSource.Length-11!= ""
203            {
204                mstrSource = mstrSource + "";
205            }

206            return mstrSource;
207        }

208
209        //金额转换
210        private string NumstrToChinese(string numstr) 
211        {
212            int i;
213            int j;
214            string mstrChar;
215            string[] mstrFlag=new string[4];
216            string mstrReturn="";
217            bool mblnAddzero=false;
218
219            mstrFlag[0= "";
220            mstrFlag[1= "T";
221            mstrFlag[2= "H";
222            mstrFlag[3= "S";
223                
224            for (i = 1;i<=numstr.Length;i++
225            {
226                j = numstr.Length  - i;
227                mstrChar = numstr.Substring(i-1,1); 
228                if (mstrChar != "0" && j > 1{mstrReturn = mstrReturn + mstrChar + mstrFlag[(j - 2% 4];}
229                if (mstrChar == "0" && mblnAddzero==false)
230                {
231                    mstrReturn = mstrReturn + "0";
232                    mblnAddzero = true;
233                }

234                if (j == 14)
235                {
236                    if (mstrReturn.Substring(mstrReturn.Length-1== "0"
237                    {mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1+ "W0";}
238                    else
239                    {mstrReturn = mstrReturn + "W";}
240                }

241                if (j == 2
242                {
243                    if (mstrReturn.Substring(mstrReturn.Length-1,1== "0")
244                    {mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1+ "Y0";}
245                    else
246                    {mstrReturn = mstrReturn + "Y";}
247                    //
248                }

249                if (j == 6)
250                {
251                    if (mstrReturn.Length  > 2
252                    {
253                        if (mstrReturn.Substring(mstrReturn.Length-2)  != "M0"
254                        {
255                            if (mstrReturn.Substring(mstrReturn.Length-1== "0")
256                            {mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1+ "W0";}
257                            else
258                            {mstrReturn = mstrReturn + "W";}
259                        }

260                    }

261                    else
262                    {
263                        if (mstrReturn.Substring(mstrReturn.Length-1== "0")
264                        {mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1+ "W0";}
265                        else
266                        {mstrReturn = mstrReturn + "W";}
267                    }

268                }

269                if (j == 10)
270                {
271                    if (mstrReturn.Substring(mstrReturn.Length-1== "0"
272                    {mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1+ "M0";}
273                    else
274                    {mstrReturn = mstrReturn + "M";}
275                }

276                if (j == 0 && mstrChar != "0"{mstrReturn = mstrReturn + mstrChar + "F";}
277                if (j == 1 && mstrChar != "0"{mstrReturn = mstrReturn + mstrChar + "J";}
278                if (mstrChar != "0"{mblnAddzero = false;}
279            }

280            if (mstrReturn.Substring(01== "1" && mstrReturn.Substring(11== mstrFlag[1]) {mstrReturn = mstrReturn.Substring(1);}
281            if (mstrReturn.Substring(mstrReturn.Length-11== "0"){mstrReturn = mstrReturn.Substring(0,mstrReturn.Length-1);}
282            if (mstrReturn.Substring(01== "0"{mstrReturn = mstrReturn.Substring(1);}
283            if (mstrReturn.Substring(mstrReturn.Length-11== "M" || mstrReturn.Substring(mstrReturn.Length-11== "W" || mstrReturn.Substring(mstrReturn.Length-11== "S" || mstrReturn.Substring(mstrReturn.Length-11== "H" || mstrReturn.Substring(mstrReturn.Length-11== "T"{mstrReturn = mstrReturn + "Y";}
284            return mstrReturn;
285        }

286
287
288    }

289}

这是我来博客园写的处女作^_^~~
posted @ 2006-08-10 16:34  match  阅读(8516)  评论(7编辑  收藏  举报