一没技术含量,玩玩而已
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MoneyConvert
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//转换事件
private void btnConvert_Click(object sender, EventArgs e)
{
//验证输入是否合法
string sNum = txtNum.Text.Trim();
//......
string sCh="";//用来存储转换后的字符
//开始转换
if (sNum.Contains('.'))//包含小数情况
{
string[] beforeNum = sNum.Split('.');
for (int i = 0; i < beforeNum[0].Length; ++i)
{
sCh += numConvertCh(beforeNum[0][i]);//数字转换为字符
sCh += unitConvertCh(beforeNum[0].Length - i);//给数字后面加单位
}
for (int i = 0; i < beforeNum[1].Length && i <= 1; ++i)
{
if (0 == i)
{
sCh += numConvertCh(beforeNum[1][i]);
sCh += "角";
}
else if (1 == i)
{
sCh += numConvertCh(beforeNum[1][i]);
sCh += "分";
}
}
}
else//不包含小数情况
{
for (int i = 0; i < sNum.Length; ++i)
{
sCh += numConvertCh(sNum[i]);//数字转换为字符
sCh += unitConvertCh(sNum.Length- i);//给数字后面加单位
}
}
//获取转换后的值,加以显示
txtCh.Text = sCh;
}
//数字转换方法函数
#region
private string numConvertCh(char c)
{
switch (c)
{
case '0':
return "零";
break;
case '1':
return "壹";
break;
case '2':
return "貮";
break;
case '3':
return "叁";
break;
case '4':
return "肆";
break;
case '5':
return "伍";
break;
case '6':
return "陆";
break;
case '7':
return "柒";
break;
case '8':
return "八";
break;
case '9':
return "玖";
break;
default:
return "";
}
}
#endregion
//单位转换函数
#region
private string unitConvertCh(int i)
{
switch (i)
{
case 1:
return "圆"; break;
case 2:
return "十"; break;
case 3:
return "佰"; break;
case 4:
return "千"; break;
case 5:
return "万"; break;
case 6:
return "亿"; break;
default:
return "";
}
}
#endregion
}
}