class Program { static void Main(string[] args) { string sInput = ""; string sOutput; while (sInput != "exit") { Console.Write("\r\n输入一个三位数(100~999),输入exit结束:"); sInput = Console.ReadLine(); if (sInput != "exit") { fun f = new fun(sInput); sOutput = f.myConvertSum(); if (string.IsNullOrEmpty(sOutput)) Console.Write("无法转换,请确认输入的是一个三位数(100~999)!"); else Console.Write("转换结果:{0}", sOutput); } else { break; } } } } class fun { string snum=""; public fun(string s) { snum = s; } public string myConvertSum() { if (!IsPositveDecimal(snum)) return ""; return Convert3Digit(int.Parse(snum)); } /// /// 判断是否是3位数正数字字符串 /// /// name= "str "> 判断字符串 /// 如果是数字,返回true,否则返回false private bool IsPositveDecimal(string str) {
int d; try {
d = int.Parse(str);
}
catch (Exception) {
return false; }
if (d >= 100 && d <= 999) //限定为3位数 return true; else
return false; }
///
/// 转换三位数字100-999 中文大写 ///
private string Convert3Digit(int num) {
const string chineseNum = "零壹贰叁肆伍陆柒捌玖";
string rstring = "";
if (num < 100 || num > 999) return rstring; int ge = num % 10;
int shi = (num % 100 - ge) / 10; int bai = num / 100; if (ge != 0)
{
rstring = chineseNum.Substring(ge, 1); if (shi != 0)
rstring = chineseNum.Substring(shi, 1) + "拾" + rstring; else
rstring = chineseNum.Substring(shi, 1) + rstring; }
else if (shi != 0) {
} } }
rstring = chineseNum.Substring(shi, 1) + "拾" + rstring; }
rstring = chineseNum.Substring(bai, 1) + "佰" + rstring; return rstring;
本文来源:https://www.wddqw.com/doc/fb6d5625bcd126fff7050bec.html