5,392
社区成员
发帖
与我相关
我的任务
分享
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;