金额大小写转换(1)


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> 12and (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;


posted @ 2010-12-31 16:28  郭振斌  阅读(655)  评论(0编辑  收藏  举报