//别人的, 借花献佛
unit MoneyConversion;
interface
uses Classes, Windows, SysUtils;
function Chn_Money(Money: Currency): string;
implementation
const
chn_number: array [0..9] of string=('零','壹','贰','叁','肆','伍','陆','柒','捌','玖');
chn_sectionunit: array [0..2] of string=('','万','亿');
function Chn_Intvalue(IValue: integer): string;
var
intstr, signstr, tstr: string;
q_digit, b_digit, s_digit, g_digit: string;
len, sectionlevel: integer;
zeroresulthead, zerohead: boolean;
begin
if (ivalue=0) then
begin
result:='零';
exit;
end
else if (ivalue<0) then
begin
signstr:='负';
ivalue:=-ivalue;
end
else signstr:='';
intstr:=inttostr(ivalue);
len:=Length(intstr);
sectionlevel:=-1; //以4位数字为一节: eg. "万"节; "亿"节;
zeroresulthead:=false;
//跨节用途.(指明上一循环后产生的中间结果(低位"节"
是否为"零"开头)
while (len>0) do
begin
inc(sectionlevel);
if (sectionlevel>=3) then // 限定 1 兆以内.
begin
raise Exception.Create('超出人民币计算范围: value('+intstr+')。');
abort;
end;
//取得本节各位的数值, 并更新数据源intstr.
g_digit:=Copy(intstr, len, 1); // 节内"个"位.
if (len>1) then s_digit:=Copy(intstr, len-1, 1)
else s_digit:=''; // 节内"十"位.
if (len>2) then b_digit:=Copy(intstr, len-2, 1)
else b_digit:=''; // 节内"百"位.
if (len>3) then q_digit:=Copy(intstr, len-3, 1)
else q_digit:=''; // 节内"千"位.
if (len>4) then Delete(intstr, len-3, 4)
else intstr:='';
len:=Length(intstr);
//对本节进行中文翻译.
zerohead:=false;
if (g_digit<>'0') then tstr:=chn_number[strtoint(g_digit)]
else tstr:=''; //完成"个"位中文翻译.
if (s_digit='0') then s_digit:=''; //完成"拾"位中文翻译.
if (s_digit<>'') then
begin
tstr:=chn_number[strtoint(s_digit)]+'拾'+tstr;
zerohead:=false;
end
else if (tstr<>'') then
begin
tstr:='零'+tstr;
zerohead:=true;
end;
if (b_digit='0') then b_digit:=''; //完成"佰"位中文翻译.
if (b_digit<>'') then
begin
tstr:=chn_number[strtoint(b_digit)]+'佰'+tstr;
zerohead:=false;
end
else if (tstr<>'') and not(zerohead) then
begin
tstr:='零'+tstr;
zerohead:=true;
end;
if (q_digit='0') then q_digit:=''; //完成"千"位中文翻译.
if (q_digit<>'') then
begin
tstr:=chn_number[strtoint(q_digit)]+'仟'+tstr;
zerohead:=false;
end
else if (tstr<>'') and not(zerohead) then
begin
tstr:='零'+tstr;
zerohead:=true;
end;
if (tstr<>'') then
begin
result:=tstr+chn_sectionunit[sectionlevel]+result;
zeroresulthead:=zerohead;
end
else if not(zeroresulthead) then
begin
result:='零'+result;
zeroresulthead:=true;
end;
end; // of while
if (zeroresulthead) then Delete(result, 1, 2); // 除首"零"
result:=signstr+result;
end;
//-----------------------------------------------------------------
function Chn_Money(Money: Currency): string;
var
intpart, chiao, cent: integer;
fracpart: double;
signstr, tstr: string;
begin
signstr:='';
if (money<0) then
begin
money:=-money;
signstr:='负';
end;
money:=money+0.005; //包含四舍五入
intpart:=Trunc(money); //Trunc truncates a real number to an integer.
fracpart:=money-intpart;
fracpart:=fracpart*10;
chiao:=Trunc(fracpart);
fracpart:=fracpart-chiao;
fracpart:=fracpart*10;
cent:=Trunc(fracpart);
tstr:=chn_intvalue(intpart);
if (chiao=0) and (cent=0) then
result:=tstr+'元整';
if (chiao <> 0) and (cent = 0) then
result:=tstr+'元'+chn_number[chiao]+'角整';
if (chiao=0) and (cent <> 0) then
result:=tstr+'元'+'零'+chn_number[cent]+'分';
if (chiao <> 0) and (cent <> 0) then
result:=tstr+'元'+chn_number[chiao]+'角'+chn_number[cent]+'分';
result:=signstr+result;
end;
end.