Rmb.cs
资源名称:Visual.rar [点击查看]
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:5k
源码类别:
C#编程
开发平台:
Others
- using System;
- class RenminbiUper
- {
- static string[] UperMoney={"","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
- static string[] Digit={"","","拾","佰","千"};
- static string[] Ling={"","零"};
- static string[] Wan={"","万"};
- static string[] Yi={"","亿"};
- public static void toUper(System.Decimal num)
- {
- //将传入的num转换为char数组
- char[] moneyNumber;
- //如果num是小于1的 比如0.78 经过num.ToString(".00")
- //转化后会变成.78 而前面的0会消失掉
- //所以要在前面补0
- if(num<1)
- {moneyNumber=("0"+num.ToString(".00")).ToCharArray(); }
- else
- //如果num是大于等于1的 则不存在以上的问题
- //转化时将会四舍五入 比如输入的是12.078 转化后会变成12.08
- moneyNumber=(num.ToString(".00")).ToCharArray();
- Console.WriteLine(moneyNumber);
- int length=moneyNumber.Length;
- int curDigit; //当前的位数
- int curValue; //当前位数的值
- int perValue=1; //前一位的值(用于判断前一位是否为零)
- int indexOfDigit; //确定数字的后缀(拾、佰、千)在数组中的下标
- //根据前一位的数是否为零来决定是否显示"零"(数组Ling的下标)
- int perValueIsZero;
- //决定是否显示"万"(数组Wan的下标)
- int isWan=0;
- //决定是否显示"亿"(数组Yi的下标)
- int isYi=0;
- string Yuan=""; //输出字符串
- bool flag=false;
- //如果moneyNumber[0]不等于零则说明moneyNumber是一个大于一的数 要考虑
- //整数部分和小数部分
- //否则说明它小于一 那就仅考虑小数部分就可以了
- if(moneyNumber[0]!='0')
- {
- // moneyNumber = "0"+ moneyNumber;
- // Console.WriteLine(moneyNumber);
- //逐字操纵数的整数部分
- for(int i=0;i<length-3;i++)
- {
- perValueIsZero=0;
- //确定当前数字所在的位数(只考虑整数部分)
- curDigit=length-3-i;
- if(curDigit<9)
- isYi=0;
- if(curDigit<5||(curDigit<13&&curDigit>8))
- {
- flag=false;
- isWan=0;
- }
- if(i>0&&perValue==0)
- perValueIsZero=1;
- if(curDigit<5)
- indexOfDigit=curDigit%5;
- else if(curDigit<9)
- {
- indexOfDigit=(curDigit-4)%5;
- }
- else if(curDigit<=13)
- indexOfDigit=(curDigit-8)%5;
- else
- indexOfDigit=(curDigit-12)%5;
- curValue=Convert.ToInt32(moneyNumber[i].ToString());
- if((curDigit>=5&&curDigit<9||curDigit>12)&&flag==false)
- {
- flag=curValue==0&&length>11?false:true;
- }
- if(curDigit==5||curDigit==13||curDigit==9)
- {
- if(flag)
- isWan=1;
- if(curValue==0)
- perValueIsZero=0;
- }
- if(curDigit==9)
- isYi=1;
- perValue=curValue;
- if( curValue!=0||(curValue==0&&(curDigit==5||curDigit==13||curDigit==9)) )
- {
- Yuan+=Ling[perValueIsZero]+UperMoney[curValue]+Digit[indexOfDigit]+Wan[isWan]+Yi[isYi];
- }
- }
- Yuan+="元";
- //考虑数的小数部分
- if(moneyNumber[length-1]=='0'&&moneyNumber[length-2]=='0')
- Yuan+="整";
- else
- {
- if(moneyNumber[length-4]=='0')
- Yuan+="零";
- if(moneyNumber[length-1]=='0')
- Yuan+=UperMoney[Convert.ToInt32(moneyNumber[length-2].ToString())]+"角";
- else if(moneyNumber[length-2]=='0')
- {
- if(moneyNumber[length-4]!='0')
- Yuan+="零";
- Yuan+=UperMoney[Convert.ToInt32(moneyNumber[length-1].ToString())]+"分";
- }
- else
- Yuan+=UperMoney[Convert.ToInt32(moneyNumber[length-2].ToString())]+"角"+UperMoney[Convert.ToInt32(moneyNumber[length-1].ToString())]+"分";
- }
- }
- else
- {
- if(moneyNumber[length-4]=='0')
- Yuan+="";
- if(moneyNumber[length-1]=='0')
- Yuan+=UperMoney[Convert.ToInt32(moneyNumber[length-2].ToString())]+"角";
- else if(moneyNumber[length-2]=='0')
- {
- if(moneyNumber[length-4]!='0')
- Yuan+="";
- Yuan+=UperMoney[Convert.ToInt32(moneyNumber[length-1].ToString())]+"分";
- }
- else
- Yuan+=UperMoney[Convert.ToInt32(moneyNumber[length-2].ToString())]+"角"+UperMoney[Convert.ToInt32(moneyNumber[length-1].ToString())]+"分";
- }
- Console.WriteLine(Yuan);
- }
- [STAThread]
- static void Main(string[] args)
- {
- //写入要转换的数字, 表示decimal数据类型
- System.Decimal d=0;
- bool flag=true;
- bool error=false;
- do
- {
- if(error)
- System.Console.WriteLine("输入格式不对,请重新输入!");
- Console.Write("请输入转换数(16位整数,0退出):");
- try
- {
- d=Convert.ToDecimal(Console.ReadLine());
- Console.WriteLine(d);
- /*
- if(d==0)
- break;
- if(d.ToString(".00").Length>19)
- {
- error=true;
- continue;
- }
- */
- toUper(d);
- error=false;
- break;
- }
- catch
- {
- error=true;
- }
- }while(flag);
- Console.ReadLine();
- }
- }