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

文章来源:IT工程信息网  http://www.systhinker.com/html/09/n-12309.html

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

思路:

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

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

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

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

 

000function TForm1.changeRmb(const strRmb:string):string
001var 
002  txt,strhighlevel:string
003  i,n,m,ilen,ipos:Integer;    //n记录整数部分长度,m记录分数部分长度 
004   strarray,strlevel:array of string
005   p:pchar
006   ispoint:boolean;//判断是否有小数点 
007begin 
008  ispoint:=false
009  result:=''
010  ipos:=0
011  m:=0
012  txt:=Trim(strRmb); 
013  i:=1
014   p:=PChar(txt); 
015//去除开头的0,以及. 
016if ((txt[1]='0') and (txt[2]<>'.')) or (txt[1]='.') then 
017begin 
018   ShowMessage('第1位不能为0或者是.,退出操作'); 
019   exit; 
020end
021//检查字符的合法性 
022while (i<length(txt))do 
023begin 
024if (p^>'9') or ((p^<'0') and (P^<>'.'))  then //ord('.')=46 
025begin 
026          ShowMessage(PChar('第'+inttostr(i)+'位包含非数字字符,将退出操作')); 
027          Exit; 
028end
029if P^='.' then 
030if ispoint then 
031begin 
032         showmessage('太多小数点,将退出!'); 
033         exit; 
034end 
035else 
036begin 
037        ipos:=i; 
038        ispoint:=true
039end
040      Inc(p); 
041      Inc(i); 
042end;//while 
043   ilen:=Length(txt); 
044if ispoint then 
045begin 
046     n:=ipos-1
047     m:=ilen-ipos; 
048end 
049else 
050    n:=ilen; 
051//判断是否超过万,或亿 
052if m>3 then 
053begin 
054       ShowMessage('小数点后位数超过3,无法转换!'); 
055       Exit; 
056end
057  SetLength(strarray,ilen+8); 
058  SetLength(strlevel,ilen+8); 
059for i:=iLen downto 1 do 
060begin 
061if txt[i]<>'.' then 
062case strtoint(txt[i]) of 
0631:strarray[i]:='壹'
0642:strarray[i]:='贰'
0653:strarray[i]:='叁'
0664:strarray[i]:='肆'
0675:strarray[i]:='伍'
0686:strarray[i]:='陆'
0697:strarray[i]:='柒'
0708:strarray[i]:='捌'
0719:strarray[i]:='玖'
0720
073begin 
074         strarray[i]:='零'
075if i<ilen then //如果低位也为零,低位零不显示 
076if (strarray[i+1]= '') or (strarray[i+1]= '零') then 
077begin 
078//strarray[i+1]:= ''; 
079              strarray[i]:= ''
080end
081if i=n then strarray[i]:=''
082         strlevel[i]:=''
083end
084end; //case 
085end
086//先处理 小数点部分 
087if m>0 then 
088begin 
089for i:=m downto 1 do 
090begin 
091        strlevel[ipos+i]:=''
092case i-1 of 
0930
094if  txt[ipos+i]='0' then 
095                strarray[ipos+i]:='' 
096else 
097               strlevel[ipos+i]:='角'
0981
099if  txt[ipos+i]='0' then 
100                  strarray[ipos+i]:='' 
101else 
102               strlevel[ipos+i]:='分'
1032
104if  txt[ipos+i]='0' then 
105               strarray[ipos+i]:='' 
106else   strlevel[ipos+i]:='厘'
107end
108       Result:=strarray[ipos+i]+strlevel[ipos+i]+result; 
109end
110end
111if ispoint and (txt[ipos-1]='0') and (n=1) then 
112    Result:=result+'' //如果少于1块时,不要显示元。 
113else 
114    Result:='元'+result; 
115for i:=n downto 1 do 
116begin 
117case n-i of 
1180,4,8,12: strlevel[i]:=''
1191,5,9,13: strlevel[i]:='拾'
1202,6,10,14: strlevel[i]:='佰'
1213,7,11,15: strlevel[i]:='仟'
122end; //case 
123if (txt[i]='0'then strlevel[i]:=''
124//要处理零 以及加上万、亿 
125if n-i=4 then 
126begin 
127if  strarray[i]='零' then   strarray[i]:=''
128      Result:=strarray[i]+strlevel[i]+'万'+result 
129end 
130else if n-i=8 then 
131begin 
132if  strarray[i]='零' then   strarray[i]:=''
133      Result:=strarray[i]+strlevel[i]+'亿'+result 
134end //begin 
135else if n-i=12 then 
136begin 
137if  strarray[i]='零' then   strarray[i]:=''
138      Result:=strarray[i]+strlevel[i]+'兆'+result 
139end //begin 
140else 
141    Result:=strarray[i]+strlevel[i]+result; 
142end; //for 
143end;
文章来源:IT工程信息网  http://www.systhinker.com/html/09/n-12309.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值