数字转中文方法(1)
今天休假,闲来无事,写了一个转数字为中文的程序,暂时时没有处理小数部分,不过处理方法应该差不多。
思路:
代码
public class NumberToCurrency
{
private string[] majorNames = { "千", "万", "亿"};
private string[] tenDoubleNams = { "", "一十", "二十", "三十", "四十", "五十", "六十", "七十", "八十", "九十" };
private string[] tenNames = { "", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九" };
private string[] numberNames = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
/// <summary>
/// 转超过1000的数
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public string ConvertMoreThanThousand(int number)
{
//越界
if (number < 999)
{
throw new ArgumentException("number must be greater than 999");
}
string result = string.Empty;
//处理千分位
string strNumber = number.ToString();
int numLen = strNumber.Length;
int tempNumber = Convert.ToInt32(strNumber.Substring(numLen - 4, 1)); //取千位
int lessThousand = Convert.ToInt32(strNumber.Substring(numLen - 3)); //千位之后所有位
result = numberNames[tempNumber] + "千";
if(lessThousand == 0)
{
//后面全为0
}
else if(lessThousand<999) //二位数,要补零
{
result += "零" + ConvertLessThanThousand(lessThousand);
}
else
{
result += ConvertLessThanThousand(lessThousand);
}
//千分位之上的数字,每4位作为一段进行处理
strNumber = strNumber.Remove(numLen - 4);
numLen = strNumber.Length; //重新计算长度
int index = 1;
string tempCalculate = string.Empty;
while (numLen > 0 && index<majorNames.Length)
{
tempCalculate = strNumber.Substring((numLen - 4) > 0 ? numLen - 4 : 0); //最后4位;
if (Convert.ToInt32(tempCalculate) < 1000)
{
tempCalculate = ConvertLessThanThousand(Convert.ToInt32(tempCalculate));
}
else
{
tempCalculate = ConvertMoreThanThousand(Convert.ToInt32(tempCalculate));
}
result = tempCalculate + majorNames[index] + result;
strNumber = strNumber.Remove((numLen - 4) > 0 ? numLen - 4 : 0);
numLen = strNumber.Length;
index++;
}
return result;
}
/// <summary>
/// 转小于1000 (0-999) 的数
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public string ConvertLessThanThousand(int number)
{
//越界
if (number < 0 || number > 999)
{
throw new ArgumentException("number must between 0 and 999");
}
string result = string.Empty;
if (number < 100)
{
return ConvertLessThanHundred(number);
}
else //大于100 (100-999)
{
int tempNumber = number / 100; //取百位
result = numberNames[tempNumber] + "百";
tempNumber = number - tempNumber * 100; //取个位与十位
if (tempNumber == 0) //tempNumber==0则就被整除
{
//do nothing
}
else if (tempNumber < 10) //存在零
{
result += "零" + ConvertLessThanHundred(tempNumber);
}
else
{
result += ConvertLessThanHundred(tempNumber);
}
}
return result;
}
/// <summary>
/// 转小于100 (0-99) 的数
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public string ConvertLessThanHundred(int number)
{
//越界
if (number < 0 || number > 99)
{
throw new ArgumentException("number must between 0 and 99");
}
string result = string.Empty; //存储返回结果
//0-10之间
if (number < 10)
{
result = numberNames[number];
}
else //10-99
{
if (number % 10 == 0) //10的倍数
{
result = tenDoubleNams[number / 10];
}
else //非10的倍数
{
result = tenDoubleNams[number / 10] + numberNames[number % 10]; //分别从十的倍数和数字数组中取
}
}
return result;
}
}
{
private string[] majorNames = { "千", "万", "亿"};
private string[] tenDoubleNams = { "", "一十", "二十", "三十", "四十", "五十", "六十", "七十", "八十", "九十" };
private string[] tenNames = { "", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九" };
private string[] numberNames = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
/// <summary>
/// 转超过1000的数
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public string ConvertMoreThanThousand(int number)
{
//越界
if (number < 999)
{
throw new ArgumentException("number must be greater than 999");
}
string result = string.Empty;
//处理千分位
string strNumber = number.ToString();
int numLen = strNumber.Length;
int tempNumber = Convert.ToInt32(strNumber.Substring(numLen - 4, 1)); //取千位
int lessThousand = Convert.ToInt32(strNumber.Substring(numLen - 3)); //千位之后所有位
result = numberNames[tempNumber] + "千";
if(lessThousand == 0)
{
//后面全为0
}
else if(lessThousand<999) //二位数,要补零
{
result += "零" + ConvertLessThanThousand(lessThousand);
}
else
{
result += ConvertLessThanThousand(lessThousand);
}
//千分位之上的数字,每4位作为一段进行处理
strNumber = strNumber.Remove(numLen - 4);
numLen = strNumber.Length; //重新计算长度
int index = 1;
string tempCalculate = string.Empty;
while (numLen > 0 && index<majorNames.Length)
{
tempCalculate = strNumber.Substring((numLen - 4) > 0 ? numLen - 4 : 0); //最后4位;
if (Convert.ToInt32(tempCalculate) < 1000)
{
tempCalculate = ConvertLessThanThousand(Convert.ToInt32(tempCalculate));
}
else
{
tempCalculate = ConvertMoreThanThousand(Convert.ToInt32(tempCalculate));
}
result = tempCalculate + majorNames[index] + result;
strNumber = strNumber.Remove((numLen - 4) > 0 ? numLen - 4 : 0);
numLen = strNumber.Length;
index++;
}
return result;
}
/// <summary>
/// 转小于1000 (0-999) 的数
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public string ConvertLessThanThousand(int number)
{
//越界
if (number < 0 || number > 999)
{
throw new ArgumentException("number must between 0 and 999");
}
string result = string.Empty;
if (number < 100)
{
return ConvertLessThanHundred(number);
}
else //大于100 (100-999)
{
int tempNumber = number / 100; //取百位
result = numberNames[tempNumber] + "百";
tempNumber = number - tempNumber * 100; //取个位与十位
if (tempNumber == 0) //tempNumber==0则就被整除
{
//do nothing
}
else if (tempNumber < 10) //存在零
{
result += "零" + ConvertLessThanHundred(tempNumber);
}
else
{
result += ConvertLessThanHundred(tempNumber);
}
}
return result;
}
/// <summary>
/// 转小于100 (0-99) 的数
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public string ConvertLessThanHundred(int number)
{
//越界
if (number < 0 || number > 99)
{
throw new ArgumentException("number must between 0 and 99");
}
string result = string.Empty; //存储返回结果
//0-10之间
if (number < 10)
{
result = numberNames[number];
}
else //10-99
{
if (number % 10 == 0) //10的倍数
{
result = tenDoubleNams[number / 10];
}
else //非10的倍数
{
result = tenDoubleNams[number / 10] + numberNames[number % 10]; //分别从十的倍数和数字数组中取
}
}
return result;
}
}
吃饭去了..........
Author:repository
From: http://repository.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
From: http://repository.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。