| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1462 人关注过本帖
标题:自己写的,C#版,把数字转换为人民币大写
只看楼主 加入收藏
zhukuntang
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-10-25
收藏
 问题点数:0 回复次数:7 
自己写的,C#版,把数字转换为人民币大写
public static string GetNumber2CN(double number)
    {
        StringBuilder cnStr = new StringBuilder();
        string CnNumber = "零壹贰叁肆伍陆柒捌玖";
        string CnUnit = "分角元十百千万十百千亿十百千万";
        string numStr = number.ToString("#0.00").Replace(".", "");
        for (int i = 0; i < numStr.Length; i++)
        {
            int num = int.Parse(numStr.Substring(i, 1));
            cnStr.Append(CnNumber.Substring(num, 1) + CnUnit.Substring(numStr.Length - i - 1, 1));
        }
        while (Regex.IsMatch(cnStr.ToString(), "零零|零十|零百|零千|零万|零亿|亿万"))
        {
            cnStr = cnStr.Replace("零十", "零");
            cnStr = cnStr.Replace("零百", "零");
            cnStr = cnStr.Replace("零千", "零");
            cnStr = cnStr.Replace("零万", "万");
            cnStr = cnStr.Replace("零亿", "亿");
            cnStr = cnStr.Replace("零零", "零");
            cnStr = cnStr.Replace("亿万", "亿零");
        }
        cnStr = cnStr.Replace("零元", "元").Replace("零角", "").Replace("零分", "");
        return cnStr.ToString().StartsWith("元") ? cnStr.ToString().Substring(1) : cnStr.ToString();
    }

如果哪位能完善的更好,贴出来分享一下

呵呵,又编辑了以下标题,方便大家查询,下面也跟了完善版

[[it] 本帖最后由 zhukuntang 于 2008-11-1 10:50 编辑 [/it]]
收到的鲜花
  • 小仙2008-10-25 18:05 送鲜花  49朵   附言:感谢分享。
搜索更多相关主题的帖子: 数字 中文 
2008-10-25 12:42
star_520
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2008-10-11
收藏
得分:0 
以前用delphi做过一个。感觉要比这个复杂多了...

人,一定要靠自己...
2008-10-25 12:52
zhukuntang
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-10-25
收藏
得分:0 
稍作修改了一下
public static string GetNumber2CN(double number)
    {
        StringBuilder cnStr = new StringBuilder();
        string CnNumber = "零壹贰叁肆伍陆柒捌玖";
        string CnUnit = "分角元拾佰仟万拾佰仟亿拾佰仟万";
        string numStr = number.ToString("#0.00").Replace(".", "");
        for (int i = 0; i < numStr.Length; i++)
        {
            int num = int.Parse(numStr.Substring(i, 1));
            cnStr.Append(CnNumber.Substring(num, 1) + CnUnit.Substring(numStr.Length - i - 1, 1));
        }
        while (Regex.IsMatch(cnStr.ToString(), "零零|零拾|零佰|零仟|零万|零亿|亿万"))
        {
            cnStr = cnStr.Replace("零拾", "零");
            cnStr = cnStr.Replace("零佰", "零");
            cnStr = cnStr.Replace("零仟", "零");
            cnStr = cnStr.Replace("零万", "万");
            cnStr = cnStr.Replace("零亿", "亿");
            cnStr = cnStr.Replace("零零", "零");
            cnStr = cnStr.Replace("亿万", "亿零");
        }
        cnStr = cnStr.Replace("零元", "元").Replace("零角", "").Replace("零分", "");
        if (cnStr.ToString().StartsWith("元"))
        {
            return cnStr.ToString().Substring(1);
        }
        if (cnStr.ToString().EndsWith("元"))
        {
            return cnStr.ToString() + "整";
        }
        return cnStr.ToString();
    }
2008-11-01 10:45
飙马
Rank: 5Rank: 5
来 自:马里亚纳
等 级:贵宾
威 望:15
帖 子:779
专家分:280
注 册:2007-3-28
收藏
得分:0 
好东西,支持!
前几天弄了一个别人写的时间转数字时钟的程序,也是非常不错!

IT精英如同彩票:平凡的人像5块也中不到一样普遍,努力一点你中了5元保了个本。奖金越高,机率也就越小,付出的也越多,盖茨如同500万一样稀有。虽然每天忙碌而平凡,但我努力成为精英,做梦中了500万。
2008-11-01 10:56
huangwei89
Rank: 1
等 级:新手上路
帖 子:127
专家分:0
注 册:2008-10-17
收藏
得分:0 
顶一下
2008-11-05 21:18
meihonganddulon
Rank: 1
来 自:河南郑州
等 级:新手上路
帖 子:54
专家分:4
注 册:2008-7-18
收藏
得分:0 
个人感觉算法太复杂
还可以优化
2008-11-06 08:43
meihonganddulon
Rank: 1
来 自:河南郑州
等 级:新手上路
帖 子:54
专家分:4
注 册:2008-7-18
收藏
得分:0 
string CnNumber = "零壹贰叁肆伍陆柒捌玖";
string CnUnit = "分角元十百千万十百千亿十百千万";
用二维数组存储
2008-11-06 08:45
zhb1875
Rank: 2
等 级:论坛游民
威 望:1
帖 子:25
专家分:72
注 册:2009-10-22
收藏
得分:0 
这是我写的一个方法.数字转成大写金额.下面的方法是大写金额转成数字
public string SWAP(string mone)//
        {
            
            hc1[0]="零";hc2[0]="分";
            hc1[1]="壹";hc2[1]="角";
            hc1[2]="贰";hc2[2]="元";
            hc1[3]="叁";hc2[3]="拾";
            hc1[4]="肆";hc2[4]="佰";
            hc1[5]="伍";hc2[5]="仟";
            hc1[6]="陆";hc2[6]="万";
            hc1[7]="柒";hc2[7]="拾";
            hc1[8]="捌";hc2[8]="佰";
            hc1[9]="玖";hc2[9]="仟";
            hc2[10]="亿";
            
            double f;
            string fuction="";
        
                if(mone!="")
                {
                    if(double.Parse(mone)>=0)
                        f=double.Parse(mone)*10000;//金额的10000倍
                    else
                    {
                        f=-double.Parse(mone)*10000;
                        fuction=" 负 ";
                    }
                    ulong invalue=ulong.Parse(f.ToString())/100;//金额的10000倍/100
                    string Value="";
                    ulong n=10;
                    int cal=0;
                    while(invalue>0)
                    {
                        ulong m=invalue%n;
                        invalue=invalue/10;
                        if(cal>10)
                            cal=cal-8;
                        Value=hc1[int.Parse(m.ToString())].ToString()+hc2[int.Parse(cal.ToString())].ToString()+Value;
                        cal++;
                    }
                    MONEY = "¥"+Value+"整";
                }
            
                else
                {
                    MessageBox.Show("请输入转换金额!");
                }
            
            return MONEY;
}
大写金额转换成数字:
public string MOVE(string str)
        {
            
           b[0]="零";b[11]="分";
           b[1]="壹";b[12]="角";
           b[2]="贰";b[13]="元";
           b[3]="叁";b[14]="拾";
           b[4]="肆";b[15]="佰";
           b[5]="伍";b[16]="仟";
           b[6]="陆";b[17]="万";
           b[7]="柒";b[18]="拾";
           b[8]="捌";b[19]="佰";
           b[9]="玖";b[20]="仟";
           b[10]="亿";
           for(int i=0;i<=str.Length-2;i++)
               a[i]=str.Substring(i+1,1);
           //GOTO:
           for(int j=0;j<=str.Length-2;j++)
               if(a[j]=="亿"||a[j]=="仟"||a[j]=="佰"||a[j]=="拾"||a[j]=="万"||a[j]=="佰"||a[j]=="拾"||a[j]=="元"||a[j]=="角"||a[j]=="分")
               {   
                  
                   if(a[j]=="元")
                   {
                      str_swap=str_swap+".";
                   }
                       a[j]="0";
                   continue;
               }
               else
               {
                  str_swap = str_swap+SWAP_(a[j]);
               }
           return str_swap;
      }
       public string SWAP_(string str_)
        {
            string strA="";
            switch(str_)
            {
               case "零":strA="0";break;
                case "壹":strA="1";break;
               case "贰":strA="2";break;
                case "叁":strA="3";break;
                case "肆":strA="4";break;
                case "伍":strA="5";break;
               case "陆":strA="6";break;
                case "柒":strA="7";break;
                case "捌":strA="8";break;
               case "玖":strA="9";break;
            }
           return strA;
        }



赠送一个方法.哈哈!!!将日期转换成大写......(其实会了金额日期也就会了)


public string Conver(string str)
        {
            string strA="";
            if(str.Length==4)
            {
                strA=setA(str.Substring(0,1))+setA(str.Substring(1,1))+setA(str.Substring(2,1))+setA(str.Substring(3,1));
            }
            else
            {
                if(str.Length==1)
                    str="0"+str;
                if(int.Parse(str)>=10)
                {
                    if(int.Parse(str)==10||int.Parse(str)==20||int.Parse(str)==30)
                        strA="零"+setA(str.Substring(0,1))+"拾";
                    else
                        strA=setA(str.Substring(0,1))+"拾"+setA(str.Substring(1,1));
                }
                else
                    strA=setA(str.Substring(0,1))+setA(str.Substring(1,1));
            }
            return strA;
        }
        public string setA(string str)
        {
            string strA="";
            switch(str)
            {
                case "0":strA="零";break;
                case "1":strA="壹";break;
                case "2":strA="贰";break;
                case "3":strA="参";break;
                case "4":strA="肆";break;
                case "5":strA="伍";break;
                case "6":strA="陆";break;
                case "7":strA="柒";break;
                case "8":strA="捌";break;
                case "9":strA="玖";break;
            }
            return strA;
        }
2010-08-07 15:31
快速回复:自己写的,C#版,把数字转换为人民币大写
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017946 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved