create or replace function l2u -- 小写金额转换成大写
(n_lowermoney in number) return varchar2 as
v_lowerstr varchar2(200); -- 小写金额
v_upperpart varchar2(200);
v_upperstr varchar2(200); -- 大写金额
begin
v_lowerstr := ltrim(rtrim(to_char(round(n_lowermoney, 2),
'9999999999999.99')));
if substr(v_lowerstr, 1, 1) = '#' then
return '转换金额超过计算范围(计算范围为:计算范围为: 0 - 9,999,999,999,999.99)';
end if;
for i in 1 .. length(v_lowerstr)
loop
select decode(substr(v_lowerstr, length(v_lowerstr) - i + 1, 1),
'.',
'元',
'0',
'零',
'1',
'壹',
'2',
'贰',
'3',
'叁',
'4',
'肆',
'5',
'伍',
'6',
'陆',
'7',
'柒',
'8',
'捌',
'9',
'玖') || decode(i,
1,
'分',
2,
'角',
3,
null,
4,
null,
5,
'拾',
6,
'佰',
7,
'仟',
8,
'万',
9,
'拾',
10,
'佰',
11,
'仟',
12,
'亿',
13,
'拾',
14,
'佰',
15,
'仟',
16,
'万',
null)
into v_upperpart
from dual;
v_upperstr := v_upperpart || v_upperstr;
end loop;
v_upperstr := replace(v_upperstr, '零拾', '零');
v_upperstr := replace(v_upperstr, '零佰', '零');
v_upperstr := replace(v_upperstr, '零仟', '零');
v_upperstr := replace(v_upperstr, '零零零', '零');
v_upperstr := replace(v_upperstr, '零零', '零');
v_upperstr := replace(v_upperstr, '零角零分', '整');
v_upperstr := replace(v_upperstr, '零分', '整');
v_upperstr := replace(v_upperstr, '零角', '零');
v_upperstr := replace(v_upperstr, '零亿零万零元', '亿元');
v_upperstr := replace(v_upperstr, '亿零万零元', '亿元');
v_upperstr := replace(v_upperstr, '零亿零万', '亿');
v_upperstr := replace(v_upperstr, '零万零元', '万元');
v_upperstr := replace(v_upperstr, '万零元', '万元');
v_upperstr := replace(v_upperstr, '零亿', '亿');
v_upperstr := replace(v_upperstr, '零万', '万');
v_upperstr := replace(v_upperstr, '零元', '元');
v_upperstr := replace(v_upperstr, '零零', '零');
-- 对壹元以下的金额的处理
v_upperstr := ltrim(ltrim(ltrim(ltrim(v_upperstr, '元'), '零'), '角'),
'分');
if substr(v_upperstr, 1, 1) = '整' then
v_upperstr := '零元整';
end if;
return v_upperstr;
exception
when others then
return '发生错误: ' || sqlcode || '--' || sqlerrm;
end l2u;