您现在的位置是:首页 >

如何把阿拉伯数字变成大写 C#实现阿拉伯数字到大写中文的转换

火烧 2021-07-23 16:34:02 1054
C#实现阿拉伯数字到大写中文的转换   /// lt ummary gt /// 本类实现阿拉伯数字到大写中文的转换 /// 该类没有对非法数字进行判别 请事先自己判断数字是否合法 /// lt /

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

  /// <summary>    /// 本类实现阿拉伯数字到大写中文的转换    /// 该类没有对非法数字进行判别 请事先自己判断数字是否合法    /// </summary>    public class ChineseNum    {

  //小写转大写        public static string GetChineseNum(string p_num)        {            ChineseNum cn = new ChineseNum();

  return cn NumToChn(p_num);        }

  //小写金额转大写金额

  public static string GetUpperMoney(double p_Money)        {            ChineseNum cn = new ChineseNum();

  return cn GetMoneyChinese(p_Money);        }

  //转换数字        private char CharToNum(char x)        {            string stringChnNames = 零一二三四五六七八九 ;            string stringNumNames = ;            return stringChnNames[stringNumNames IndexOf(x)];        }

  //转换万以下整数        private string WanStrToInt(string x)        {            string[] stringArrayLevelNames = new string[ ] { 十 百 千 };            string ret = ;            int i;            for (i = x Length ; i >= ; i )                if (x[i] == )                {                    ret = CharToNum(x[i]) + ret;                }                else                {                    ret = CharToNum(x[i]) + stringArrayLevelNames[x Length i] + ret;                }            while ((i = ret IndexOf( 零零 )) != )            {                ret = ret Remove(i );            }            if (ret[ret Length ] == 零 && ret Length > )            {                ret = ret Remove(ret Length );            }            if (ret Length >= && ret Substring( ) == 一十 )            {                ret = ret Remove( );            }            return ret;        }

  //转换整数        private string StrToInt(string x)        {            int len = x Length;            string ret temp;            if (len <= )            {                ret = WanStrToInt(x);            }            else if (len <= )            {                ret = WanStrToInt(x Substring( len )) + 万 ;                temp = WanStrToInt(x Substring(len ));                if (temp IndexOf( 千 ) == && temp != )                    ret += 零 + temp;                else                    ret += temp;            }            else            {                ret = WanStrToInt(x Substring( len )) + 亿 ;                temp = WanStrToInt(x Substring(len ));                if (temp IndexOf( 千 ) == && temp != )                {                    ret += 零 + temp;                }                else                {                    ret += temp;                }                ret += 万 ;                temp = WanStrToInt(x Substring(len ));                if (temp IndexOf( 千 ) == && temp != )                {                    ret += 零 + temp;                }                else                {                    ret += temp;                }            }            int i;            if ((i = ret IndexOf( 零万 )) != )            {                ret = ret Remove(i + );            }            while ((i = ret IndexOf( 零零 )) != )            {                ret = ret Remove(i );            }            if (ret[ret Length ] == 零 && ret Length > )            {                ret = ret Remove(ret Length );            }            return ret;        }        //转换小数        private string StrToDouble(string x)        {            string ret = ;            for (int i = ; i < x Length; i++)            {                ret += CharToNum(x[i]);            }            return ret;        }        private string NumToChn(string x)        {            if (x Length == )            {                return ;            }            string ret = ;            if (x[ ] == )            {                ret = 负 ;                x = x Remove( );            }            if (x[ ] ToString() == )            {                x = + x;            }            if (x[x Length ] ToString() == )            {                x = x Remove(x Length );            }            if (x IndexOf( ) > )            {                ret += StrToInt(x Substring( x IndexOf( ))) + 点 + StrToDouble(x Substring(x IndexOf( ) + ));            }            else            {                ret += StrToInt(x);            }            return ret;        }

  //金额转换       

  private string GetMoneyChinese(Double Money)        {            int i;            string mstrSource;

  if (Money == )            {                return ;            }            mstrSource = Money ToString( # );            i = mstrSource IndexOf( );            if (i > ) { mstrSource = mstrSource Replace( ); }            if (mstrSource Substring( ) == ) { mstrSource = mstrSource Remove( ); }

如何把阿拉伯数字变成大写 C#实现阿拉伯数字到大写中文的转换

  mstrSource = NumstrToChinese(mstrSource);            if (mstrSource Length == ) { return ; }            //负            if (Money < )            {                mstrSource = 负 + mstrSource;            }            mstrSource = mstrSource Replace( 零 );            mstrSource = mstrSource Replace( 壹 );            mstrSource = mstrSource Replace( 贰 );            mstrSource = mstrSource Replace( 三 );            mstrSource = mstrSource Replace( 肆 );            mstrSource = mstrSource Replace( 伍 );            mstrSource = mstrSource Replace( 陆 );            mstrSource = mstrSource Replace( 柒 );            mstrSource = mstrSource Replace( 捌 );            mstrSource = mstrSource Replace( 玖 );            mstrSource = mstrSource Replace( M 亿 );            mstrSource = mstrSource Replace( W 万 );            mstrSource = mstrSource Replace( S 仟 );            mstrSource = mstrSource Replace( H 佰 );            mstrSource = mstrSource Replace( T 拾 );            mstrSource = mstrSource Replace( Y 圆 );            mstrSource = mstrSource Replace( J 角 );            mstrSource = mstrSource Replace( F 分 );            if (mstrSource Substring(mstrSource Length ) != 分 )            {                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 <= numstr Length; i++)            {                j = numstr Length i;                mstrChar = numstr Substring(i );                if (mstrChar != && j > ) { mstrReturn = mstrReturn + mstrChar + mstrFlag[(j ) % ]; }                if (mstrChar == && mblnAddzero == false)                {                    mstrReturn = mstrReturn + ;                    mblnAddzero = true;                }                if (j == )                {                    if (mstrReturn Substring(mstrReturn Length ) == )                    { mstrReturn = mstrReturn Substring( mstrReturn Length ) + W ; }                    else                    { mstrReturn = mstrReturn + W ; }                }                if (j == )                {                    if (mstrReturn Substring(mstrReturn Length ) == )                    { mstrReturn = mstrReturn Substring( mstrReturn Length ) + Y ; }                    else                    { mstrReturn = mstrReturn + Y ; }                    //元                }                if (j == )                {                    if (mstrReturn Length > )                    {                        if (mstrReturn Substring(mstrReturn Length ) != M )                        {                            if (mstrReturn Substring(mstrReturn Length ) == )                            { mstrReturn = mstrReturn Substring( mstrReturn Length ) + W ; }                            else                            { mstrReturn = mstrReturn + W ; }                        }                    }                    else                    {                        if (mstrReturn Substring(mstrReturn Length ) == )                        { mstrReturn = mstrReturn Substring( mstrReturn Length ) + W ; }                        else                        { mstrReturn = mstrReturn + W ; }                    }                }                if (j == )                {                    if (mstrReturn Substring(mstrReturn Length ) == )                    { mstrReturn = mstrReturn Substring( mstrReturn Length ) + M ; }                    else                    { mstrReturn = mstrReturn + M ; }                }                if (j == && mstrChar != ) { mstrReturn = mstrReturn + mstrChar + F ; }                if (j == && mstrChar != ) { mstrReturn = mstrReturn + mstrChar + J ; }                if (mstrChar != ) { mblnAddzero = false; }            }

lishixinzhi/Article/program/net/201311/13523  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码