用Delphi编制金额大写换程序
点击次数:57 次 发布日期:2008-11-09 08:43:14 作者:源代码网
|
1.定义函数num—str function num—str(ls: Variant): String; var dx—sz,dx—dw,str—int,str—dec,dx—str,fu:string; a,b,b2,c,d:string; num—int,num—dec,len—int,i,a—int,pp:integer; //dx—str为返回字符串 begin dx—sz:=′零壹贰叁肆伍陆柒捌玖′; dx—dw:=′万仟佰拾亿仟佰拾万仟佰拾元′; //处理金额小于零情况 if ls〈0 then begin ls:=ls(-1); fu:=′负′; end else fu:=′′; //取得整数值及整数串 dx—str:=ls; if (ls〉0)and(ls〈1) then dx—str:=′0′+dx—str; pp:=pos(′.′,dx—str); if pp〉0 then str—int:=copy(dx—str,1,pos(′.′,dx—str)-1) else str—int:=dx—str; num—int:=strtoint(str—int); //取得小数值及小数串 if (ls〉0)and(ls〈1) then num—dec:=ls100 else num—dec:=(ls-num—int)100; str—dec:=inttostr(num—dec); len—int:=Length(str—int); dx—str:=′′; //转换整数部分 for i:=1 to len—int do begin //a为小写数字字符,b为对应的大写字符 //c为对应大写单位,d为当前大写字符串的最后一个汉字 软件开发网 www.mscto.com a:=copy(str—int,i,1); a—int:=strtoint(a); b:=copy(dx—sz,(a—int2+1),2); c:=copy(dx—dw,((13-len—int+i-1)2+1),2); if dx—str〈〉′′ then d:=copy(dx—str,Length(dx—str)-1,2) else d:=′′; if (b=′零′)and((d=′零′)or(b=b2)or(c=′元′)or(c=′万′)or(c=′亿′)) then b:=′′; if (a=′0′)and(c〈〉′元′)and(c〈〉万′′)and(c〈〉′亿′) then c:=′′; if ((c=′元′)or(c=′万′)or(c=′亿′))and (d=′零′)and(a=′0′) then begin dx—str:=copy(dx—str,1,Length(dx—str)-2); d:=copy(dx—str,Length(dx—str)-1,2); if ((c=′元′)and(d=′万′))or((c=′万′)and(d=′亿′)) then c:=′′; end; dx—str:=dx—str+b+c; b2:=b; end; //处理金额小于1的情况 if Length(dx—str)〈=2 then dx—str:=′′; //转换小数部分 if (num—dec〈10)and(ls〉0) then begin a—int:=strtoint(str—dec); b:=copy(dx—sz,(a—int2+1),2); if num—dec=0 then dx—str:=dx—str+′整′; if num—dec〉0 then dx—str:=dx—str+′零′+b+′分′; end; if num—dec〉=10 then begin a—int:=strtoint(copy(str—dec,1,1)); a:=copy(dx—sz,(a—int2+1),2); a—int:=strtoint(copy(str—dec,2,1)); b:=copy(dx—sz,(a—int2+1),2); if a〈〉′零′ then a:=a+′角′; if b〈〉′零′ then b:=b+′分′ else b:=′′; dx—str:=dx—str+a+b; end; if ls=0 then dx—str:=′零元整′; dx—str:=fu+dx—str; //函数返回字符串 Result:=dx—str; end; 2.调用方法 函数参数采用Variant类型,调用时参数值可以是实型,也可以是字符串,非常方便,下面举例说明: 新建窗口,在相应pas文件中加入num—str函数,并在窗口中添加Button1和Edit1、Edit2控件,双击Button1输入以下代码,运行程序即可: procedure TForm1.Button1Click(Sender: TObject); begin Edit1.text:=num—str(202055010.32); //也可以在Edit2中输入数串调用 //Edit1.text:=num—str(Edit2.text); end; 源代码网推荐 源代码网供稿. |
