我从网上下了两个小写金额转换大写金额的函数。 可是有些不足。 第一个函数没有对零的处理。 第二个函数显示不出值, 请各位大虾帮我看看啊。 function TForm1.xTOd(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]; xTOd:=k; end;
调用: procedure TForm1.Button1Click(Sender: TObject); var Sum:real; begin sum:=12.34; showmessage('人民币大写:'+xTOd(Sum)); end; ////////////////////////////////////////////////////////////// function RMBchange(num:extended):string; var yuan,ge,wan,yi:int64;
jiao:short; strge,strwan,stryj,tempstr:string;
isyuannotempty:boolean; tempextended;extended;
function singerchange(num:short):string; begin
case num of 1: result:='壹'; 2: result:='贰'; 3: result:='叁'; 4: result:='肆'; 5: result:='伍'; 6: result:='陆'; 7: result:='柒'; 8: result:='捌'; 9: result:='玫'; 0: result:='; end; end;
function fourchange(num:integer):string; var qian,bai,shi,ge:string; begin qian:=singerchange(num div 1000); if qian<>' then qian:=qian+'仟'; bai:=singerchange(num div 100 mod 10); if bai<>' then bai:=bai+'佰'; shi:=singerchange(num div 10 mod 10); if shi<>' then shi:=shi+'拾'; ge:=singerchange(num mod 10);
if(qian<>')and(bai=') then if(shi<>')or(ge<>')then bai:='零'; if(qian<>')or(bai<>') then if(shi=')and(ge<>') and (bai<>'零')then shi:='零'; result:=qian+bai+shi+ge; end;
function littlechange(num:short):string; var jiao,fen:string; begin jiao:=singerchange(num div 10); fen:+singerchange(num mod 10); if jiao<>' then jiao:=jiao+'角'; if fen<>' then fen:=fen+'分';
if jiao=' then if fen<>' then if isyuannotempty then fen:='零' +fen; result:=jiao+fen; end;
begin yuan:=strtoint64(floattostr(int(num))); if pos('.',floattostr(num))<>0 then begin tempstr:='0.'+copy(floattostr(num),pos('.',floattostr(num))+1,255); tempextended:=strtofloat(floattostr(realround(strtofloat(tempstr),2)*100)); jiao:= strtoint64(floattostr(int(tempextended))); end else jiao:=0;
yi:=yuan div 100000000; wan:=yuan div 10000 mod 10000; ge:=yuan mod 10000; strge:=fourchange(wan); stryi:=fourchange(yi);
if stryi<>' then stryi:=stryi+'亿'; if strwan<>' then strwan:=strwan+'万';
if(stryi<>')or(strwan<>') then if(pos('千',strge)=0) and (strge<>') then strge:='零'+strge;
if stryi<>' then if(pos('千',strwan)=0)and(strwan<>') then strwan:='零'+strwan;
if(stryi+strwan+strge)<>' then isyuannotempty:=true else isyuannotempty:=false;
if isyuannotempty then result:=stryi+strwan+strge+'元' else result:=stryi+strwan+strge;
if jiao<>0 then result:=result+littlechange(jiao); end;
function TForm1.xTOd(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]; xTOd:=k; end;
调用: procedure TForm1.Button1Click(Sender: TObject); var Sum:real; begin sum:=12.34; showmessage('人民币大写:'+xTOd(Sum)); end; ////////////////////////////////////////////////////////////// function RMBchange(num:extended):string; var yuan,ge,wan,yi:int64;
jiao:short; strge,strwan,stryj,tempstr:string;
isyuannotempty:boolean; tempextended;extended;
function singerchange(num:short):string; begin
case num of 1: result:='壹'; 2: result:='贰'; 3: result:='叁'; 4: result:='肆'; 5: result:='伍'; 6: result:='陆'; 7: result:='柒'; 8: result:='捌'; 9: result:='玫'; 0: result:='; end; end;
function fourchange(num:integer):string; var qian,bai,shi,ge:string; begin qian:=singerchange(num div 1000); if qian<>' then qian:=qian+'仟'; bai:=singerchange(num div 100 mod 10); if bai<>' then bai:=bai+'佰'; shi:=singerchange(num div 10 mod 10); if shi<>' then shi:=shi+'拾'; ge:=singerchange(num mod 10);
if(qian<>')and(bai=') then if(shi<>')or(ge<>')then bai:='零'; if(qian<>')or(bai<>') then if(shi=')and(ge<>') and (bai<>'零')then shi:='零'; result:=qian+bai+shi+ge; end;
function littlechange(num:short):string; var jiao,fen:string; begin jiao:=singerchange(num div 10); fen:+singerchange(num mod 10); if jiao<>' then jiao:=jiao+'角'; if fen<>' then fen:=fen+'分';
if jiao=' then if fen<>' then if isyuannotempty then fen:='零' +fen; result:=jiao+fen; end;
begin yuan:=strtoint64(floattostr(int(num))); if pos('.',floattostr(num))<>0 then begin tempstr:='0.'+copy(floattostr(num),pos('.',floattostr(num))+1,255); tempextended:=strtofloat(floattostr(realround(strtofloat(tempstr),2)*100)); jiao:= strtoint64(floattostr(int(tempextended))); end else jiao:=0;
yi:=yuan div 100000000; wan:=yuan div 10000 mod 10000; ge:=yuan mod 10000; strge:=fourchange(wan); stryi:=fourchange(yi);
if stryi<>' then stryi:=stryi+'亿'; if strwan<>' then strwan:=strwan+'万';
if(stryi<>')or(strwan<>') then if(pos('千',strge)=0) and (strge<>') then strge:='零'+strge;
if stryi<>' then if(pos('千',strwan)=0)and(strwan<>') then strwan:='零'+strwan;
if(stryi+strwan+strge)<>' then isyuannotempty:=true else isyuannotempty:=false;
if isyuannotempty then result:=stryi+strwan+strge+'元' else result:=stryi+strwan+strge;
if jiao<>0 then result:=result+littlechange(jiao); end;
//整数部分处理 ls_capital_num = "" If Integer(ls_integer) <> 0 Then lb_begin = True li_last_num = 10 For li_i = 0 To li_len - 1 li_num = Integer(Mid(ls_integer, li_len - li_i, 1)) li_y = Truncate(li_i / 4, 0) li_x = li_i - li_y * 4 If li_num <> 0 Then ls_capital[li_x, li_y] = ls_num[li_num] + ls_unit[li_x, li_y] lb_begin = False Else If li_x = 0 Then If li_y <> 0 And li_last_num <> 0 Then ls_capital[li_x, li_y] = ls_unit[li_x, li_y] + '零' Else ls_capital[li_x, li_y] = ls_unit[li_x, li_y] End If Else If Not lb_begin And li_last_num <> 0 Then ls_capital[li_x, li_y] = '零' Else ls_capital[li_x, li_y] = ' End If End If //当本段四位数字全为 0 时,不需要本段四位数字的单位 If li_x = 3 Then If Mid(ls_integer, li_len - (li_y * 4 + li_x), 4) = '0000' And li_y <> 0 Then ls_capital[0, li_y] = Mid(ls_capital[0, li_y], 3) End If End If End If li_last_num = li_num Next For li_i = 0 To li_len - 1 li_y = Truncate(li_i / 4, 0) li_x = li_i - li_y * 4 ls_capital_num = ls_capital[li_x, li_y] + ls_capital_num Next End If
//小数部分处理,即添加角分 If Integer(ls_decimal) <> 0 Then li_jiao = Integer(Left(ls_decimal, 1)) li_fen = Integer(Right(ls_decimal, 1)) If li_jiao > 0 Then ls_capital_num = ls_capital_num + ls_num[li_jiao] + "角" End If If li_fen > 0 Then If li_jiao = 0 And Long(ls_integer) > 0 Then ls_capital_num = ls_capital_num + '零' End If ls_capital_num = ls_capital_num + ls_num[li_fen] + "分" End If End If
//添加“整” If Integer(ls_decimal) = 0 Then ls_capital_num = ls_capital_num + "整" ElseIf Integer(ls_decimal) <> 0 Then li_fen = Integer(Right(ls_decimal, 1)) If li_fen = 0 Then ls_capital_num = ls_capital_num + "整" End If
//添加“负” If ld_num < 0 Then ls_capital_num = "负" + ls_capital_num End If
if (ld_num > -0.0000001) and (ld_num < 0) then ls_capital_num = '零元整' end if
//整数部分处理 ls_capital_num := '; If Integer(ls_integer) <> 0 Then begin//1 lb_begin := True ; li_last_num := 10; For li_i := 0 To li_len - 1 do begin //2 li_num := Integer(copy(ls_integer, li_len - li_i, 1)); li_y := Trunc(li_i / 4); li_x := li_i - li_y * 4 ; If li_num <> 0 Then begin ls_capital[li_x, li_y] := ls_num[li_num] + ls_unit[li_x, li_y]; lb_begin := False; end Else begin If li_x = 0 Then begin If (li_y <> 0) And (li_last_num <> 0) Then begin ls_capital[li_x, li_y] := ls_unit[li_x, li_y] + '零'; end Else begin ls_capital[li_x, li_y] := ls_unit[li_x, li_y]; end; //end if end Else begin If (Not lb_begin) And (li_last_num <> 0) Then ls_capital[li_x, li_y] := '零' Else ls_capital[li_x, li_y] := ';
end; //end if
//当本段四位数字全为 0 时,不需要本段四位数字的单位 If li_x = 3 Then begin If (MidStr(ls_integer, li_len - (li_y * 4 + li_x), 4)= '0000') And (li_y <> 0) Then begin ls_capital[0, li_y] := MidStr(ls_capital[0, li_y],1, 3); end; end; //end if
li_last_num := li_num; end;// end ;// Next for //2
For li_i:=0 To li_len - 1 do begin li_y := Trunc(li_i / 4); li_x := li_i - li_y * 4; ls_capital_num := ls_capital[li_x, li_y] + ls_capital_num ; end;//Next for end; //end if //1
//小数部分处理,即添加角分 If Integer(ls_decimal) <> 0 Then begin li_jiao := Integer(LeftStr(ls_decimal, 1)); li_fen := Integer(RightStr(ls_decimal, 1)); If li_jiao > 0 Then begin ls_capital_num := ls_capital_num + ls_num[li_jiao] + '角'; end; If li_fen > 0 Then begin If (li_jiao = 0) And (LongInt(ls_integer) > 0) Then begin ls_capital_num := ls_capital_num + '零'; end; ls_capital_num := ls_capital_num + ls_num[li_fen] + '分'; end; end;
//添加“整” If Integer(ls_decimal) = 0 Then begin ls_capital_num := ls_capital_num + '整'; end Else begin If Integer(ls_decimal) <> 0 Then begin li_fen := Integer(RightStr(ls_decimal, 1)); end; If li_fen = 0 Then begin ls_capital_num := ls_capital_num + '整' end; end;
If ld_num < 0 Then ls_capital_num := '负' + ls_capital_num ;
if (ld_num > -0.0000001) and (ld_num < 0) then ls_capital_num := '零元整';
Result:=ls_capital_num;
end;
procedure TForm1.Button1Click(Sender: TObject); var f:double; s:string; begin s:=trim(edit1.text); f:=strtofloat(s); edit2.Text := xiao2da(f); end;