如何实现小写金额到大写金额的转换及在转账支票上打印。(200)

  • 主题发起人 主题发起人 sharpwing
  • 开始时间 开始时间
S

sharpwing

Unregistered / Unconfirmed
GUEST, unregistred user!
如何实现小写金额到大写金额的转换及在转账支票上打印。谢谢。找了些,都不能太好的处理零的问题。请高手指教指教。给个思路。谢谢。
 
先用现成的,自己再编写处理零问题。
 
function TFunctions.CovertMoney(Money: Extended): String;Var sStr, buf, sPower, buf1 : String;
i, j, iPosition, lenth, iTmp : Integer;
flag0 : boolean;
Function Sjoin(Var buff : String;
sString : String;
digit : String;
nextdig : String;
iPos : Integer) : Integer;
Var dig : String;
// * 1 flag0 : Boolean;
begin
flag0 := True;
If digit = '0' then
If nextdig <> '0' then
dig := '零' else
flag0 := False;
If digit = '1' then
dig := '壹';
If digit = '2' then
dig := '贰';
If digit = '3' then
dig := '叁';
If digit = '4' then
dig := '肆';
If digit = '5' then
dig := '伍';
If digit = '6' then
dig := '陆';
If digit = '7' then
dig := '柒';
If digit = '8' then
dig := '捌';
If digit = '9' then
dig := '玖';
If digit = '0' then
If (flag0 = True) And (iPos <> 0) then
begin
buff := buff + dig;
Result := 2;
End else
Result := 0 else
begin
buff := buff + dig;
buff := buff + sString;
Result := 4;
end;
end;
begin
If money = 0 then
begin
Result := '零元整';
exit;
end;
iPosition := 0;
buf1 := trim(Format('%12.2f', [money]));
lenth := Length(buf1);
buf := ' ';
// 16 bit space;
j := 1;
For i := 16 - lenth To 15 do
begin
buf := buf1[j];
j := j + 1;
end;
For i := 1 To 15 do
begin
iTmp := ord(buf);
If ((iTmp >= 49) And (iTmp <= 58)) Or (chr(iTmp) = ',') then
break;
end;
While i <= 15 do
begin
flag0 := True;
iTmp := 0;
Case i - 1 Of 0, 4, 8 : sPower := '仟';
1, 5, 9 : sPower := '佰';
2, 6, 10 : sPower := '拾';
3 : begin
sPower := '亿';
If copy(buf, i, 1) = '0' then
begin
sStr := sStr + sPower+'零';
iPosition := iPosition + 2;
iTmp := 1;
end;
end;
7 : begin
sPower := '万';
If copy(buf, i, 1) = '0' then
begin
sStr := sStr + sPower+'零';
iPosition := iPosition + 2;
iTmp := 1;
end;
end;
11 : begin
sPower := '元';
If (copy(buf, i, 1) = '0') And (iPosition <> 0) then
begin
sStr := sStr + sPower;
iPosition := iPosition + 2;
iTmp := 1;
end;
end;
12 : begin
flag0 := False;
If (copy(buf, i + 1, 1) = '0') And (copy(buf, i + 2, 1) = '0') then
begin
sPower := '整';
sStr := sStr + sPower;
iPosition := iPosition + 2;
iTmp := 2;
end;
end;
13 : sPower := '角';
14 : begin
If copy(buf, i, 1) = '0' then
iTmp := 2;
sPower := '分';
end;
end;
If (flag0 = True) And (iTmp = 0) then
begin
If i > 0 then
iPosition := iPosition + Sjoin(sStr, sPower, copy(buf, i, 1), copy(buf, i + 1, 1), iPosition) else
iPosition := iPosition + Sjoin(sStr, sPower, copy(buf, i, 1), copy(buf, i, 1), iPosition);
end;
If iTmp = 2 then
break else
i := i + 1;
end;
// If (ilength <> 0) then
ilength := iPosition;
If pos('整', sStr) < 1 then
begin
if (pos('角', sStr) < 1) and (pos('分', sStr) < 1) then
sStr := sStr + '整';
end;
sStr :=stringreplace(sStr,'零零','零',[rfReplaceAll]);
sStr :=stringreplace(sStr,'零元','元',[rfReplaceAll]);
Result := Trim(sStr);
end;
 
hi,hellbeast。谢谢,你给的代码基本上可以,由点小瑕疵,就是在亿的时候,后面会多个一个万字。例如:900000001 玖亿零壹元整 ,能不能帮着改一下啊。谢谢。200分都给你啦。
 
试试下面的:function StrToRMB(Curs: string): String;var Daxie, Danwei, MinusCurs: string;
I, J, Deccount: Integer;
Rmb: Int64;
begin
Curs := Trim(Curs);
if (Curs = '-') or (Curs = '.') or (Curs = '') then
// '.','-',''错 begin
Result := 'ERROR';
Exit;
end;
Deccount := 0;
for I := 1 to Length(Curs)do
begin
if not (Curs in ['0'..'9', '.', '-']) then
//'123w2'错 begin
Result := 'ERROR';
Exit;
end;
if (Curs = '.') and (Deccount > 0) then
//'12313.324.23'错 begin
Result := 'ERROR';
Exit;
end;
if (Curs = '-') and (I > 1) then
//'-123-123'错 begin
Result := 'ERROR';
Exit;
end;
if Curs = '.' then
Inc(Deccount);
end;
Rmb := Round(StrToFloat(Curs) * 100);
MinusCurs := '';
//负数标志 if Rmb < 0 then
begin
MinusCurs := '(-)';
Rmb := (-1) * Rmb;
end;
if Rmb >= 1E18 then
//超过9千万亿 begin
Result := 'ERROR';
Exit;
end;
Curs := '';
I := 0;
J := 0;
while Rmb > 0do
begin
J := Rmb mod 10;
case J of 0: Daxie := '零';
1: Daxie := '壹';
2: Daxie := '贰';
3: Daxie := '叁';
4: Daxie := '肆';
5: Daxie := '伍';
6: Daxie := '陆';
7: Daxie := '柒';
8: Daxie := '捌';
9: Daxie := '玖';
end;
case I of 0: Danwei := '分';
1: Danwei := '角';
2: Danwei := '圆';
3: Danwei := '拾';
4: Danwei := '佰';
5: Danwei := '仟';
6: Danwei := '万';
7: Danwei := '拾';
8: Danwei := '佰';
9: Danwei := '仟';
10: Danwei := '亿';
11: Danwei := '拾';
12: Danwei := '佰';
13: Danwei := '仟';
14: Danwei := '万';
15: Danwei := '拾';
16: Danwei := '佰';
17: Danwei := '仟';
end;
Rmb := Rmb div 10;
if J <> 0 then
Curs := Daxie + Danwei + Curs;
//该位上不为0 if (J = 0) and (not (I in [2, 6, 10, 14])) then
//该位为0,是一般位 Curs := Daxie + Curs;
if (J = 0) and (I in [2, 6, 10, 14]) then
//该位为0,是敏感位 Curs := Danwei + Curs;
Inc(I);
end;
while Pos('零零', Curs) > 0do
Curs := StringReplace(Curs, '零零', '零', []);
Curs := StringReplace(Curs, '零圆', '圆', []);
while Pos('零万', Curs) > 0do
Curs := StringReplace(Curs, '零万', '万', []);
//上万亿后可能两个'零万' Curs := StringReplace(Curs, '零亿', '亿', []);
Curs := StringReplace(Curs, '角零', '角整', []);
if Copy(Curs, Length(Curs) - 3, 4) = '圆零' then
//最后两位是圆零. Curs := StringReplace(Curs, '圆零', '圆整', []);
//小数点后 Curs := StringReplace(Curs, '亿万', '亿', []);
Result := MinusCurs + Curs;
end;
 
感谢。szhcracker的完美。但也要谢谢hellbeast.
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部