阿拉伯数字转中文的C#源程序

 

项目需要将阿拉伯数字转为中文序号,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

转载于:https://www.cnblogs.com/DreamlikeAttic/archive/2009/08/06/1540066.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值