c#

位置:IT落伍者 >> c# >> 浏览文章

C#实现阿拉伯数字到大写中文的转换


发布日期:2023年01月30日
 
C#实现阿拉伯数字到大写中文的转换

/// <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;

}

}

               

上一篇:教你如何有效监控.NET应用程序

下一篇:C#区别和认识四个判等函数