熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#實現阿拉伯數字到大寫中文的轉換

2022-06-13   來源: .NET編程 

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


From:http://tw.wingwit.com/Article/program/net/201311/13523.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.