用Delphi编制金额大写转换程序

 在从事与财务相关的软件开发过程中,通常要求将小写金额转换成相应的大写金额,并打印在大写金额栏中。下面是用Delphi3.0编制的一个转换函数,能够方便的在程序中调用,并返回字符串。

  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:=ls100

  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为当前大写字符串的最后一个汉字

   a:=copy(str—int,i,1);

   a—int:=strtoint(a);

   b:=copy(dx—sz,(a—int2+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—int2+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—int2+1),2);

  a—int:=strtoint(copy(str—dec,2,1));

  b:=copy(dx—sz,(a—int2+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;

  摘自《赛迪网》 杨波/文

转载于:https://www.cnblogs.com/wenwencao/archive/2008/12/29/1364428.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值