/// <summary>
/// 本类实现阿拉伯数字到大写中文的转换
/// 该类没有对非法数字进行判别请事先自己判断数字是否合法
/// </summary>
public class ChineseNum
{
//小写转大写
public static string GetChineseNum(string p_num)
{
ChineseNum cn = new ChineseNum();
return cnNumToChn(p_num);
}
//小写金额转大写金额
public static string GetUpperMoney(double p_Money)
{
ChineseNum cn = new ChineseNum();
return cnGetMoneyChinese(p_Money);
}
//转换数字
private char CharToNum(char x)
{
string stringChnNames = 零一二三四五六七八九;
string stringNumNames = ;
return stringChnNames[stringNumNamesIndexOf(x)];
}
//转换万以下整数
private string WanStrToInt(string x)
{
string[] stringArrayLevelNames = new string[] { 十 百 千 };
string ret = ;
int i;
for (i = xLength ; i >= ; i)
if (x[i] == )
{
ret = CharToNum(x[i]) + ret;
}
else
{
ret = CharToNum(x[i]) + stringArrayLevelNames[xLength i] + ret;
}
while ((i = retIndexOf(零零)) != )
{
ret = retRemove(i );
}
if (ret[retLength ] == 零 && retLength > )
{
ret = retRemove(retLength );
}
if (retLength >= && retSubstring( ) == 一十)
{
ret = retRemove( );
}
return ret;
}
//转换整数
private string StrToInt(string x)
{
int len = xLength;
string ret temp;
if (len <= )
{
ret = WanStrToInt(x);
}
else if (len <= )
{
ret = WanStrToInt(xSubstring( len )) + 万;
temp = WanStrToInt(xSubstring(len ));
if (tempIndexOf(千) == && temp != )
ret += 零 + temp;
else
ret += temp;
}
else
{
ret = WanStrToInt(xSubstring( len )) + 亿;
temp = WanStrToInt(xSubstring(len ));
if (tempIndexOf(千) == && temp != )
{
ret += 零 + temp;
}
else
{
ret += temp;
}
ret += 万;
temp = WanStrToInt(xSubstring(len ));
if (tempIndexOf(千) == && temp != )
{
ret += 零 + temp;
}
else
{
ret += temp;
}
}
int i;
if ((i = retIndexOf(零万)) != )
{
ret = retRemove(i + );
}
while ((i = retIndexOf(零零)) != )
{
ret = retRemove(i );
}
if (ret[retLength ] == 零 && retLength > )
{
ret = retRemove(retLength );
}
return ret;
}
//转换小数
private string StrToDouble(string x)
{
string ret = ;
for (int i = ; i < xLength; i++)
{
ret += CharToNum(x[i]);
}
return ret;
}
private string NumToChn(string x)
{
if (xLength == )
{
return ;
}
string ret = ;
if (x[] == )
{
ret = 负;
x = xRemove( );
}
if (x[]ToString() == )
{
x = + x;
}
if (x[xLength ]ToString() == )
{
x = xRemove(xLength );
}
if (xIndexOf() > )
{
ret += StrToInt(xSubstring( xIndexOf())) + 点 + StrToDouble(xSubstring(xIndexOf() + ));
}
else
{
ret += StrToInt(x);
}
return ret;
}
//金额转换
private string GetMoneyChinese(Double Money)
{
int i;
string mstrSource;
if (Money == )
{
return ;
}
mstrSource = MoneyToString(#);
i = mstrSourceIndexOf();
if (i > ) { mstrSource = mstrSourceReplace( ); }
if (mstrSourceSubstring( ) == ) { mstrSource = mstrSourceRemove( ); }
mstrSource = NumstrToChinese(mstrSource);
if (mstrSourceLength == ) { return ; }
//负
if (Money < )
{
mstrSource = 负 + mstrSource;
}
mstrSource = mstrSourceReplace( 零);
mstrSource = mstrSourceReplace( 壹);
mstrSource = mstrSourceReplace( 贰);
mstrSource = mstrSourceReplace( 三);
mstrSource = mstrSourceReplace( 肆);
mstrSource = mstrSourceReplace( 伍);
mstrSource = mstrSourceReplace( 陆);
mstrSource = mstrSourceReplace( 柒);
mstrSource = mstrSourceReplace( 捌);
mstrSource = mstrSourceReplace( 玖);
mstrSource = mstrSourceReplace(M 亿);
mstrSource = mstrSourceReplace(W 万);
mstrSource = mstrSourceReplace(S 仟);
mstrSource = mstrSourceReplace(H 佰);
mstrSource = mstrSourceReplace(T 拾);
mstrSource = mstrSourceReplace(Y 圆);
mstrSource = mstrSourceReplace(J 角);
mstrSource = mstrSourceReplace(F 分);
if (mstrSourceSubstring(mstrSourceLength ) != 分)
{
mstrSource = mstrSource + 整;
}
return mstrSource;
}
//金额转换
private string NumstrToChinese(string numstr)
{
int i;
int j;
string mstrChar;
string[] mstrFlag = new string[];
string mstrReturn = ;
bool mblnAddzero = false;
mstrFlag[] = ;
mstrFlag[] = T;
mstrFlag[] = H;
mstrFlag[] = S;
for (i = ; i <= numstrLength; i++)
{
j = numstrLength i;
mstrChar = numstrSubstring(i );
if (mstrChar != && j > ) { mstrReturn = mstrReturn + mstrChar + mstrFlag[(j ) % ]; }
if (mstrChar == && mblnAddzero == false)
{
mstrReturn = mstrReturn + ;
mblnAddzero = true;
}
if (j == )
{
if (mstrReturnSubstring(mstrReturnLength ) == )
{ mstrReturn = mstrReturnSubstring( mstrReturnLength ) + W; }
else
{ mstrReturn = mstrReturn + W; }
}
if (j == )
{
if (mstrReturnSubstring(mstrReturnLength ) == )
{ mstrReturn = mstrReturnSubstring( mstrReturnLength ) + Y; }
else
{ mstrReturn = mstrReturn + Y; }
//元
}
if (j == )
{
if (mstrReturnLength > )
{
if (mstrReturnSubstring(mstrReturnLength ) != M)
{
if (mstrReturnSubstring(mstrReturnLength ) == )
{ mstrReturn = mstrReturnSubstring( mstrReturnLength ) + W; }
else
{ mstrReturn = mstrReturn + W; }
}
}
else
{
if (mstrReturnSubstring(mstrReturnLength ) == )
{ mstrReturn = mstrReturnSubstring( mstrReturnLength ) + W; }
else
{ mstrReturn = mstrReturn + W; }
}
}
if (j == )
{
if (mstrReturnSubstring(mstrReturnLength ) == )
{ mstrReturn = mstrReturnSubstring( mstrReturnLength ) + M; }
else
{ mstrReturn = mstrReturn + M; }
}
if (j == && mstrChar != ) { mstrReturn = mstrReturn + mstrChar + F; }
if (j == && mstrChar != ) { mstrReturn = mstrReturn + mstrChar + J; }
if (mstrChar != ) { mblnAddzero = false; }
}
if (mstrReturnSubstring( ) == && mstrReturnSubstring( ) == mstrFlag[]) { mstrReturn = mstrReturnSubstring(); }
if (mstrReturnSubstring(mstrReturnLength ) == ) { mstrReturn = mstrReturnSubstring( mstrReturnLength ); }
if (mstrReturnSubstring( ) == ) { mstrReturn = mstrReturnSubstring(); }
if (mstrReturnSubstring(mstrReturnLength ) == M || mstrReturnSubstring(mstrReturnLength ) == W || mstrReturnSubstring(mstrReturnLength ) == S || mstrReturnSubstring(mstrReturnLength ) == H || mstrReturnSubstring(mstrReturnLength ) == T) { mstrReturn = mstrReturn + Y; }
return mstrReturn;
}
}