使用C#实现阿拉伯数字到大写中文的转换
程序员文章站
2022-06-08 22:30:40
先记下来,以备后用!
///
/// 金额转为大写金额
/// ...
先记下来,以备后用!
/// <summary> /// 金额转为大写金额 /// </summary> public class moneyconvertchinese { /// <summary> /// 金额转为大写金额 /// </summary> /// <param name="lowermoney"></param> /// <returns></returns> public string moneytochinese(string lowermoney) { string functionreturnvalue = null; bool isnegative = false; // 是否是负数 if (lowermoney.trim().substring(0, 1) == "-") { // 是负数则先转为正数 lowermoney = lowermoney.trim().remove(0, 1); isnegative = true; } string strlower = null; string strupart = null; string strupper = null; int itemp = 0; // 保留两位小数 123.489→123.49 123.4→123.4 lowermoney = math.round(double.parse(lowermoney), 2).tostring(); if (lowermoney.indexof(".") > 0) { if (lowermoney.indexof(".") == lowermoney.length - 2) { lowermoney = lowermoney + "0"; } } else { lowermoney = lowermoney + ".00"; } strlower = lowermoney; itemp = 1; strupper = ""; while (itemp <= strlower.length) { switch (strlower.substring(strlower.length - itemp, 1)) { case ".": strupart = "圆"; break; case "0": strupart = "零"; break; case "1": strupart = "壹"; break; case "2": strupart = "贰"; break; case "3": strupart = "叁"; break; case "4": strupart = "肆"; break; case "5": strupart = "伍"; break; case "6": strupart = "陆"; break; case "7": strupart = "柒"; break; case "8": strupart = "捌"; break; case "9": strupart = "玖"; break; } switch (itemp) { case 1: strupart = strupart + "分"; break; case 2: strupart = strupart + "角"; break; case 3: strupart = strupart + ""; break; case 4: strupart = strupart + ""; break; case 5: strupart = strupart + "拾"; break; case 6: strupart = strupart + "佰"; break; case 7: strupart = strupart + "仟"; break; case 8: strupart = strupart + "万"; break; case 9: strupart = strupart + "拾"; break; case 10: strupart = strupart + "佰"; break; case 11: strupart = strupart + "仟"; break; case 12: strupart = strupart + "亿"; break; case 13: strupart = strupart + "拾"; break; case 14: strupart = strupart + "佰"; break; case 15: strupart = strupart + "仟"; break; case 16: strupart = strupart + "万"; break; default: strupart = strupart + ""; break; } strupper = strupart + strupper; itemp = itemp + 1; } strupper = strupper.replace("零拾", "零"); strupper = strupper.replace("零佰", "零"); strupper = strupper.replace("零仟", "零"); strupper = strupper.replace("零零零", "零"); strupper = strupper.replace("零零", "零"); strupper = strupper.replace("零角零分", "整"); strupper = strupper.replace("零分", "整"); strupper = strupper.replace("零角", "零"); strupper = strupper.replace("零亿零万零圆", "亿圆"); strupper = strupper.replace("亿零万零圆", "亿圆"); strupper = strupper.replace("零亿零万", "亿"); strupper = strupper.replace("零万零圆", "万圆"); strupper = strupper.replace("零亿", "亿"); strupper = strupper.replace("零万", "万"); strupper = strupper.replace("零圆", "圆"); strupper = strupper.replace("零零", "零"); // 对壹圆以下的金额的处理 if (strupper.substring(0, 1) == "圆") { strupper = strupper.substring(1, strupper.length - 1); } if (strupper.substring(0, 1) == "零") { strupper = strupper.substring(1, strupper.length - 1); } if (strupper.substring(0, 1) == "角") { strupper = strupper.substring(1, strupper.length - 1); } if (strupper.substring(0, 1) == "分") { strupper = strupper.substring(1, strupper.length - 1); } if (strupper.substring(0, 1) == "整") { strupper = "零圆整"; } functionreturnvalue = strupper; if (isnegative == true) { return "负" + functionreturnvalue; } else { return functionreturnvalue; } } }
测试代码:
static void main(string[] args) { console.write("请输入要转成大写的数字:"); string str = console.readline(); console.writeline("大写:" + new moneyconvertchinese().moneytochinese(str)); console.readline(); }
测试结果:
下面是其它网友的补充:
using system; namespace consoleapp { /// <summary> /// 本类实现阿拉伯数字到大写中文的转换 /// 该类没有对非法数字进行判别 /// 请调用numtochn方法 /// </summary> public class numformat { public numformat() { // // todo: 在此处添加构造函数逻辑 // } // 转换数字 private char tonum(char x) { string strchnnames="零一二三四五六七八九"; string strnumnames="0123456789"; return strchnnames[strnumnames.indexof(x)]; } // 转换万以下整数 private string changeint(string x) { string[] strarraylevelnames=new string[4] {"","十","百","千"}; string ret = ""; int i; for (i=x.length-1;i>=0;i--) if (x[i] == '0') ret = tonum(x[i]) + ret; else ret = tonum(x[i]) + strarraylevelnames[x.length-1-i] + ret; while ((i=ret.indexof("零零"))!=-1) ret=ret.remove(i, 1); if (ret[ret.length-1]=='零' && ret.length>1) ret=ret.remove(ret.length-1,1); if (ret.length>=2 && ret.substring(0,2)=="一十") ret=ret.remove(0,1); return ret; } // 转换整数 private string toint(string x) { int len = x.length; string ret,temp; if (len<=4) ret = changeint(x); else if (len<=8) { ret = changeint(x.substring(0,len-4)) + "万"; temp = changeint(x.substring(len-4,4)); if (temp.indexof("千")==-1 && temp!="") ret += "零" + temp; else ret += temp; } else { ret=changeint(x.substring(0,len-8)) + "亿"; temp=changeint(x.substring(len-8,4)); if (temp.indexof("千")==-1 && temp!="") ret += "零" + temp; else ret += temp; ret += "万"; temp = changeint(x.substring(len-4,4)); if (temp.indexof("千")==-1 && temp!="") ret += "零" + temp; else ret += temp; } int i; if ((i=ret.indexof("零万"))!=-1) ret = ret.remove(i+1,1); while ((i=ret.indexof("零零"))!=-1) ret = ret.remove(i,1); if (ret[ret.length-1]=='零' && ret.length>1) ret = ret.remove(ret.length-1,1); return ret; } private string todecimal(string x) { string ret=""; for (int i=0;i<x.length;i++) ret += tonum(x[i]); return ret; } public string numtochn(string x) { if (x.length==0) return ""; string ret=""; if (x[0]=='-') { ret="负"; x=x.remove(0,1); } if (x[0].tostring()==".") x="0"+x; if (x[x.length-1].tostring()==".") x=x.remove(x.length-1,1); if (x.indexof(".")>-1) ret += toint(x.substring(0,x.indexof(".")))+"点"+todecimal(x.substring(x.indexof(".")+1)); else ret += toint(x); return ret; } } }
测试工程
using system; namespace consoleapp { class mainclass { static void main(string[] args) { /* system.console.writeline("hello, the world!"); system.console.writeline("my love!"); classtest ct = new classtest(); system.console.writeline(ct.get_str()); */ /* // 重载运算符 myvector v1 = new myvector(5, 12); myvector v2 = new myvector(4, 3); myvector v3 = new myvector(); v3 = v1 + v2; system.console.writeline("{0}测试一下", v3.length); */ // 转换成大写数字 numformat nf = new numformat(); string x; while (true) { console.write("x="); x = console.readline(); if (x == "") break; console.writeline("{0}={1}", x, nf.numtochn(x)); } } } }