create or replace function money2chinese(money in number) return varchar2 is
stryuan varchar2(150);
stryuanfen varchar2(152);
numlenyuan number;
numlenyuanfen number;
strrstyuan varchar2(600);
strrstfen varchar2(200);
strrst varchar2(800);
type typetabmapping is table of varchar2(2) index by binary_integer;
tabnummapping typetabmapping;
tabunitmapping typetabmapping;
numunitindex number;
i number;
j number;
charcurrentnum char(1);
begin
if money is null then
return null;
end if;
stryuan := to_char(floor(money));
if stryuan = '0' then
numlenyuan := 0;
stryuanfen := lpad(to_char(floor(money * 100)), 2, '0');
else
numlenyuan := length(stryuan);
stryuanfen := to_char(floor(money * 100));
end if;
if stryuanfen = '0' then
numlenyuanfen := 0;
else
numlenyuanfen := length(stryuanfen);
end if;
if numlenyuan = 0 or numlenyuanfen = 0 then
strrst := '零圆整';
return strrst;
end if;
tabnummapping(0) := '零';
tabnummapping(1) := '壹';
tabnummapping(2) := '贰';
tabnummapping(3) := '叁';
tabnummapping(4) := '肆';
tabnummapping(5) := '伍';
tabnummapping(6) := '陆';
tabnummapping(7) := '柒';
tabnummapping(8) := '捌';
tabnummapping(9) := '玖';
tabunitmapping(-2) := '分';
tabunitmapping(-1) := '角';
tabunitmapping(1) := '';
tabunitmapping(2) := '拾';
tabunitmapping(3) := '佰';
tabunitmapping(4) := '仟';
tabunitmapping(5) := '万';
tabunitmapping(6) := '拾';
tabunitmapping(7) := '佰';
tabunitmapping(8) := '仟';
tabunitmapping(9) := '亿';
for i in 1 .. numlenyuan loop
j := numlenyuan - i + 1;
numunitindex := mod(i, 8);
if numunitindex = 0 then
numunitindex := 8;
end if;
if numunitindex = 1 and i > 1 then
strrstyuan := tabunitmapping(9) || strrstyuan;
end if;
charcurrentnum := substr(stryuan, j, 1);
if charcurrentnum <> 0 then
strrstyuan := tabnummapping(charcurrentnum) ||
tabunitmapping(numunitindex) || strrstyuan;
else
if (i = 1 or i = 5) then
if substr(stryuan, j - 3, 4) <> '0000' then
strrstyuan := tabunitmapping(numunitindex) || strrstyuan;
end if;
else
if substr(stryuan, j + 1, 1) <> '0' then
strrstyuan := tabnummapping(charcurrentnum) || strrstyuan;
end if;
end if;
end if;
end loop;
for i in -2 .. -1 loop
j := numlenyuan - i;
charcurrentnum := substr(stryuanfen, j, 1);
if charcurrentnum <> '0' then
strrstfen := tabnummapping(charcurrentnum) || tabunitmapping(i) ||
strrstfen;
end if;
end loop;
if strrstyuan is not null then
strrstyuan := strrstyuan || '圆';
end if;
if strrstfen is null then
strrstyuan := strrstyuan || '整';
elsif length(strrstfen) = 2 and substr(strrstfen, 2) = '角' then
strrstfen := strrstfen || '整';
end if;
strrst := strrstyuan || strrstfen;
--strrst := replace(strrst, '亿零', '亿');
--strrst := replace(strrst, '万零', '万');
return strrst;
end money2chinese;