好像没有什么问题了耶。 调用 :
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.textBox1.TextChanged -= new EventHandler(textBox1_TextChanged);
this.textBox1.Text = GetChineseMoney.GetEngFormatNum(this.textBox1.Text);
this.textBox1.Select(this.textBox1.TextLength, 0);
this.textBox2.Text = GetChineseMoney.ConvertToChineseMoney (this.textBox1.Text);
this.textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
}
类:
public class GetChineseMoney
{
private static string ConvertSingleUpperNum(char inputChar)
{
switch (inputChar)
{
case '1':
return "壹";
case '2':
return "贰";
case '3':
return "叁";
case '4':
return "肆";
case '5':
return "伍";
case '6':
return "陆";
case '7':
return "柒";
case '8':
return "捌";
case '9':
return "玖";
case '0':
return "零";
}
return "";
}
/// <summary>
/// 把4位数字转换成相应的形式
/// </summary>
/// <param name="inputStr">传入的字符</param>
/// <returns>处理好的大写字符</returns>
private static string Convert4Num(string inputStr)
{
//while(inputStr.Length<4)
//{
// inputStr = "0" + inputStr;
//}
string result = "";
result += ConvertSingleUpperNum(inputStr[0]) + "千";
result += ConvertSingleUpperNum(inputStr[1]) + "百";
result += ConvertSingleUpperNum(inputStr[2]) + "十";
result += ConvertSingleUpperNum(inputStr[3]);
result = result.Replace("零千", "零");
result = result.Replace("零百", "零");
result = result.Replace("零十", "零");
return result;
}
//每四位获取单位
private static string[] GetUnit(int length)
{
string[] result = new string[length];
result[length - 1] = "";
for (int i = length - 2; i >= 0; i--)
{
if ((i - length) * (-1) % 2 == 0)
{
result[i] = "万";
}
else
{
result[i] = "亿";
}
}
return result;
}
//主方法
public static string ConvertToChineseMoney(string inputStr)
{
string[] strArray = null;
if (inputStr.IndexOf('.') >= 0)
{
strArray = inputStr.Split('.');
if (strArray == null || strArray.Length != 2)
{
return "错误";
}
string strAbovePoint = ConvertInt(strArray[0]);
string strAfterPoint = "";
if (strArray[1].Length == 1)
{
strAfterPoint += ConvertSingleUpperNum(strArray[1][0]) + "角";
}
if (strArray[1].Length >= 2)
{
strAfterPoint += ConvertSingleUpperNum(strArray[1][0]) + "角" + ConvertSingleUpperNum(strArray[1][1]) + "分";
}
return strAbovePoint + "元" + strAfterPoint;
}
else
{
return ConvertInt(inputStr);
}
}
private static string ConvertInt(string inputStr)
{
if (inputStr.Length >= 22)
{
return "溢出";
}
if (string.IsNullOrEmpty(inputStr))
{
return "";
}
string result = "";
inputStr = inputStr.Replace(",", "");
while (inputStr.Length % 4 != 0)
{
inputStr = "0" + inputStr;
}
int colLength = inputStr.Length / 4;
string[] strCol = new string[colLength];
for (int i = 0, j = 0; i < inputStr.Length; i = i + 4, j++)
{
strCol[j] = inputStr.Substring(i, 4);
}
string[] danwei = GetUnit(colLength);
for (int i = 0; i < colLength; i++)
{
if (strCol[i] == "0000" && danwei[i] == "万")
{
result += Convert4Num(strCol[i]);
}
else
{
result += Convert4Num(strCol[i]) + danwei[i];
}
}
while (result.IndexOf("零零") >= 0)
{
result = result.Replace("零零", "零");
}
result = result.Replace("零万", "万");
result = result.Replace("零亿", "亿");
while (result.StartsWith("零"))
{
result = result.Substring(1);
}
while (result.EndsWith("零"))
{
result = result.Substring(0, result.Length - 1);
}
return result;
}
public static string GetEngFormatNum(string inputStr)
{
string tempStr = inputStr.Replace(",", "");
string tail = "";
if (tempStr.IndexOf('.') >= 0)
{
tail = tempStr.Substring(tempStr.IndexOf('.'));
tempStr = tempStr.Substring(0, tempStr.IndexOf('.'));
}
string result = "";
for (int i = tempStr.Length - 1; i >= 0; i--)
{
if ((tempStr.Length - i) % 3 == 0)
{
result = "," + tempStr[i] + result;
}
else
{
result = tempStr[i] + result;
}
}
if (result.StartsWith(","))
{
result = result.Substring(1);
}
return result + tail;
}
}