项目需要将阿拉伯数字转为中文序号,google了一下,网上散发的源代码片断颇有点混乱,我不喜欢为这种小问题使用太长的代码,自己动手丰衣足食.看起来还是很简洁的,只实现int类型转汉字。由于中文小数就是一个位一个位读,所以直接将double.toString(),然后小数点后的阿拉伯数字字符与一个中文数字字符数组配对就可以了,不予实现。源代码实现为int的扩展方法,如下:
/// <summary>
/// 将整数转为大写的中文数字
/// </summary>
/// <param name="ni_intInput"></param>
/// <returns></returns>
public static string ToCNUpperCase(this int ni_intInput)
{
string tstrRet = "";
int tintInput;
int tintRemainder, tintDigitPosIndex = 0;
int tintLoopX = 0;
string[] tastrNumCNChar = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
string[] tastrDigitPosCNChar = new string[] { "", "十", "佰", "仟", "万", "亿" };
tintInput = ni_intInput;
tintLoopX = 0;
while (tintInput / 10 > 0 || tintInput > 0)
{
tintRemainder = (tintInput % 10);
if (tintLoopX == 5)//十万
tintDigitPosIndex = 1;
else if (tintLoopX == 8)//亿
tintDigitPosIndex = 5;
else if (tintLoopX == 9)//十亿
tintDigitPosIndex = 1;
//end if
if (tintRemainder > 0)
tstrRet
= tastrNumCNChar[tintRemainder] + tastrDigitPosCNChar[tintDigitPosIndex] + tstrRet;
else
tstrRet
= tastrNumCNChar[tintRemainder] + tstrRet; ;
//end if
tintDigitPosIndex += 1;
tintLoopX += 1;
tintInput /= 10;
}//end while
tstrRet = System.Text.RegularExpressions.Regex.Replace(tstrRet, "零零*零*", "零");
return tstrRet;
}//end
/// <summary>
/// 将整数转为小写的中文数字
/// </summary>
/// <param name="ni_intInput"></param>
/// <returns></returns>
public static string ToCNLowerCase(this int ni_intInput)
{
string tstrRet = "";
int tintInput;
int tintRemainder, tintDigitPosIndex = 0;
int tintLoopX = 0;
string[] tastrNumCNChar = new string[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
string[] tastrDigitPosCNChar = new string[] { "", "十", "百", "千", "万", "亿" };
tintInput = ni_intInput;
tintLoopX = 0;
while (tintInput / 10 > 0 || tintInput > 0)
{
tintRemainder = (tintInput % 10);
if (tintLoopX == 5)//十万
tintDigitPosIndex = 1;
else if (tintLoopX == 8)//亿
tintDigitPosIndex = 5;
else if (tintLoopX == 9)//十亿
tintDigitPosIndex = 1;
//end if
if (tintRemainder > 0)
tstrRet
= tastrNumCNChar[tintRemainder] + tastrDigitPosCNChar[tintDigitPosIndex] + tstrRet;
else
tstrRet
= tastrNumCNChar[tintRemainder] + tstrRet; ;
//end if
tintDigitPosIndex += 1;
tintLoopX += 1;
tintInput /= 10;
}//end while
tstrRet = System.Text.RegularExpressions.Regex.Replace(tstrRet, "零零*零*", "零");
return tstrRet;
}//end