迷梦小筑—让灵魂在代码中无限升华
我幸运,并不是所有的猫都可以拥有大脑!
posts - 6,  comments - 26,  trackbacks - 0
 

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

posted on 2009-08-06 09:24 拍拍猫脑 阅读(190) 评论(2) 编辑 收藏

FeedBack:
2009-08-06 16:43 | 阿Dong→沁鉫      
string str="零一二三四五六七八九";
string str1="十百千万十百千亿十百千万";
string num="1234567890";
string cn="";
for(int i=num.Length-1;i>-1;i--)
{
cn=str.Substring(int.Parse(num.Substring(i,1)),1)+cn;
cn=str1.Substring(num.Length-i-1,1)+cn;
}
return cn.Remove(0,1).Trim('零');

你觉得我这个思路 可以吗

 回复 引用 查看   
2009-11-20 20:19 | 双击2[未注册用户]
引用阿Dong→沁鉫:
string str="零一二三四五六七八九";
string str1="十百千万十百千亿十百千万";
string num="1234567890";
string cn="";
for(int i=num.Length-1;i>-1;i--)
{
cn=str.Substring(int.Parse(num.Substring(i,1)),1)+cn;
cn=str1.Substring(num.Length-i-1,1)+cn;
}
return cn.Remove(0,1).Trim('零');

你觉得我这个思路 可以吗


不错

 回复 引用   

我与猫、计算机一起生活在神秘的程序世界,我的爱好是利用猫脑开发智能移动设备...

联系方式:
email:missilecat@163.com
QQ:85403578

昵称:拍拍猫脑
园龄:5年8个月
粉丝:0
关注:0

<2009年8月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

搜索

 
 

常用链接

随笔分类

随笔档案

文章分类

最新评论

阅读排行榜

评论排行榜

推荐排行榜