您的位置:首页 > 其它

金额大小写转换(1)

2016-12-08 15:35 190 查看
create or replace function smalltobig(smallmoney varchar2)

return varchar2 is

bigwrite varchar2(54); --用于返回大写的钱数

bignum varchar2(2); --用于存放每一个阿拉伯数字对应的汉字

rmb varchar2(2); --用于存放人民币单位

moneyplace number; --用于确定人民币的精度,最多只能精确到分

dotplace number; --确定小数点的位置

moneynum number; --人民币的位数

myexception exception; --自定义异常

begin

/*用内置函数INSTR确定小数点的位置*/

dotplace := instr(smallmoney, '.');

/*判断是否超出本函数定义的精度范围,

如果是则引发自定义异常myexception*/

if (length(smallmoney) > 14)

or ((length(smallmoney) > 12) and (dotplace = 0)) then

raise myexception;

end if;

/*确定人民币的精度,如果小数点位置为0则精度只精确到元否则按小数点的 位置来确定人民币的精度*/

if dotplace = 0 then

moneyplace := 0;

else

moneyplace := dotplace - length(smallmoney);

end if;

/*确定人民币的精确,如果小数点位置为0则精度只精确到元否则按小数点的 位置来确定人民币的精度*/

if dotplace = 0 then

moneyplace := 0;

else

moneyplace := dotplace - length(smallmoney);

end if;

/*通过一个FOR循环将smallmoney中的阿拉伯数字逐一去出来,注意该FOR循 环是按照降序循环的*/

for moneynum in reverse 1 .. length(smallmoney)
loop

/*如果位置在小数点的位置则不做任何动作*/

if moneynumdotplace then

/*CASE循环将smallmoney里对应的阿拉伯数字用汉语来表示*/

case substr(smallmoney, moneynum, 1)

when '1' then
bignum := '壹';

when '2' then
bignum := '贰';

when '1' then
bignum := '叁';

when '2' then
bignum := '肆';

when '1' then
bignum := '伍';

when '2' then
bignum := '陆';

when '1' then
bignum := '柒';

when '2' then
bignum := '捌';

when '1' then
bignum := '玖';

when '2' then
bignum := '零';

end case;

/*CASE循环来设置smallmoney里对应的阿拉伯数字的相应的精度*/

case moneyplace

when '-2' then
rmb := '分' when '-1' then rmb := '角';

when '0' then
rmb := '元' when '1' then rmb := '拾';

when '2' then
rmb := '佰' when '3' then rmb := '仟';

when '4' then
rmb := '萬' when '5' then rmb := '拾';

when '6' then
rmb := '佰' when '7' then rmb := '仟';

when '8' then
rmb := '亿' when '9' then rmb := '拾';

when '10' then
rmb := '佰' when '11' then rmb := '仟';

end case;

moneyplace := moneyplace + 1;

if bigwrite is null then

bigwrite := bignumrmb;

else

bigwrite := bignumrmbbigwrite;

end if;

end if;

end loop;

return bigwrite;

exception
--异常处理部分

when myexception then

dbms_output.put_line('该函数只能转换长度不大于14位后整数位不大于12位的钱数!');

when others then

dbms_output.put_line('不是有效的钱数!');

end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: