Delphi 2009 BUG

apan_bj 2008-09-09 05:33:24
输入123,输出壹佰贰拾叁元整
在D7中可以,很好用,但D2009输出乱七八糟,那位高人可以分析下哪儿的问题?!
-------------------------------------------------------------------------
function toChineseCapitalMoney(const Num:double):string;
const
mnUnit:string='分角元';
hzUnit:string='拾佰仟万拾佰仟亿拾佰仟万拾佰仟';
hzNum:string='零壹贰叁肆伍陆柒捌玖';
var
szChMoney,szNum:string;
iLen, iNum, iAddZero,i:integer;
function DRound(Value:double;cnt:byte):double;
var
fTmp:double;
nTmp:double;
k:int64;
begin
Result:=Value;
if cnt>18 then exit;
nTmp:=Power(10.0,cnt);
fTmp:=0.5;
fTmp:=fTmp/nTmp;
Result:=fTmp+Result;
Result:=Result*nTmp;
k:=0;
asm
fld qword ptr Result
//__ftol begin
push ebp
mov ebp,esp
LEA ESP,k
wait
fstcw word ptr [ebp-$04]
wait
mov al,[ebp-$03]
or [ebp-$04],$00000c01
fldcw word ptr [ebp-$04]
fistp qword ptr [ebp-$0c]
mov [ebp-$03],al
fldcw word ptr [ebp-$04]
mov eax ,[ebp-$0c]
mov edx,[ebp-$08]
mov esp,ebp
pop ebp
//__ftol end
push esp
lea esp,k
mov [esp],eax
add esp,4
mov [esp],edx
mov esp,ebp
pop esp
fild qword ptr k
fstp qword ptr Result
fld qword ptr nTmp
fdivr qword ptr Result
fstp qword ptr Result
end;
end;
begin
iAddZero:=0;
szNum:=FormatFloat('0', DRound(Num,2)*100.0); //这样可能会有数字误差,double只有15位有效数字,这里只有13
Result:='';
iLen:=Length(szNum);
if Pos('E',UpperCase(szNum))>0 then exit;
if (iLen>18) or (iLen=0) or (Num<0) then exit; //数据错误返回

for i:=1 to iLen do
begin
iNum:=StrToInt(copy(szNum,i,1));
if iNum=0 then
begin
if (((iLen-i-2) mod 4)=0) and ((iLen-i-3)>0) and (((iLen-i)>=8) or (iAddZero<3)) then
szChMoney:=copy(szChMoney+hzUnit,((iLen-i-3) mod 8)*2+1,2);
inc(iAddZero);
end
else
begin
if ((iAddZero>0) and ((iLen-i)>=2)) and (((iLen-i-1) mod 4)<>0) then
szChMoney:=szChMoney+copy(hzNum,1,2);
szChMoney:=szChMoney+copy(hzNum,iNum*2+1,2);
iAddZero:=0;
end;
if (iAddZero<1) or ((iLen-i)=2) then
if (iLen-i)>=3 then
begin
szChMoney:=szChMoney+copy(hzUnit,((iLen-i-3) mod 8)*2+1,2);
end
else
szChMoney:=szChMoney+copy(mnUnit,((iLen-i) mod 3)*2+1,2);
end;
if Trim(szChMoney)='' then
szChMoney:='零元整'
else
if copy(szNum,Length(szNum),1)='0' then szChMoney:=szChMoney+'整';
Result:=szChMoney;
end;
------------------------
...全文
159 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ly_liuyang 2008-09-09
  • 打赏
  • 举报
回复
还用汇编干啥?String是Unicode的~汇编不好操作的
mygodsos 2008-09-09
  • 打赏
  • 举报
回复
牛,还汇编的
zwjchina 2008-09-09
  • 打赏
  • 举报
回复
没仔细看逻辑,主要修改了字符串的处理。
简单验证了下是对的,你自己测试看看。


function toChineseCapitalMoney(const Num:double):string;
const
mnUnit:string='分角元';
hzUnit:string='拾佰仟万拾佰仟亿拾佰仟万拾佰仟';
hzNum:string='零壹贰叁肆伍陆柒捌玖';
var
szChMoney,szNum:string;
iLen, iNum, iAddZero,i:integer;
function DRound(Value:double;cnt:byte):double;
var
fTmp:double;
nTmp:double;
k:int64;
begin
Result:=Value;
if cnt>18 then exit;
nTmp := Power(10.0,cnt);
fTmp:=0.5;
fTmp:=fTmp/nTmp;
Result:=fTmp+Result;
Result:=Result*nTmp;
k:=0;
asm
fld qword ptr Result
//__ftol begin
push ebp
mov ebp,esp
LEA ESP,k
wait
fstcw word ptr [ebp-$04]
wait
mov al,[ebp-$03]
or [ebp-$04],$00000c01
fldcw word ptr [ebp-$04]
fistp qword ptr [ebp-$0c]
mov [ebp-$03],al
fldcw word ptr [ebp-$04]
mov eax ,[ebp-$0c]
mov edx,[ebp-$08]
mov esp,ebp
pop ebp
//__ftol end
push esp
lea esp,k
mov [esp],eax
add esp,4
mov [esp],edx
mov esp,ebp
pop esp
fild qword ptr k
fstp qword ptr Result
fld qword ptr nTmp
fdivr qword ptr Result
fstp qword ptr Result
end;
end;
begin
iAddZero:=0;
szNum:=FormatFloat('0', DRound(Num,2)*100.0); //这样可能会有数字误差,double只有15位有效数字,这里只有13
Result:='';
iLen:=Length(szNum);
if Pos('E',UpperCase(szNum))>0 then exit;
if (iLen>18) or (iLen=0) or (Num <0) then exit; //数据错误返回

for i:=1 to iLen do
begin
iNum:=StrToInt(copy(szNum,i,1));
if iNum=0 then
begin
if (((iLen-i-2) mod 4)=0) and ((iLen-i-3)>0) and (((iLen-i)>=8) or (iAddZero <3)) then
begin
szChMoney := szChMoney + hzUnit;
szChMoney := szChMoney[(ilen - i - 3) mod 8 + 1];
end;
inc(iAddZero);
end
else
begin
if ((iAddZero>0) and ((iLen-i)>=2)) and (((iLen-i-1) mod 4) <>0) then
szChMoney := szChMoney + hzNum[1];
szChMoney:=szChMoney + hzNum[iNum + 1];
iAddZero:=0;
end;
if (iAddZero <1) or ((iLen-i)=2) then
if (iLen-i)>=3 then
begin
szChMoney := szChMoney + hzUnit[(ilen - i - 3) mod 8 + 1];
//szChMoney:=szChMoney+copy(hzUnit,(((iLen-i-3) mod 8)*2+1), 2);
end
else
begin
szChMoney := szChMoney + mnUnit[(iLen-i) mod 3 + 1];
//copy(mnUnit,((iLen-i) mod 3)*2+1,2);
end;
end;
if Trim(szChMoney)='' then
szChMoney:='零元整'
else
if szNum[Length(szNum)]='0' then szChMoney:=szChMoney+'整';
Result:=szChMoney;
end;
Rainstorey 2008-09-09
  • 打赏
  • 举报
回复
本来想仔细看看,但看这标题我也就不看了。

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧