[原创] 用C#将数字转换为人民币大写

网上已经有一些数字转换为人民币大写的源码和控件,但还是自己动手写了一个。一是觉得好玩,二是希望能给需要的朋友提供一些方便:

  1 None.gif      public   class  XConvert
  2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
  3InBlock.gif        public static string ToRMB(Double e)
  4ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
  5InBlock.gif            return ToRMB(System.Convert.ToDecimal(e));
  6ExpandedSubBlockEnd.gif        }

  7InBlock.gif
  8InBlock.gif        public static string ToRMB(Decimal e)
  9ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 10InBlock.gif            string eString;//数字的格式化字符串
 11InBlock.gif            string eNum;//单数字
 12InBlock.gif            int eLen;//格式化字符串长度
 13InBlock.gif
 14InBlock.gif            System.Text.StringBuilder rmb=new System.Text.StringBuilder();//人民币大写
 15InBlock.gif            string yuan;//
 16InBlock.gif            bool seriesZero;//连续0标志
 17InBlock.gif            bool minus=false;//负数标志
 18InBlock.gif
 19InBlock.gif            if (e==0m)
 20ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 21InBlock.gif                return "零圆整";
 22ExpandedSubBlockEnd.gif            }

 23InBlock.gif            if (e<0m)
 24ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 25InBlock.gif                minus=true;
 26InBlock.gif                e=System.Math.Abs(e);                
 27ExpandedSubBlockEnd.gif            }

 28InBlock.gif            if (e>999999999999.99m)
 29ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 30InBlock.gif                throw new Exception("超过最大范围");
 31ExpandedSubBlockEnd.gif            }

 32InBlock.gif
 33InBlock.gif            eString=e.ToString("0.00");
 34InBlock.gif            eLen=eString.Length;
 35InBlock.gif            yuan=(eString.Substring(0,1)=="0"?"":"");
 36InBlock.gif
 37InBlock.gif            eNum=eString.Substring(eLen-1,1);//分位
 38InBlock.gif            if (eNum=="0")
 39ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 40InBlock.gif                rmb.Append("");
 41InBlock.gif                seriesZero=true;
 42ExpandedSubBlockEnd.gif            }

 43InBlock.gif            else
 44ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 45InBlock.gif                rmb.Append(stringNum(eNum)+"");
 46InBlock.gif                seriesZero=false;
 47ExpandedSubBlockEnd.gif            }

 48InBlock.gif
 49InBlock.gif            eNum=eString.Substring(eLen-2,1);//角位
 50InBlock.gif            if (eNum=="0")
 51ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 52InBlock.gif                if (!seriesZero)
 53ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 54InBlock.gif                    if (!(eLen==4&&eString.Substring(0,1)=="0"))
 55ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 56InBlock.gif                        rmb.Insert(0,"");
 57ExpandedSubBlockEnd.gif                    }

 58ExpandedSubBlockEnd.gif                }

 59ExpandedSubBlockEnd.gif            }

 60InBlock.gif            else
 61ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 62InBlock.gif                rmb.Insert(0,stringNum(eNum)+"");            
 63InBlock.gif                seriesZero=false;
 64ExpandedSubBlockEnd.gif            }

 65InBlock.gif
 66InBlock.gif            if (eLen<=7)
 67ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 68InBlock.gif                rmb.Insert(0,stringNum4(eString.Substring(0,eLen-3))+yuan);
 69ExpandedSubBlockEnd.gif            }

 70InBlock.gif            else if (eLen<=11)
 71ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 72InBlock.gif                rmb.Insert(0,stringNum4(eString.Substring(eLen-7,4))+yuan);
 73InBlock.gif                rmb.Insert(0,stringNum4(eString.Substring(0,eLen-7))+"");
 74ExpandedSubBlockEnd.gif            }

 75InBlock.gif            else if (eLen<=15)
 76ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 77InBlock.gif                rmb.Insert(0,stringNum4(eString.Substring(eLen-7,4))+yuan);
 78InBlock.gif                rmb.Insert(0,stringNum4(eString.Substring(eLen-11,4))+(eString.Substring(eLen-11,4)=="0000"?"":""));
 79InBlock.gif                rmb.Insert(0,stringNum4(eString.Substring(0,eLen-11))+"亿");
 80ExpandedSubBlockEnd.gif            }

 81InBlock.gif
 82InBlock.gif            if (minus) rmb.Insert(0,"");
 83InBlock.gif
 84InBlock.gif            return rmb.ToString();
 85ExpandedSubBlockEnd.gif        }

 86InBlock.gif
 87InBlock.gif        private static string stringNum4(string eNum4)
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 89InBlock.gif            string eNum;
 90InBlock.gif            bool seriesZero=false;
 91InBlock.gif            System.Text.StringBuilder rmb4=new System.Text.StringBuilder();
 92InBlock.gif            int eLen=eNum4.Length;
 93InBlock.gif
 94InBlock.gif            eNum=eNum4.Substring(eLen-1,1);//个位
 95InBlock.gif            if (eNum=="0")
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 97InBlock.gif                seriesZero=true;
 98ExpandedSubBlockEnd.gif            }

 99InBlock.gif            else
100ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
101InBlock.gif                rmb4.Append(stringNum(eNum));
102ExpandedSubBlockEnd.gif            }

103InBlock.gif
104InBlock.gif            if (eLen>=2)//十位
105ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
106InBlock.gif                eNum=eNum4.Substring(eLen-2,1);
107InBlock.gif                if (eNum=="0")
108ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
109InBlock.gif                    if (!seriesZero)
110ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
111InBlock.gif                        rmb4.Insert(0,"");
112InBlock.gif                        seriesZero=true;
113ExpandedSubBlockEnd.gif                    }

114ExpandedSubBlockEnd.gif                }

115InBlock.gif                else
116ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
117InBlock.gif                    rmb4.Insert(0,stringNum(eNum)+"");
118InBlock.gif                    seriesZero=false;
119ExpandedSubBlockEnd.gif                }

120ExpandedSubBlockEnd.gif            }

121InBlock.gif
122InBlock.gif            if (eLen>=3)//百位
123ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
124InBlock.gif                eNum=eNum4.Substring(eLen-3,1);
125InBlock.gif                if(eNum=="0")
126ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
127InBlock.gif                    if (!seriesZero)
128ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
129InBlock.gif                        rmb4.Insert(0,"");
130InBlock.gif                        seriesZero=true;
131ExpandedSubBlockEnd.gif                    }

132ExpandedSubBlockEnd.gif                }

133InBlock.gif                else
134ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
135InBlock.gif                    rmb4.Insert(0,stringNum(eNum)+"");
136InBlock.gif                    seriesZero=false;
137ExpandedSubBlockEnd.gif                }

138ExpandedSubBlockEnd.gif            }

139InBlock.gif
140InBlock.gif            if (eLen==4)//千位
141ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
142InBlock.gif                eNum=eNum4.Substring(0,1);
143InBlock.gif                if(eNum=="0")
144ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
145InBlock.gif                    if (!seriesZero)
146ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
147InBlock.gif                        rmb4.Insert(0,"");
148InBlock.gif                        seriesZero=true;
149ExpandedSubBlockEnd.gif                    }

150ExpandedSubBlockEnd.gif                }

151InBlock.gif                else
152ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
153InBlock.gif                    rmb4.Insert(0,stringNum(eNum)+"");
154InBlock.gif                    seriesZero=false;
155ExpandedSubBlockEnd.gif                }

156ExpandedSubBlockEnd.gif            }

157InBlock.gif
158InBlock.gif            return rmb4.ToString();
159ExpandedSubBlockEnd.gif        }

160InBlock.gif
161InBlock.gif        private static string stringNum(string eNum)
162ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
163InBlock.gif            switch (eNum)
164ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
165InBlock.gif                case "1":
166InBlock.gif                    return "";
167InBlock.gif                case "2":
168InBlock.gif                    return "";
169InBlock.gif                case "3":
170InBlock.gif                    return "";
171InBlock.gif                case "4":
172InBlock.gif                    return "";
173InBlock.gif                case "5":
174InBlock.gif                    return "";
175InBlock.gif                case "6":
176InBlock.gif                    return "";
177InBlock.gif                case "7":
178InBlock.gif                    return "";
179InBlock.gif                case "8":
180InBlock.gif                    return "";
181InBlock.gif                case "9":
182InBlock.gif                    return "";
183InBlock.gif                default:
184InBlock.gif                    return "";
185ExpandedSubBlockEnd.gif            }

186ExpandedSubBlockEnd.gif        }

187InBlock.gif
不足之处,望朋友们指正。

转载于:https://www.cnblogs.com/chinadhf/archive/2005/12/13/296125.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值