public class Number2English
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private string[] StrNO = new string[19];
private string[] Unit = new string[8];
private string[] StrTens = new string[9];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string NumberToString(double Number)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string Str;
string BeforePoint;
string AfterPoint;
string tmpStr;
int nBit;
string CurString;
int nNumLen;
Init();
Str = Convert.ToString(Math.Round(Number,2));
if (Str.IndexOf(".")==-1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BeforePoint = Str;
AfterPoint = "";
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BeforePoint = Str.Substring(0,Str.IndexOf("."));
AfterPoint = Str.Substring(Str.IndexOf(".")+1,Str.Length - Str.IndexOf(".")-1);
}
if (BeforePoint.Length > 12)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
Str = "";
while (BeforePoint.Length > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nNumLen = Len(BeforePoint);
if (nNumLen % 3 == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CurString = Left(BeforePoint, 3);
BeforePoint = Right(BeforePoint, nNumLen - 3);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CurString = Left(BeforePoint, (nNumLen % 3));
BeforePoint = Right(BeforePoint, nNumLen - (nNumLen % 3));
}
nBit = Len(BeforePoint) / 3;
tmpStr = DecodeHundred(CurString);
if((BeforePoint == Len(BeforePoint).ToString() || nBit ==0) && Len(CurString) ==3)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (System.Convert.ToInt32(Left(CurString, 1)) != 0 & System.Convert.ToInt32(Right(CurString, 2)) != 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tmpStr = Left(tmpStr, tmpStr.IndexOf(Unit[3]) + Len(Unit[3])) + Unit[7] + " " + Right(tmpStr, Len(tmpStr) - (tmpStr.IndexOf(Unit[3]) + Len(Unit[3])));
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tmpStr = Unit[7] + " " + tmpStr;
}
}
if (nBit == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Str = Convert.ToString(Str + " " + tmpStr).Trim();
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Str = Convert.ToString(Str + " " + tmpStr + " " + Unit[nBit-1]).Trim();
}
if (Left(Str, 3) == Unit[7])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Str = Convert.ToString(Right(Str, Len(Str) - 3)).Trim();
}
if( BeforePoint == Len(BeforePoint).ToString())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "";
}
}
BeforePoint = Str;
if (Len(AfterPoint) > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
AfterPoint = Unit[5] + " " + DecodeHundred(AfterPoint) + " " + Unit[6];
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
AfterPoint = Unit[4];
}
return BeforePoint + " " + AfterPoint;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void Init()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (StrNO[0] != "One")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
StrNO[0] = "One";
StrNO[1] = "Two";
StrNO[2] = "Three";
StrNO[3] = "Four";
StrNO[4] = "Five";
StrNO[5] = "Six";
StrNO[6] = "Seven";
StrNO[7] = "Eight";
StrNO[8] = "Nine";
StrNO[9] = "Ten";
StrNO[10] = "Eleven";
StrNO[11] = "Twelve";
StrNO[12] = "Thirteen";
StrNO[13] = "Fourteen";
StrNO[14] = "Fifteen";
StrNO[15] = "Sixteen";
StrNO[16] = "Seventeen";
StrNO[17] = "Eighteen";
StrNO[18] = "Nineteen";
StrTens[0] = "Ten";
StrTens[1] = "Twenty";
StrTens[2] = "Thirty";
StrTens[3] = "Forty";
StrTens[4] = "Fifty";
StrTens[5] = "Sixty";
StrTens[6] = "Seventy";
StrTens[7] = "Eighty";
StrTens[8] = "Ninety";
Unit[0] = "Thousand";
Unit[1] = "Million";
Unit[2] = "Billion";
Unit[3] = "Hundred";
Unit[4] = "Only";
Unit[5] = "Point";
Unit[6] = "Cent";
Unit[7] = "And";
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string DecodeHundred(string HundredString)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int tmp;
string rtn="";
if( Len(HundredString) > 0 && Len(HundredString) <= 3)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch (Len(HundredString))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case 1:
tmp = System.Convert.ToInt32(HundredString);
if (tmp != 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
rtn=StrNO[tmp-1].ToString();
}
break;
case 2:
tmp = System.Convert.ToInt32(HundredString);
if (tmp != 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((tmp < 20))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
rtn=StrNO[tmp-1].ToString();
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (System.Convert.ToInt32(Right(HundredString, 1)) == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
rtn=StrTens[Convert.ToInt32(tmp / 10)-1].ToString();
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
rtn=Convert.ToString(StrTens[Convert.ToInt32(tmp / 10)-1] + "-" + StrNO[System.Convert.ToInt32(Right(HundredString, 1))-1]);
}
}
}
break;
case 3:
if (System.Convert.ToInt32(Left(HundredString, 1)) != 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
rtn=Convert.ToString(StrNO[System.Convert.ToInt32(Left(HundredString, 1))-1] + " " + Unit[3] + " " + DecodeHundred(Right(HundredString, 2)));
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
rtn=DecodeHundred(Right(HundredString, 2)).ToString();
}
break;
default:
break;
}
}
return rtn;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string Left(string str,int n)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.Substring(0,n);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string Right(string str,int n)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.Substring(str.Length-n,n);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int Len(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.Length;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public class DecimalToUpper
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public DecimalToUpper()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 小写金额转换为大写金额,其他条件:金额小于一万亿,最多两位小数
/// </summary>
/// <param name="d">源金额,d 《 1000000000000.00(一万亿),且最多两位小数 </param>
/// <returns>结果,大写金额</returns>
public string ToUpper(decimal d)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (d == 0)
return "零元整";
string je = d.ToString("####.00");
if (je.Length > 15)
return "";
je = new String('0',15 - je.Length) + je; //若小于15位长,前面补0
string stry = je.Substring(0,4); //取得'亿'单元
string strw = je.Substring(4,4); //取得'万'单元
string strg = je.Substring(8,4); //取得'元'单元
string strf = je.Substring(13,2); //取得小数部分
string str1 = "",str2 = "",str3 = "";
str1 = this.getupper(stry,"亿"); //亿单元的大写
str2 = this.getupper(strw,"万"); //万单元的大写
str3 = this.getupper(strg,"元"); //元单元的大写
string str_y = "", str_w = "";
if (je[3] == '0' || je[4] == '0') //亿和万之间是否有0
str_y = "零";
if (je[7] == '0' || je[8] == '0') //万和元之间是否有0
str_w = "零";
string ret = str1 + str_y + str2 + str_w + str3; //亿,万,元的三个大写合并
for (int i = 0 ;i < ret.Length;i++) //去掉前面的"零"
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ret[i] != '零')
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ret = ret.Substring(i);
break;
}
}
for (int i = ret.Length - 1;i > -1 ;i--) //去掉最后的"零"
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ret[i] != '零')
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ret = ret.Substring(0,i+1);
break;
}
}
if (ret[ret.Length - 1] != '元') //若最后不位不是'元',则加一个'元'字
ret = ret + "元";
if (ret == "零零元") //若为零元,则去掉"元数",结果只要小数部分
ret = "";
if (strf == "00") //下面是小数部分的转换
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ret = ret + "整";
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string tmp = "";
tmp = this.getint(strf[0]);
if (tmp == "零")
ret = ret + tmp;
else
ret = ret + tmp + "角";
tmp = this.getint(strf[1]);
if (tmp == "零")
ret = ret + "整";
else
ret = ret + tmp + "分";
}
if (ret[0] == '零')
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ret = ret.Substring(1); //防止0.03转为"零叁分",而直接转为"叁分"
}
return ret; //完成,返回
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 把一个单元转为大写,如亿单元,万单元,个单元
/// </summary>
/// <param name="str">这个单元的小写数字(4位长,若不足,则前面补零)</param>
/// <param name="strDW">亿,万,元</param>
/// <returns>转换结果</returns>
private string getupper(string str,string strDW)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (str == "0000")
return "";
string ret = "";
string tmp1 = this.getint(str[0]) ;
string tmp2 = this.getint(str[1]) ;
string tmp3 = this.getint(str[2]) ;
string tmp4 = this.getint(str[3]) ;
if (tmp1 != "零")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ret = ret + tmp1 + "仟";
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ret = ret + tmp1;
}
if (tmp2 != "零")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ret = ret + tmp2 + "佰";
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (tmp1 != "零") //保证若有两个零'00',结果只有一个零,下同
ret = ret + tmp2;
}
if (tmp3 != "零")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ret = ret + tmp3 + "拾";
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (tmp2 != "零")
ret = ret + tmp3;
}
if (tmp4 != "零")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ret = ret + tmp4 ;
}
if (ret[0] == '零') //若第一个字符是'零',则去掉
ret = ret.Substring(1);
if (ret[ret.Length - 1] == '零') //若最后一个字符是'零',则去掉
ret = ret.Substring(0,ret.Length - 1);
return ret + strDW; //加上本单元的单位
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 单个数字转为大写
/// </summary>
/// <param name="c">小写阿拉伯数字 0---9</param>
/// <returns>大写数字</returns>
private string getint(char c)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string str = "";
switch ( c )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case '0':
str = "零";
break;
case '1':
str = "壹";
break;
case '2':
str = "贰";
break;
case '3':
str = "叁";
break;
case '4':
str = "肆";
break;
case '5':
str = "伍";
break;
case '6':
str = "陆";
break;
case '7':
str = "柒";
break;
case '8':
str = "拐";
break;
case '9':
str = "玖";
break;
}
return str;
}
}
posted @
2006-10-12 13:21
疾行者
阅读(
416)
评论()
编辑
收藏
举报