declare
nullException exception;
notDigitException exception;
Result varchar2(100);--返回字符串
num varchar2(20):= '&num'; --转换的数字
str1 char(20) :='零壹贰参肆伍陆柒捌玖'; --数字大写
str2 char(50) :='元拾佰仟万拾佰仟亿拾佰仟万拾佰仟'; --数字位数(从低至高,暂可支持16位,如需再长则在字符串后面添加‘兆拾佰...’)
num_pre number(1) := 1;--前(低)一位上的数字
num_current number(1);--当前位上的数字
num_count number(2) := 0;--当前数字位数
begin
dbms_output.put_line('******************人民币小写转大写******************');
dbms_output.put_line('');
--转换数字为null时返回null
if num is null then
raise nullException; --当输入数字为空时抛出空异常
end if;
if substr(num,1,1)='0' and length(num)!=1 then
raise notDigitException; --当输入数字以零开头时抛出自定义异常
end if;
--处理数字
for i in reverse 1..length(num) loop --(从低至高)
num_count := num_count + 1 ;--当前数字位数
num_current := to_number(substr(num,i,1));--当前位上的数字
if num_current>0 then --当前位上数字不为0按正常处理
result := substr(str1,num_current+1,1) || substr(str2,num_count,1) || result;
else --当前位上数字为0时
if mod(num_count-1,4)=0 then --当前位是元、万或亿时
result := substr(str2,num_count,1) || result;
num_pre := 0;--元、万,亿前不准加零
end if;
if num_pre>0 or length(num)=1 then --上一位数字不为0或只有个位时
result := substr(str1,num_current+1,1) || result;
end if;
end if;
num_pre := num_current; --获取前(低)一位上的数字
end loop;
dbms_output.put_line('RMB '|| num || '元 转为大写为: ' || Result);
exception
when nullException then
dbms_output.put_line('请输入数字');
when notDigitException then
dbms_output.put_line('请输入合法的整数');
when others then
raise_application_error(-20001,'数字转换大写出现错误!'||sqlerrm);
end ;