经常有的一些报表程序需要打印人民币的大写金额,所以写了一个9位之内的人民币小写向大写转换的工具。
代码如下:
using System;
using System.Text;
namespace RMBLowerToUpper
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class LToU
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
// if(args.Length!=1)
// {
// Console.WriteLine("error number!");
// return;
// }
string number=null;
do
{
Console.WriteLine("Please input a number:");
StringBuilder sb = new StringBuilder();
number=Console.ReadLine();
string[] div=number.Split('.');
if(div.Length!=2 || div[1].Length !=2)
{
Console.WriteLine("error number!");
return;
}
// foreach(string n in div)
// {
// Console.WriteLine(n);
// }
#region--------处理整数部分-------------
char[] bit=div[0].ToCharArray();
//先转换成大写
for(int i=0;i<bit.Length ;i++)
{
switch(bit[i])
{
case '1':
sb.Append("壹");
break;
case '2':
sb.Append ("贰");
break;
case '3':
sb.Append("叁");
break;
case '4':
sb.Append ("肆");
break;
case '5':
sb.Append("伍");
break;
case '6':
sb.Append ("陆");
break;
case '7':
sb.Append("柒");
break;
case '8':
sb.Append ("捌");
break;
case '9':
sb.Append("玖");
break;
//处理诸如10000.00,10020.00,1002000.00的情况
case '0':
if((i+1)<bit.Length && bit[i+1]=='0')
{
sb.Append ("");
}
else if(i==bit.Length -1 || bit.Length -i==5 || bit.Length -i==9)
{
sb.Append ("");
}
else
{
sb.Append ("零");
}
break;
}
//插入相应的分割单位
switch(bit.Length -i)
{
case 2:
if(bit[i]=='0')
{
sb.Append ("");
}
else
{
sb.Append ("拾");
}
break;
case 3:
if(bit[i]=='0')
{
sb.Append ("");
}
else
{
sb.Append ("百");
}
break;
case 4:
if(bit[i]=='0')
{
sb.Append ("");
}
else
{
sb.Append ("仟");
}
break;
case 5:
// if(bit[i]=='0')
// {
// sb.Append ("");
// }
// else
// {
sb.Append ("万");
// }
break;
case 6:
if(bit[i]=='0')
{
sb.Append ("");
}
else
{
sb.Append ("拾");
}
break;
case 7:
if(bit[i]=='0')
{
sb.Append ("");
}
else
{
sb.Append ("百");
}
break;
case 8:
if(bit[i]=='0')
{
sb.Append ("");
}
else
{
sb.Append ("仟");
}
break;
}
}
sb.Append ("圆整");
#endregion
#region ----------处理小数部分-----------
char[] point=div[1].ToCharArray();
for(int j=0;j<point.Length ;j++)
{
switch(point[j])
{
case '1':
sb.Append("壹");
break;
case '2':
sb.Append ("贰");
break;
case '3':
sb.Append("叁");
break;
case '4':
sb.Append ("肆");
break;
case '5':
sb.Append("伍");
break;
case '6':
sb.Append ("陆");
break;
case '7':
sb.Append("柒");
break;
case '8':
sb.Append ("捌");
break;
case '9':
sb.Append("玖");
break;
case '0':
if((j+1)<point.Length && point[j+1]=='0')
{
sb.Append ("");
}
else if(j==point.Length -1)
{
sb.Append ("");
}
else
{
sb.Append ("零");
}
break;
}
switch(point.Length -j)
{
case 2:
if(point[j]=='0')
{
sb.Append ("");
}
else
{
sb.Append ("角");
}
break;
case 1:
if(point[j]=='0')
{
sb.Append ("");
}
else
{
sb.Append ("分");
}
break;
}
}
#endregion
string u=sb.ToString();
Console.WriteLine(u);
}while(number.ToUpper()!="EXIT");
}
}
}
测试界面如下: