看了mantousoft提供的代码,想起了几年前与人打赌时写的人民币金额大小写转换程序,于是就翻了出来.
//作者 ZLB(zlb_nj@sina.com) //对不起,代码有点乱.
function TForm1.F2C(r: real): string; var tmp1,rr :string; l,i,j,k:integer; const n1:array[0..9] of string=('零','壹','贰','叁','肆','伍','陆','柒','捌','玖'); const n2:array[0..3] of string=(','拾','佰','仟'); const n3:array[0..2] of string=('元','万','亿'); begin tmp1:=FormatFloat('#.00',r); l:=length(tmp1); rr:='; if strtoint(tmp1[l])<>0 then begin rr:='分'; rr:=n1[strtoint(tmp1[l])]+rr; end; if strtoint(tmp1[l-1])<>0 then begin rr:='角'+rr; rr:=n1[strtoint(tmp1[l-1])]+rr; end;
i:=l-3; j:=0;k:=0; while i>0 do begin if j mod 4=0 then begin rr:=n3[k]+rr; inc(k);if k>2 then k:=1; j:=0; end; if strtoint(tmp1[i])<>0 then rr:=n2[j]+rr; rr:=n1[strtoint(tmp1[i])]+rr; inc(j); dec(i); end; while pos('零零',rr)>0 do rr:= stringreplace(rr,'零零','零',[rfReplaceAll]); rr:=stringreplace(rr,'零亿','亿零',[rfReplaceAll]); while pos('零零',rr)>0 do rr:= stringreplace(rr,'零零','零',[rfReplaceAll]); rr:=stringreplace(rr,'零万','万零',[rfReplaceAll]); while pos('零零',rr)>0 do rr:= stringreplace(rr,'零零','零',[rfReplaceAll]); rr:=stringreplace(rr,'零元','元零',[rfReplaceAll]); while pos('零零',rr)>0 do rr:= stringreplace(rr,'零零','零',[rfReplaceAll]); rr:=stringreplace(rr,'亿万','亿',[rfReplaceAll]); if copy(rr,length(rr)-1,2)='零' then rr:=copy(rr,1,length(rr)-2); result:=rr; end; |