(C#)数值型货币的大写转换
2024-07-21 02:18:39
供稿:网友
本人现在做的项目要求对数值型货币,转换成大写,在网上搜索了半天,没有找到c#写的类型,不得不自己写了,测试了,还能满足要求,只是算法有点繁琐,有哪位大虾再给改改!
下面就是我得代码:
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
namespace winmarket
{
/// <summary>
/// classfun 的摘要说明。
/// </summary>
public class classfun
{
private string capstr;
public classfun()
{
//
// todo: 在此处添加构造函数逻辑
//
}
public string moneynumtocap(decimal num)
{
string cap="零壹贰叁肆伍陆柒捌玖";
string numstr="0123456789";
string moneynumstr=num.tostring();
int pint=moneynumstr.indexof(".");
int numint;
string moneyint=null;
string moneydec=null;
string intstr=null;
string moneycap=null;
string moneyintstr=null;
string moneydecstr=null;
// capstr=pint.tostring();
if(pint!=-1)
{
string strarr=".";
char[] chararr=strarr.tochararray();
string[] moneynumarr=moneynumstr.split(chararr);
moneyint=moneynumarr[0].tostring();
moneydec=moneynumarr[1].tostring();
}
else
{
moneyint=moneynumstr;
moneydec="00";
}
if(moneyint.length>16)
{
messagebox.show("数值超界");
}
else
{
//--- 处理整数部分--------
for(int j=1;j<=moneyint.length;j++)
{
moneyintstr=moneyint.substring(j-1,1);
for(int i=0;i<=9;i++)
{
intstr=numstr.substring(i,1);
moneycap=cap.substring(i,1);
if(moneyintstr==intstr)
{
switch (intstr)
{
case "0":
capstr=capstr+moneycap;
break;
case "1":
capstr=capstr+moneycap;
break;
case "2":
capstr=capstr+moneycap;
break;
case "3":
capstr=capstr+moneycap;
break;
case "4":
capstr=capstr+moneycap;
break;
case "5":
capstr=capstr+moneycap;
break;
case "6":
capstr=capstr+moneycap;
break;
case "7":
capstr=capstr+moneycap;
break;
case "8":
capstr=capstr+moneycap;
break;
case "9":
capstr=capstr+moneycap;
break;
}
}
}
numint=moneyint.length-j+1;
switch (numint)
{
case 16:
capstr=capstr+"仟万";
break;
case 15:
capstr=capstr+"佰万";
break;
case 14:
capstr=capstr+"拾万";
break;
case 13:
capstr=capstr+"万";
break;
case 12:
capstr=capstr+"仟";
break;
case 11:
capstr=capstr+"佰";
break;
case 10:
capstr=capstr+"拾";
break;
case 9:
capstr=capstr+"亿";
break;
case 8:
capstr=capstr+"仟";
break;
case 7:
capstr=capstr+"佰";
break;
case 6:
capstr=capstr+"拾";
break;
case 5:
capstr=capstr+"万";
break;
case 4:
capstr=capstr+"仟";
break;
case 3:
capstr=capstr+"佰";
break;
case 2:
capstr=capstr+"拾";
break;
case 1:
capstr=capstr+"元";
break;
}
}
//------处理小数部分-----
for(int j=1; j<=2; j++)
{
moneydecstr=moneydec.substring(j-1,1);
for(int i=0;i<=9;i++)
{
intstr=numstr.substring(i,1);
moneycap=cap.substring(i,1);
if(moneydecstr==intstr)
{
switch (intstr)
{
case "0":
capstr=capstr+moneycap;
break;
case "1":
capstr=capstr+moneycap;
break;
case "2":
capstr=capstr+moneycap;
break;
case "3":
capstr=capstr+moneycap;
break;
case "4":
capstr=capstr+moneycap;
break;
case "5":
capstr=capstr+moneycap;
break;
case "6":
capstr=capstr+moneycap;
break;
case "7":
capstr=capstr+moneycap;
break;
case "8":
capstr=capstr+moneycap;
break;
case "9":
capstr=capstr+moneycap;
break;
}
}
}
switch(j)
{
case 1:
capstr=capstr+"角";
break;
case 2:
capstr=capstr+"分";
break;
}
}
}
return capstr;
}
}
}