数字转换大写人民币的delphi实现

之前用Access作一个应用,但找不到货币转换成大写人民币的功能(当然可以通过excel 转换,但不在一个应用上,放弃)。以为比较简单,自己试写,谁知用了两个晚上,才基本实现,但没有小数点(元)以下货币的转换。也没有兆的判断。而且对字符的合法性也没有检验。今天有时间,将其完善一下。(可能还会有考虑不周的地方)

 思路:

1、判断是否带有小数点的金额,如果是,就找出小数点所在数据。该位字符不进行转换。

2、小数点前的作为整数位,进行转换。整数位的每一位都有表示金额的级别:拾、佰、仟。而且还有万、亿的级别。

3、小数点后的作为角、分、厘处理。

4、如果有连续的零,只显示一个零。如50006,显示为伍万零陆。如果是发生在拾、佰、仟位,该零还要去掉。

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
function TForm1.changeRmb(const strRmb:string):string;
var
  txt,strhighlevel:
string;
  i,n,m,ilen,ipos:Integer;    
//n记录整数部分长度,m记录分数部分长度
   strarray,strlevel:
array of string;
   p:pchar;
   ispoint:boolean;
//判断是否有小数点
begin
  ispoint:
=false;
  result:
='';
  ipos:
=0;
  m:
=0;
  txt:
=Trim(strRmb);
  i:
=1;
   p:
=PChar(txt);
  
//去除开头的0,以及.
 
if ((txt[1]='0'and (txt[2]<>'.')) or (txt[1]='.'then
  
begin
   ShowMessage(
'第1位不能为0或者是.,退出操作');
   exit;
  
end;
  
//检查字符的合法性
  
while (i<length(txt))do
    
begin
      
if (p^>'9'or ((p^<'0'and (P^<>'.'))  then  //ord('.')=46
        
begin
          ShowMessage(PChar(
''+inttostr(i)+'位包含非数字字符,将退出操作'));
          Exit;
        
end;
      
if P^='.' then
       
if ispoint then
        
begin
         showmessage(
'太多小数点,将退出!');
         exit;
        
end
       
else
       
begin
        ipos:
=i;
        ispoint:
=true;
       
end;
      Inc(p);
      Inc(i);
    
end;//while
   ilen:
=Length(txt);
   
if ispoint then
    
begin
     n:
=ipos-1;
     m:
=ilen-ipos;
    
end
   
else
    n:
=ilen;
  
//判断是否超过万,或亿
   
if m>3 then
     
begin
       ShowMessage(
'小数点后位数超过3,无法转换!');
       Exit;
     
end;
  SetLength(strarray,ilen
+8);
  SetLength(strlevel,ilen
+8);

 
for i:=iLen downto 1 do
   
begin
    
if txt[i]<>'.' then
     
case strtoint(txt[i]) of
       
1:strarray[i]:='';
       
2:strarray[i]:='';
       
3:strarray[i]:='';
       
4:strarray[i]:='';
       
5:strarray[i]:='';
       
6:strarray[i]:='';
       
7:strarray[i]:='';
       
8:strarray[i]:='';
       
9:strarray[i]:='';
       
0:
       
begin
         strarray[i]:
='';
         
if i<ilen then   //如果低位也为零,低位零不显示
          
if (strarray[i+1]= ''or (strarray[i+1]= ''then
            
begin
              
//strarray[i+1]:= '';
              strarray[i]:
= '';
            
end;
         
if i=then strarray[i]:='';
         strlevel[i]:
='';
       
end;
     
end//case
  
end;
//先处理 小数点部分

  
if m>0 then
  
begin
    
for i:=downto 1 do
      
begin
        strlevel[ipos
+i]:='';
        
case i-1 of
          
0:
             
if  txt[ipos+i]='0' then
                strarray[ipos
+i]:=''
             
else
               strlevel[ipos
+i]:='';
          
1:
             
if  txt[ipos+i]='0' then
                  strarray[ipos
+i]:=''
             
else
               strlevel[ipos
+i]:='';
          
2:
             
if  txt[ipos+i]='0' then
               strarray[ipos
+i]:=''
              
else   strlevel[ipos+i]:='';
        
end;
       Result:
=strarray[ipos+i]+strlevel[ipos+i]+result;
      
end;
  
end;
  
if ispoint and (txt[ipos-1]='0'and (n=1then
    Result:
=result+''   //如果少于1块时,不要显示元。
  
else
    Result:
=''+result;

   
for i:=downto 1 do
   
begin

      
case n-of
      
0,4,8,12: strlevel[i]:='';
      
1,5,9,13: strlevel[i]:='';
      
2,6,10,14: strlevel[i]:='';
      
3,7,11,15: strlevel[i]:='';
     
end//case
   
if (txt[i]='0')  then strlevel[i]:='';
 
//要处理零 以及加上万、亿
    
if n-i=4 then
     
begin
      
if  strarray[i]='' then   strarray[i]:='';
      Result:
=strarray[i]+strlevel[i]+''+result
     
end
     
else if n-i=8 then
     
begin
      
if  strarray[i]='' then   strarray[i]:='';
      Result:
=strarray[i]+strlevel[i]+'亿'+result
     
end //begin
     
else if n-i=12 then
     
begin
      
if  strarray[i]='' then   strarray[i]:='';
      Result:
=strarray[i]+strlevel[i]+''+result
     
end //begin
    
else
    Result:
=strarray[i]+strlevel[i]+result;
 
end//for
end;

转载于:https://www.cnblogs.com/samsonleung/archive/2008/09/26/1299737.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值