金额大小写转换(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;
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;
相关文章推荐
- Mac FFmpeg x265, aac, ffplay编译安装
- 在VirtualBox上安装CentOS 7
- Android 如何读取本地图片或者资源图片并自定义大小
- java 的wait与sleep
- 区块链在中国(1):IBM HyperLedger fabric
- web前端之锋利的jQuery十一:综合开发,编写购物网首页
- 最大整数(连接多位数)
- 零件分组
- Android 抓包之Fiddler的使用
- 人工智能学习笔记-基本概念
- synchronized关键字的实现原理
- AngularJS路由之ui-router(一)
- Dungeon Master(bfs)
- AngularJS模仿Form表单提交的实现代码
- argc 和 argv 详解与经典实例
- pycharm常用快捷键
- 金额大小写转换(2)
- Http状态码详解
- 4种函数调用方式
- IOS相关笔记