1.阿拉伯数字转换英文数字表示算法解析
例如:
1,111,111,111,111,111.00
1千1百1十1兆1千1百1十1亿1千1百1十1万1千1百1十1
分整数部分处理和小数部分处理
A.整数部分处理算法
三位数字一节
从右边数第一个三位数字单位为空
从右边数第二个三位数字单位万
从右边数第三个三位数字单位亿
从右边数第三个三位数字单位兆
然后循环处理每三位 千,百,个
单位用美元(dollar)
B.小数部分处理(只保留2为小数)
个位和十位处理 单位用美分(CNET)
注意事项:注意零的处理
Code
public class Number2English
{
//存放One--Nineteen
private readonly string[] StrNO = new string[19];
//存放Ten--Ninety
private readonly string[] StrTens = new string[9];
//Unit:存放单位
private readonly string[] Unit = new string[8];
public string NumberToString(double Number)
{
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) //没有小数点
{
BeforePoint = Str;
AfterPoint = "";
}
else //带有小数点
{
BeforePoint = Str.Substring(0, Str.IndexOf("."));
AfterPoint = Str.Substring(Str.IndexOf(".") + 1, Str.Length - Str.IndexOf(".") - 1);
}
if (BeforePoint.Length > 12) //整数部分大于12位返回空
{
return null;
}
Str = "";
while (BeforePoint.Length > 0)
{
#region MyRegion
nNumLen = Len(BeforePoint);
if (nNumLen % 3 == 0)
{
CurString = Left(BeforePoint, 3);
BeforePoint = Right(BeforePoint, nNumLen - 3);
}
else
{
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)
{
if (Convert.ToInt32(Left(CurString, 1)) != 0 & Convert.ToInt32(Right(CurString, 2)) != 0)
{
tmpStr = Left(tmpStr, tmpStr.IndexOf(Unit[3]) + Len(Unit[3])) + Unit[7] + " " +
Right(tmpStr, Len(tmpStr) - (tmpStr.IndexOf(Unit[3]) + Len(Unit[3])));
}
else
{
tmpStr = Unit[7] + " " + tmpStr;
}
}
if (nBit == 0)
{
Str = Convert.ToString(Str + " " + tmpStr).Trim();
}
else
{
Str = Convert.ToString(Str + " " + tmpStr + " " + Unit[nBit - 1]).Trim();
}
if (Left(Str, 3) == Unit[7])
{
Str = Convert.ToString(Right(Str, Len(Str) - 3)).Trim();
}
if (BeforePoint == Len(BeforePoint).ToString())
{
return "";
}
#endregion
}
BeforePoint = Str;
if (Len(AfterPoint) > 0)
{
AfterPoint = Unit[5] + " " + DecodeHundred(AfterPoint) + " " + Unit[6];
}
else
{
AfterPoint = Unit[4];
}
return (BeforePoint + " " + AfterPoint).ToUpper();
}
/// <summary>
/// 初始化常量数组
/// StrNO:存放One--Nineteen,StrTens:存放Ten--Ninety,Unit:存放单位
/// </summary>
private void Init()
{
if (StrNO[0] != "One")
{
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] = "Dollars and";
Unit[6] = "Cent";
Unit[7] = "";
}
}
private string DecodeHundred(string HundredString)
{
int tmp;
string rtn = "";
if (Len(HundredString) > 0 && Len(HundredString) <= 3)
{
switch (Len(HundredString))
{
case 1:
tmp = Convert.ToInt32(HundredString);
if (tmp != 0)
{
rtn = StrNO[tmp - 1];
}
break;
case 2:
tmp = Convert.ToInt32(HundredString);
if (tmp != 0)
{
if ((tmp < 20))
{
rtn = StrNO[tmp - 1];
}
else
{
if (Convert.ToInt32(Right(HundredString, 1)) == 0)
{
rtn = StrTens[Convert.ToInt32(tmp / 10) - 1];
}
else
{
rtn =
Convert.ToString(StrTens[Convert.ToInt32(tmp / 10) - 1] + " " +
StrNO[Convert.ToInt32(Right(HundredString, 1)) - 1]);
}
}
}
break;
case 3:
if (Convert.ToInt32(Left(HundredString, 1)) != 0)
{
rtn =
Convert.ToString(StrNO[Convert.ToInt32(Left(HundredString, 1)) - 1] + " " + Unit[3] +
"AND " +
DecodeHundred(Right(HundredString, 2)));
}
else
{
rtn = DecodeHundred(Right(HundredString, 2));
}
break;
default:
break;
}
}
return rtn;
}
/// <summary>
/// 从字符窜str的左侧第n个位置截取子字符串
/// </summary>
/// <param name="str"></param>
/// <param name="n"></param>
/// <returns></returns>
private string Left(string str, int n)
{
return str.Substring(0, n);
}
/// <summary>
/// 从字符窜str的右侧第那n位置截取子字符串
/// </summary>
/// <param name="str"></param>
/// <param name="n"></param>
/// <returns></returns>
private string Right(string str, int n)
{
return str.Substring(str.Length - n, n);
}
/// <summary>
/// 获取字符串长度
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
private int Len(string str)
{
return str.Length;
}
}
public class Number2English
{
//存放One--Nineteen
private readonly string[] StrNO = new string[19];
//存放Ten--Ninety
private readonly string[] StrTens = new string[9];
//Unit:存放单位
private readonly string[] Unit = new string[8];
public string NumberToString(double Number)
{
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) //没有小数点
{
BeforePoint = Str;
AfterPoint = "";
}
else //带有小数点
{
BeforePoint = Str.Substring(0, Str.IndexOf("."));
AfterPoint = Str.Substring(Str.IndexOf(".") + 1, Str.Length - Str.IndexOf(".") - 1);
}
if (BeforePoint.Length > 12) //整数部分大于12位返回空
{
return null;
}
Str = "";
while (BeforePoint.Length > 0)
{
#region MyRegion
nNumLen = Len(BeforePoint);
if (nNumLen % 3 == 0)
{
CurString = Left(BeforePoint, 3);
BeforePoint = Right(BeforePoint, nNumLen - 3);
}
else
{
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)
{
if (Convert.ToInt32(Left(CurString, 1)) != 0 & Convert.ToInt32(Right(CurString, 2)) != 0)
{
tmpStr = Left(tmpStr, tmpStr.IndexOf(Unit[3]) + Len(Unit[3])) + Unit[7] + " " +
Right(tmpStr, Len(tmpStr) - (tmpStr.IndexOf(Unit[3]) + Len(Unit[3])));
}
else
{
tmpStr = Unit[7] + " " + tmpStr;
}
}
if (nBit == 0)
{
Str = Convert.ToString(Str + " " + tmpStr).Trim();
}
else
{
Str = Convert.ToString(Str + " " + tmpStr + " " + Unit[nBit - 1]).Trim();
}
if (Left(Str, 3) == Unit[7])
{
Str = Convert.ToString(Right(Str, Len(Str) - 3)).Trim();
}
if (BeforePoint == Len(BeforePoint).ToString())
{
return "";
}
#endregion
}
BeforePoint = Str;
if (Len(AfterPoint) > 0)
{
AfterPoint = Unit[5] + " " + DecodeHundred(AfterPoint) + " " + Unit[6];
}
else
{
AfterPoint = Unit[4];
}
return (BeforePoint + " " + AfterPoint).ToUpper();
}
/// <summary>
/// 初始化常量数组
/// StrNO:存放One--Nineteen,StrTens:存放Ten--Ninety,Unit:存放单位
/// </summary>
private void Init()
{
if (StrNO[0] != "One")
{
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] = "Dollars and";
Unit[6] = "Cent";
Unit[7] = "";
}
}
private string DecodeHundred(string HundredString)
{
int tmp;
string rtn = "";
if (Len(HundredString) > 0 && Len(HundredString) <= 3)
{
switch (Len(HundredString))
{
case 1:
tmp = Convert.ToInt32(HundredString);
if (tmp != 0)
{
rtn = StrNO[tmp - 1];
}
break;
case 2:
tmp = Convert.ToInt32(HundredString);
if (tmp != 0)
{
if ((tmp < 20))
{
rtn = StrNO[tmp - 1];
}
else
{
if (Convert.ToInt32(Right(HundredString, 1)) == 0)
{
rtn = StrTens[Convert.ToInt32(tmp / 10) - 1];
}
else
{
rtn =
Convert.ToString(StrTens[Convert.ToInt32(tmp / 10) - 1] + " " +
StrNO[Convert.ToInt32(Right(HundredString, 1)) - 1]);
}
}
}
break;
case 3:
if (Convert.ToInt32(Left(HundredString, 1)) != 0)
{
rtn =
Convert.ToString(StrNO[Convert.ToInt32(Left(HundredString, 1)) - 1] + " " + Unit[3] +
"AND " +
DecodeHundred(Right(HundredString, 2)));
}
else
{
rtn = DecodeHundred(Right(HundredString, 2));
}
break;
default:
break;
}
}
return rtn;
}
/// <summary>
/// 从字符窜str的左侧第n个位置截取子字符串
/// </summary>
/// <param name="str"></param>
/// <param name="n"></param>
/// <returns></returns>
private string Left(string str, int n)
{
return str.Substring(0, n);
}
/// <summary>
/// 从字符窜str的右侧第那n位置截取子字符串
/// </summary>
/// <param name="str"></param>
/// <param name="n"></param>
/// <returns></returns>
private string Right(string str, int n)
{
return str.Substring(str.Length - n, n);
}
/// <summary>
/// 获取字符串长度
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
private int Len(string str)
{
return str.Length;
}
}