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

 

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

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

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

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

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

 44InBlock.gif                else
 45ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 46InBlock.gif                    ret = CharToNum(x[i]) + stringArrayLevelNames[x.Length - 1 - i] + ret;
 47ExpandedSubBlockEnd.gif                }

 48InBlock.gif            while ((i = ret.IndexOf("零零")) != -1)
 49ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 50InBlock.gif                ret = ret.Remove(i, 1);
 51ExpandedSubBlockEnd.gif            }

 52InBlock.gif            if (ret[ret.Length - 1== '' && ret.Length > 1)
 53ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 54InBlock.gif                ret = ret.Remove(ret.Length - 11);
 55ExpandedSubBlockEnd.gif            }

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

 60InBlock.gif            return ret;
 61ExpandedSubBlockEnd.gif        }

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

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

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

 88InBlock.gif                else
 89ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 90InBlock.gif                    ret += temp;
 91ExpandedSubBlockEnd.gif                }

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

 98InBlock.gif                else
 99ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
100InBlock.gif                    ret += temp;
101ExpandedSubBlockEnd.gif                }

102InBlock.gif
103ExpandedSubBlockEnd.gif            }

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

109InBlock.gif            while ((i = ret.IndexOf("零零")) != -1)
110ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
111InBlock.gif                ret = ret.Remove(i, 1);
112ExpandedSubBlockEnd.gif            }

113InBlock.gif            if (ret[ret.Length - 1== '' && ret.Length > 1)
114ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
115InBlock.gif                ret = ret.Remove(ret.Length - 11);
116ExpandedSubBlockEnd.gif            }

117InBlock.gif            return ret;
118ExpandedSubBlockEnd.gif        }

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

127InBlock.gif            return ret;
128ExpandedSubBlockEnd.gif        }

129InBlock.gif
130InBlock.gif        private string NumToChn(string x)
131ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
132InBlock.gif            if (x.Length == 0)
133ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
134InBlock.gif                return "";
135ExpandedSubBlockEnd.gif            }

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

142InBlock.gif            if (x[0].ToString() == ".")
143ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
144InBlock.gif                x = "0" + x;
145ExpandedSubBlockEnd.gif            }

146InBlock.gif            if (x[x.Length - 1].ToString() == ".")
147ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
148InBlock.gif                x = x.Remove(x.Length - 11);
149ExpandedSubBlockEnd.gif            }

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

154InBlock.gif            else
155ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
156InBlock.gif                ret += StrToInt(x);
157ExpandedSubBlockEnd.gif            }

158InBlock.gif            return ret;
159ExpandedSubBlockEnd.gif        }

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

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

206InBlock.gif            return mstrSource;
207ExpandedSubBlockEnd.gif        }

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

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

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

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

260ExpandedSubBlockEnd.gif                    }

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

268ExpandedSubBlockEnd.gif                }

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

276ExpandedSubBlockStart.gifContractedSubBlock.gif                if (j == 0 && mstrChar != "0"dot.gif{mstrReturn = mstrReturn + mstrChar + "F";}
277ExpandedSubBlockStart.gifContractedSubBlock.gif                if (j == 1 && mstrChar != "0"dot.gif{mstrReturn = mstrReturn + mstrChar + "J";}
278ExpandedSubBlockStart.gifContractedSubBlock.gif                if (mstrChar != "0"dot.gif{mblnAddzero = false;}
279ExpandedSubBlockEnd.gif            }

280ExpandedSubBlockStart.gifContractedSubBlock.gif            if (mstrReturn.Substring(01== "1" && mstrReturn.Substring(11== mstrFlag[1]) dot.gif{mstrReturn = mstrReturn.Substring(1);}
281ExpandedSubBlockStart.gifContractedSubBlock.gif            if (mstrReturn.Substring(mstrReturn.Length-11== "0")dot.gif{mstrReturn = mstrReturn.Substring(0,mstrReturn.Length-1);}
282ExpandedSubBlockStart.gifContractedSubBlock.gif            if (mstrReturn.Substring(01== "0"dot.gif{mstrReturn = mstrReturn.Substring(1);}
283ExpandedSubBlockStart.gifContractedSubBlock.gif            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"dot.gif{mstrReturn = mstrReturn + "Y";}
284InBlock.gif            return mstrReturn;
285ExpandedSubBlockEnd.gif        }

286InBlock.gif
287InBlock.gif
288ExpandedSubBlockEnd.gif    }

289ExpandedBlockEnd.gif}

这是我来博客园写的处女作^_^~~

转载于:https://www.cnblogs.com/match/archive/2006/08/10/473490.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值