小写金额转换为大写 //本函数用于将小于十万亿元的小写金额转换为大写 Function NtoC( n0 :real) :String; Function IIF( b :boolean; s1,s2 :string) :string; begin if b then IIF:= s1 else IIF:=s2; end; //本函数的功能一目了然: 当b为真时返回s1,否则返回s2 Const c= ’零壹贰叁肆伍陆柒捌玖◇分角圆拾佰仟万拾佰仟亿拾佰仟万’; var L,i,n :integer; Z :boolean; s,s1,s2 :string; begin s:= FormatFloat( ’0.00’, ABS(n0)); L:= Length( s); Z:= n0<1; For i:= 1 To L-3 do begin n:= StrToInt( s[ L-i-2]); s1:=IIf((n=0)And(Z Or (i=9) Or (i=5) Or (i=1)), ’’, Copy( c, n*2+1, 2)) + IIf((n=0)And((i<>9)And(i<>5)And(i<>1) Or Z And(i=1)),’’,Copy(c,(i+13)*2-1,2)) + s1; Z:= (n=0); end; Z:= False; For i:= 1 To 2 do begin n:= StrToInt( s[ L-i+1]); s2:= IIf((n=0)And((i=1) Or (i=2)And(Z Or (n0<1))), ’’, Copy( c, n*2+1, 2)) + IIf( (n>0), Copy( c,(i+11)*2-1, 2), IIf( (i=2) Or Z, ’’,’整’)) + s2; Z:= (n=0); end; For i:= 1 To Length( s1) do If Copy(s1, i, 4) = ’亿万’ Then Delete(s1,i+2,2); NtoC:= IIf(n0=0, ’零’, IIF(n0<0, ’-’,’’)+ s1+s2); End; //对于大写金额中“零”的用法,习惯不同,清指正。 //在FoxPro、VB中,IIF都是内部函数。但Delphi没有,只得自己定义。
Function funLowerToUpper(i:Real):String; const d='零壹贰叁肆伍陆柒捌玖分角元拾佰仟万拾佰仟亿'; var m,k:string; j:integer; begin k:=''; m:=floattostr(int(i*100)); for j:=length(m) downto 1 do k:=k+d[(strtoint(m[Length(m)-j+1])+1)*2-1]+ d[(strtoint(m[Length(m)-j+1])+1)*2]+d[(10+j)*2-1]+d[(10+j)*2]; Result:=k; end;试试这个
function funLowerToUpper(S:string):String; const d='零壹贰叁肆伍陆柒捌玖'; var k:string; a : Char ; j:integer; begin k:=''; for j:=1 to length(S) do begin a:= S[j]; if not (a in['0'..'9']) then begin Result :='存在非数字字符' ; exit end else k:=k+d[strtoint(S[j])*2+1 ]+d[(strtoint(S[j])+1)*2]; end ; Result:=k; end;
此程序为打印发票时小写转为人民币大写的 function TForm_Login.RMB(NN:real):string; var HZ,NS,NW,NA,N1,N2:string; LA,X,Nk:integer; begin if NN>9999999999999.99 then begin MessageDlg('金额溢出.',mtError,[mbOk], 0); HZ:=''; Result:=HZ; exit; end; if NN=0 then begin HZ:='零元'; result:=HZ; exit; end; NN:=strtofloat(formatfloat('###0.00',NN)); NS:='零壹贰叁肆伍陆柒捌玖'; NW:='分角元拾佰仟万拾佰仟亿拾佰仟万'; NA:=FloatToStr(NN*100); LA:=length(NA); X:=1; HZ:=''; while X<=LA do begin NK:=Ord(NA[x])-Ord('0'); N1:=Copy(NS,NK*2+1,2); N2:=Copy(NW,LA*2+1-X*2,2); if (NK=0) AND ((N2='亿') OR( N2='万') OR( N2='元'))then begin if copy(HZ,Length(HZ)-1,2)='零' then HZ:=copy(HZ,1,length(HZ)-2); if copy(HZ,Length(HZ)-1,2)='亿' then if N2='元' then begin N1:=N2; N2:='零'; end else N2:='' else begin N1:=N2; N2:='零'; end end else if NK=0 then begin if copy(HZ,length(HZ)-1,2)='零' then N1:=''; if N2='分' then begin if copy(HZ,length(HZ)-1,2)='零' then HZ:=copy(HZ,1,length(HZ)-2)+'整' else HZ:=HZ+'整'; N1:=''; end; N2:=''; end; HZ:=HZ+N1+N2; X:=X+1 end; Result:=HZ; end;
//本函数用于将小于十万亿元的小写金额转换为大写
Function NtoC( n0 :real) :String;
Function IIF( b :boolean; s1,s2 :string) :string;
begin if b then IIF:= s1 else IIF:=s2;
end; //本函数的功能一目了然: 当b为真时返回s1,否则返回s2
Const c= ’零壹贰叁肆伍陆柒捌玖◇分角圆拾佰仟万拾佰仟亿拾佰仟万’;
var L,i,n :integer; Z :boolean; s,s1,s2 :string;
begin
s:= FormatFloat( ’0.00’, ABS(n0));
L:= Length( s);
Z:= n0<1;
For i:= 1 To L-3 do
begin
n:= StrToInt( s[ L-i-2]);
s1:=IIf((n=0)And(Z Or (i=9) Or (i=5) Or (i=1)), ’’, Copy( c, n*2+1, 2))
+ IIf((n=0)And((i<>9)And(i<>5)And(i<>1) Or Z And(i=1)),’’,Copy(c,(i+13)*2-1,2))
+ s1;
Z:= (n=0);
end;
Z:= False;
For i:= 1 To 2 do
begin
n:= StrToInt( s[ L-i+1]);
s2:= IIf((n=0)And((i=1) Or (i=2)And(Z Or (n0<1))), ’’, Copy( c, n*2+1, 2))
+ IIf( (n>0), Copy( c,(i+11)*2-1, 2), IIf( (i=2) Or Z, ’’,’整’))
+ s2;
Z:= (n=0);
end;
For i:= 1 To Length( s1) do If Copy(s1, i, 4) = ’亿万’ Then Delete(s1,i+2,2);
NtoC:= IIf(n0=0, ’零’, IIF(n0<0, ’-’,’’)+ s1+s2);
End;
//对于大写金额中“零”的用法,习惯不同,清指正。
//在FoxPro、VB中,IIF都是内部函数。但Delphi没有,只得自己定义。
const
d='零壹贰叁肆伍陆柒捌玖分角元拾佰仟万拾佰仟亿';
var
m,k:string;
j:integer;
begin
k:='';
m:=floattostr(int(i*100));
for j:=length(m) downto 1 do
k:=k+d[(strtoint(m[Length(m)-j+1])+1)*2-1]+
d[(strtoint(m[Length(m)-j+1])+1)*2]+d[(10+j)*2-1]+d[(10+j)*2];
Result:=k;
end;试试这个
const
d='零壹贰叁肆伍陆柒捌玖';
var
k:string;
a : Char ;
j:integer;
begin
k:='';
for j:=1 to length(S) do
begin
a:= S[j];
if not (a in['0'..'9']) then
begin
Result :='存在非数字字符' ;
exit
end
else
k:=k+d[strtoint(S[j])*2+1 ]+d[(strtoint(S[j])+1)*2];
end ;
Result:=k;
end;
function TForm_Login.RMB(NN:real):string;
var
HZ,NS,NW,NA,N1,N2:string;
LA,X,Nk:integer;
begin
if NN>9999999999999.99 then
begin
MessageDlg('金额溢出.',mtError,[mbOk], 0);
HZ:='';
Result:=HZ;
exit;
end;
if NN=0 then
begin
HZ:='零元';
result:=HZ;
exit;
end;
NN:=strtofloat(formatfloat('###0.00',NN));
NS:='零壹贰叁肆伍陆柒捌玖';
NW:='分角元拾佰仟万拾佰仟亿拾佰仟万';
NA:=FloatToStr(NN*100);
LA:=length(NA);
X:=1;
HZ:='';
while X<=LA do
begin
NK:=Ord(NA[x])-Ord('0');
N1:=Copy(NS,NK*2+1,2);
N2:=Copy(NW,LA*2+1-X*2,2);
if (NK=0) AND ((N2='亿') OR( N2='万') OR( N2='元'))then
begin
if copy(HZ,Length(HZ)-1,2)='零' then
HZ:=copy(HZ,1,length(HZ)-2);
if copy(HZ,Length(HZ)-1,2)='亿' then
if N2='元' then
begin
N1:=N2;
N2:='零';
end
else
N2:=''
else
begin
N1:=N2;
N2:='零';
end
end
else if NK=0 then
begin
if copy(HZ,length(HZ)-1,2)='零' then
N1:='';
if N2='分' then
begin
if copy(HZ,length(HZ)-1,2)='零' then
HZ:=copy(HZ,1,length(HZ)-2)+'整'
else
HZ:=HZ+'整';
N1:='';
end;
N2:='';
end;
HZ:=HZ+N1+N2;
X:=X+1
end;
Result:=HZ;
end;