金额转换为人民币大写(C#)
2
3 namespace 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 - 1, 1);
55 }
56 if (ret.Length >= 2 && ret.Substring(0, 2) == "一十")
57 {
58 ret = ret.Remove(0, 1);
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 - 4, 4));
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 - 8, 4));
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 - 4, 4));
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 + 1, 1);
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 - 1, 1);
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 - 1, 1);
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-1, 1) != "分")
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(0, 1) == "1" && mstrReturn.Substring(1, 1) == mstrFlag[1]) {mstrReturn = mstrReturn.Substring(1);}
281 if (mstrReturn.Substring(mstrReturn.Length-1, 1) == "0"){mstrReturn = mstrReturn.Substring(0,mstrReturn.Length-1);}
282 if (mstrReturn.Substring(0, 1) == "0") {mstrReturn = mstrReturn.Substring(1);}
283 if (mstrReturn.Substring(mstrReturn.Length-1, 1) == "M" || mstrReturn.Substring(mstrReturn.Length-1, 1) == "W" || mstrReturn.Substring(mstrReturn.Length-1, 1) == "S" || mstrReturn.Substring(mstrReturn.Length-1, 1) == "H" || mstrReturn.Substring(mstrReturn.Length-1, 1) == "T") {mstrReturn = mstrReturn + "Y";}
284 return mstrReturn;
285 }
286
287
288 }
289}
这是我来博客园写的处女作^_^~~
转载于:https://www.cnblogs.com/match/archive/2006/08/10/473490.html
金额转换为人民币大写(C#)相关推荐
- 小写金额转换为人民币大写
首先,定义一系列的常量,包括0~9对应的大写汉字.单位."整".零元整.负数.四舍五入后精确的位数: private static final String[] CN_UPPER_ ...
- 把数字转换为人民币大写(用于银行系统)
大二是老师让做一个综合试验::用C++实现简易银行系统,其中用到在打印流水账时要求把金额转换为人民币大写...当时傻里吧唧的,,,随便拷贝了个同学的应付老师交了了事...前一段一个大二的弟兄向我提起银 ...
- php 0改成百 千_【面试题】小数转换为人民币大写形式,PHP实现。
前段时间面试,有个编程题目是将小数转换为人民币大写形式,最近用PHP多,就写一段,全当记个日志吧.未完待续. $newline = ' '; $RMB = array('', '壹', '贰', '叁 ...
- 30行,金额转人民币大写的代码
本文转自:http://www.cnblogs.com/stephen-wang/p/3155958.html 金额转人民币大写是一种常见的要求,但是这一看似简单的要求,实现起来却并不容易. 前不久, ...
- .net 将数字转换为人民币大写
程序中用到将数字转换为大写人民币,在网上找了一个方法,记录下来. using System.Text; using System.Text.RegularExpressions; ///<sum ...
- 将数字字符串转换为人民币大写,壹、贰、叁、肆、伍、陆、柒、捌、玖、拾
将数字字符串转换为人民币大写,壹.贰.叁.肆.伍.陆.柒.捌.玖.拾 ''' 将数字字符串转成大写字符串 ''' a="零.壹.贰.叁.肆.伍.陆.柒.捌.玖.拾" b=" ...
- 将金额转换为中文大写
将金额转换为中文大写 思路 判断符号,0则直接返回 数字去掉点 00结尾的要加'整' 循环,从后面每次取一个数位上的值直到最前面, 若是非零则加上对应的数值和单位 若是零,判断上一个是否为零,是,则不 ...
- 浮点数字转换为人民币大写字体
新开博客,贴上一段平常时写的代码,权当记录和分享. 下面是代码块(有比较详细的注释,转换过程中需要注意的是0的处理): //浮点数转换为人民币大写字体 import java.util.Scanner ...
- 正则也很牛,把阿拉伯数字的金额转换为中文大写数字
using System; using System.Text.RegularExpressions; class Program { // 把阿拉伯数字的金额转换为中文大写数字 static ...
最新文章
- 视觉稿与H5页面之间的终端适配
- CM005-逆向分析过程(上篇)
- mysql 导入gtid_开启gtid导入报错
- linux系统资源管理系统,linux基础4系统资源管理
- 电信机顶盒怎么连接鸿蒙系统电视,电信机顶盒的密码是多少,怎么改密码
- DjVu、PDF中的隐藏文本
- python将多个txt内容合并_python合并多个txt文件成为一个文件
- android显示txt文件的组件,Android文本控件的介绍
- 谷歌Chrome浏览器如何开启无痕模式 Chrome浏览器无痕模式开启方法
- 微软Windows 8 非常实用的12个技巧
- 庆祝61-牛客模拟笔试七月场(使圆圈队形中相邻小朋友的身高差的最大值最小的解法)
- matlab 两个视频同时播放视频,视频画面合并教程:两个或多个视频合并在同一屏幕上同时播放...
- 使用Python解密仿射密码
- web前端之CSS样式案例--博雅网页
- 模拟器安装不了apk,fail to start adbCheck settings to verify your chosen adb path is valid.
- 百度移动营销页常见问题汇总,99%的人不知道
- Android 10 SystemUI 如何添加4G信号和WiFi图标
- 赛事快讯|2022中国工程机器人大赛——飞思无人机仿真与自主任务赛项演示视频来啦!
- Node-RED中通过node-red-ui-webcam节点实现访问摄像头并截取照片预览
- 快刀初试:Spark GraphX在淘宝的实践
热门文章
- 你真正做到敏捷了吗?
- 水仙花数的while方法
- 开关电源:效率与VOUT的关系
- TiDB 在金融行业关键业务场景的实践(下篇)
- 计算机图形学(四)几何变换_4_二维复合变换_4_二维刚体变换
- 优美图案c语言程序,C语言编程之一个最优美的图案
- Android SELinux开发入门指南之如何增加Java Binder Service权限
- pcf85263 linux驱动源码,PCF85263AT/AJ
- HTML设计一个图书管理网页
- [集合竞价-AI量化]天天做超短,集合竞价的盘口语言你真的读懂了麽?