日期:2014-05-18 浏览次数:20824 次
using System.Text; namespace MoneytoChinese { class Test { static void Main() { for (; ; ) { System.Console.Write("金额: "); string s = System.Console.ReadLine(); decimal m = 450002030.05m; try { m = decimal.Parse(s); } catch { //break; } System.Console.WriteLine("大写: "+MoneytoChinese(m)); } } public static string MoneytoChinese(decimal money) { string ChineseMoney = ""; string[] snum = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; string[] sdanwei = new string[] { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" }; long lastnum; int i = 0; for (long tmpmoney = (long)(money * 100); tmpmoney >= 1; tmpmoney /= 10) { lastnum = tmpmoney % 10; ChineseMoney = snum[lastnum] + sdanwei[i++] + ChineseMoney; } ChineseMoney = ChineseMoney.Replace("零角", "零"); ChineseMoney = ChineseMoney.Replace("零零分", ""); ChineseMoney = ChineseMoney.Replace("零分", ""); ChineseMoney = ChineseMoney.Replace("零拾", "零"); ChineseMoney = ChineseMoney.Replace("零佰", "零"); ChineseMoney = ChineseMoney.Replace("零仟", "零"); ChineseMoney = ChineseMoney.Replace("零零", "零"); ChineseMoney = ChineseMoney.Replace("零零", "零"); ChineseMoney = ChineseMoney.Replace("零元", "元"); ChineseMoney = ChineseMoney.Replace("零万", "万"); ChineseMoney = ChineseMoney.Replace("零亿", "亿"); ChineseMoney = ChineseMoney.Replace("亿万", "亿"); return ChineseMoney; } } }
// 例如:(new Money(200)).ToString() == "贰佰元" namespace Skyiv.Util { using System.Text; class Test { static void Main() { for (;;) { System.Console.Write("金额: "); string s = System.Console.ReadLine(); decimal m; try { m = decimal.Parse(s); } catch { break; } System.Console.WriteLine("大写: " + new Money(m)); } } } // 该类重载的 ToString() 方法返回的是大写金额字符串 class Money { public string Yuan = "元"; // “元”,可以改为“圆”、“卢布”之类 public string Jiao = "角"; // “角”,可以改为“拾” public string Fen = "分"; // “分”,可以改为“美分”之类 static string Digit = "零壹贰叁肆伍陆柒捌