using System;
\n
namespace ConsoleApp
\n
{
\n
/// <summary>
\n
/// 本类实现阿拉伯数字到大写中文的转换
\n
/// 该类没有对非法数字进行判别
\n
/// 请调用NumToChn方法
\n
/// </summary>
\n
public class NumFormat
\n
{
\n
public NumFormat()
\n
{
\n
//
\n
// TOD 在此处添加构造函数逻辑
\n
//
\n
}
\n
// 转换数字
\n
private char ToNum(char x)
\n
{
\n
string strChnNames=”零一二三四五六七八九”;
\n
string strNumNames=”0123456789″;
\n
return strChnNames[strNumNames.IndexOf(x)];
\n
}
\n
// 转换万以下整数
\n
private string ChangeInt(string x)
\n
{
\n
string[] strArrayLevelNames=new string[4] {“”,”十”,”百”,”千”};
\n
string ret = “”;
\n
int i;
\n
for (i=x.Length-1;i>=0;i–)
\n
if (x[i] == ’0′)
\n
ret = ToNum(x[i]) + ret;
\n
else
\n
ret = ToNum(x[i]) + strArrayLevelNames[x.Length-1-i] + ret;
\n
while ((i=ret.IndexOf(“零零”))!=-1)
\n
ret=ret.Remove(i, 1);
\n
if (ret[ret.Length-1]==’零’ && ret.Length>1)
\n
ret=ret.Remove(ret.Length-1,1);
\n
if (ret.Length>=2 && ret.Substring(0,2)==”一十”)
\n
ret=ret.Remove(0,1);
\n
return ret;
\n
}
\n
// 转换整数
\n
private string ToInt(string x)
\n
{
\n
int len = x.Length;
\n
string ret,temp;
\n
if (len<=4)
\n
ret = ChangeInt(x);
\n
else if (len<=8)
\n
{
\n
ret = ChangeInt(x.Substring(0,len-4)) + “万”;
\n
temp = ChangeInt(x.Substring(len-4,4));
\n
if (temp.IndexOf(“千”)==-1 && temp!=”")
\n
ret += “零” + temp;
\n
else
\n
ret += temp;
\n
}
\n
else
\n
{
\n
ret=ChangeInt(x.Substring(0,len-8)) + “亿”;
\n
temp=ChangeInt(x.Substring(len-8,4));
\n
if (temp.IndexOf(“千”)==-1 && temp!=”")
\n
ret += “零” + temp;
\n
else
\n
ret += temp;
\n
ret += “万”;
\n
temp = ChangeInt(x.Substring(len-4,4));
\n
if (temp.IndexOf(“千”)==-1 && temp!=”")
\n
ret += “零” + temp;
\n
else
\n
ret += temp;
\n
}
\n
int i;
\n
if ((i=ret.IndexOf(“零万”))!=-1)
\n
ret = ret.Remove(i+1,1);
\n
while ((i=ret.IndexOf(“零零”))!=-1)
\n
ret = ret.Remove(i,1);
\n
if (ret[ret.Length-1]==’零’ && ret.Length>1)
\n
ret = ret.Remove(ret.Length-1,1);
\n
return ret;
\n
}
\n
private string ToDecimal(string x)
\n
{
\n
string ret=”";
\n
for (int i=0;i<x.Length;i++)
\n
ret += ToNum(x[i]);
\n
return ret;
\n
}
\n
public string NumToChn(string x)
\n
{
\n
if (x.Length==0)
\n
return “”;
\n
string ret=”";
\n
if (x[0]==’-\')
\n
{
\n
ret=”负”;
\n
x=x.Remove(0,1);
\n
}
\n
if (x[0].ToString()==”.”)
\n
x=”0″+x;
\n
if (x[x.Length-1].ToString()==”.”)
\n
x=x.Remove(x.Length-1,1);
\n
if (x.IndexOf(“.”)>-1)
\n
ret += ToInt(x.Substring(0,x.IndexOf(“.”)))+”点”+ToDecimal(x.Substring(x.IndexOf(“.”)+1));
\n
else
\n
ret += ToInt(x);
\n
return ret;
\n
}
\n
}
\n
}
\n
测试工程
\n
using System;
\n
namespace ConsoleApp
\n
{
\n
class MainClass
\n
{
\n
static void Main(string[] args)
\n
{
\n
/*
\n
System.Console.WriteLine(“Hello, The World!”);
\n
System.Console.WriteLine(“My Love!”);
\n
ClassTest ct = new ClassTest();
\n
System.Console.WriteLine(ct.Get_Str());
\n
*/
\n
/*
\n
// 重载运算符
\n
MyVector v1 = new MyVector(5, 12);
\n
MyVector v2 = new MyVector(4, 3);
\n
MyVector v3 = new MyVector();
\n
v3 = v1 + v2;
\n
System.Console.WriteLine(“{0}测试一下”, v3.Length);
\n
*/
\n
// 转换成大写数字
\n
NumFormat nf = new NumFormat();
\n
string x;
\n
while (true)
\n
{
\n
Console.Write(“X=”);
\n
x = Console.ReadLine();
\n
if (x == “”) break;
\n
Console.WriteLine(“{0}={1}”, x, nf.NumToChn(x));
\n
}
\n
}
\n
}
\n
}
\n
来源:CSDN