大小寫轉換函式(財務應用)

junsansi發表於2007-04-10
CREATE OR REPLACE FUNCTION num2rmb(je NUMBER) RETURN VARCHAR2 IS
result varchar2(100);
i pls_integer;
snum varchar2(20) := ltrim(replace(to_char(abs(je), '9999999999999990.99'), '.'));
len pls_integer := length(snum);
sch varchar2(20) := '零壹貳叄肆伍陸柒捌玖';
sjin varchar2(50) := '分角圓拾佰仟萬拾佰仟億拾佰仟萬拾佰仟';
srmb varchar2(100):= '';
num pls_integer;
jin varchar(2);
s_num pls_integer := 0; --'0'起始位置
e_num pls_integer := 0; --'0'結束位置
begin
for i in 1..len loop
num := to_number(substr(snum, i, 1));
if num <> 0 then --非'0'時處理:
if s_num = 0 then
srmb := srmb||substr(sch, num + 1, 1)||substr(sjin, len - i + 1, 1); --前面字元非'0', 正常聯接...
else
srmb := srmb|| --否則:
case when s_num = e_num then --前面只有一個'0'
case s_num when 7 then '萬' --只處理進位
when 11 then '億'
when 15 then '萬' end
when e_num < 12 then --否則(多個'0'處理)
case when s_num < 7 then '' --萬以內..
when s_num < 11 then case when e_num < 8 and s_num < 10 then '萬' end
when s_num < 15 then case when e_num < 12 then '億' end
else '萬億' end
when e_num < 16 and s_num > 14 then '萬' end||
case when s_num > 3 and e_num < 3 then '圓零'
when e_num = 3 then '圓'
when e_num not in (7, 11, 15) or s_num - e_num > 2 then '零' end;
srmb := srmb||substr(sch, num + 1, 1)||substr(sjin, len - i + 1, 1);
end if;
s_num := 0;
e_num := 0;
else if s_num = 0 then --當s_num = 0時'0'串起始,
s_num := len - i + 1; --記錄開始
e_num := s_num; --各結整位置。
else
e_num := len - i + 1; --否則新的結整位置。
end if;
end if;
end loop;
if s_num <>0 then --此時以'0'結尾
srmb := srmb||
case when s_num = len then '零圓整' --全'0'串,加...
when s_num = 1 or s_num = 2 then '整' --分(1),角(2)後加...
when s_num < 7 or s_num = 10 then '圓整'
when s_num <11 then '萬圓整'
when s_num <15 then '億圓整'
else '萬億圓整' end;
else
srmb := srmb||'整'; --不以'0'結尾,加...
end if;
if je < 0 then
srmb:='負'||srmb;
end if;
RETURN srmb;
end num2rmb;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7607759/viewspace-22209/,如需轉載,請註明出處,否則將追究法律責任。

相關文章