plsql小写金额转大写金额函数


  1. create or replace function comm.F_upper_money(p_num in number default null)
  2.   return nvarchar2 is
  3.   /*Ver:1.0 Created By xsb on 2003-8-18 For:
  4.   将金额数字(单位元)转换为大写(采用从低至高算法)
  5.   数字整数部分不得超过16位,可以是负数。
  6.   Ver:1.1 Modified By xsb on 2003-8-20 For:个位数处理也放在For循环中。
  7.   Ver:1.2 Modified By xsb on 2003-8-22 For:分后不带整字。
  8.   Ver:1.3 Modified By xsb on 2003-8-28 For:完善测试用例。
  9.   测试用例:
  10.   SET HEAD OFF
  11.   SET FEED OFF
  12.   select '无参数时='||f_upper_money() from dual;
  13.   select 'null='||f_upper_money(null) from dual;
  14.   select '0='||f_upper_money(0) from dual;
  15.   select '0.01='||f_upper_money(0.01) from dual;
  16.   select '0.126='||f_upper_money(0.126) from dual;
  17.   select '01.234='||f_upper_money(01.234) from dual;
  18.   select '10='||f_upper_money(10) from dual;
  19.   select '100.1='||f_upper_money(100.1) from dual;
  20.   select '100.01='||f_upper_money(100.01) from dual;
  21.   select '10000='||f_upper_money(10000) from dual;
  22.   select '10012.12='||f_upper_money(10012.12) from dual;
  23.   select '20000020.01='||f_upper_money(20000020.01) from dual;
  24.   select '3040506708.901='||f_upper_money(3040506708.901) from dual;
  25.   select '40005006078.001='||f_upper_money(40005006078.001) from dual;
  26.   select '-123456789.98='||f_upper_money(-123456789.98) from dual;
  27.   select '123456789123456789.89='||f_upper_money(123456789123456789.89) from dual;

  28.   */
  29.   Result nvarchar2(100); --返回字符串
  30.   num_round nvarchar2(100) := to_char(abs(round(p_num, 2))); --转换数字为小数点后2位的字符(正数)
  31.   num_left nvarchar2(100); --小数点左边的数字
  32.   num_right nvarchar2(2); --小数点右边的数字
  33.   str1 nchar(10) := '零壹贰叁肆伍陆柒捌玖'; --数字大写
  34.   str2 nchar(16) := '元拾佰仟万拾佰仟亿拾佰仟万拾佰仟'; --数字位数(从低至高)
  35.   num_pre number(1) := 1; --前一位上的数字
  36.   num_current number(1); --当前位上的数字
  37.   num_count number := 0; --当前数字位数

  38. begin
  39.   if p_num is null then
  40.     return null;
  41.   end if; --转换数字为null时返回null

  42.   select to_char(nvl(substr(to_char(num_round),
  43.                             1,
  44.                             decode(instr(to_char(num_round), '.'),
  45.                                    0,
  46.                                    length(num_round),
  47.                                    instr(to_char(num_round), '.') - 1)),
  48.                      0))
  49.     into num_left
  50.     from dual; --取得小数点左边的数字
  51.   select substr(to_char(num_round),
  52.                 decode(instr(to_char(num_round), '.'),
  53.                        0,
  54.                        length(num_round) + 1,
  55.                        instr(to_char(num_round), '.') + 1),
  56.                 2)
  57.     into num_right
  58.     from dual; --取得小数点右边的数字

  59.   if length(num_left) > 16 then
  60.     return '**********';
  61.   end if; --数字整数部分超过16位时

  62.   --采用从低至高的算法,先处理小数点右边的数字
  63.   if length(num_right) = 2 then
  64.     if to_number(substr(num_right, 1, 1)) = 0 then
  65.       result := '零' ||
  66.                 substr(str1, to_number(substr(num_right, 2, 1)) + 1, 1) || '分';
  67.     else
  68.       result := substr(str1, to_number(substr(num_right, 1, 1)) + 1, 1) || '角' ||
  69.                 substr(str1, to_number(substr(num_right, 2, 1)) + 1, 1) || '分';
  70.     end if;
  71.   elsif length(num_right) = 1 then
  72.     result := substr(str1, to_number(substr(num_right, 1, 1)) + 1, 1) || '角整';
  73.   else
  74.     result := '整';
  75.   end if;
  76.   --再处理小数点左边的数字
  77.   for i in reverse 1 .. length(num_left) loop
  78.     --(从低至高)
  79.     num_count := num_count + 1; --当前数字位数
  80.     num_current := to_number(substr(num_left, i, 1)); --当前位上的数字
  81.     if num_current > 0 then
  82.       --当前位上数字不为0按正常处理
  83.       result := substr(str1, num_current + 1, 1) ||
  84.                 substr(str2, num_count, 1) || result;
  85.     else
  86.       --当前位上数字为0时
  87.       if mod(num_count - 1, 4) = 0 then
  88.         --当前位是元、万或亿时
  89.         result := substr(str2, num_count, 1) || result;
  90.         num_pre := 0; --元、万,亿前不准加零
  91.       end if;
  92.       if num_pre > 0 or length(num_left) = 1 then
  93.         --上一位数字不为0或只有个位时
  94.         result := substr(str1, num_current + 1, 1) || result;
  95.       end if;
  96.     end if;
  97.     num_pre := num_current;
  98.   end loop;

  99.   if p_num < 0 then
  100.     --转换数字是负数时
  101.     result := '负' || result;
  102.   end if;

  103.   return Result;

  104. exception
  105.   when others then
  106.     raise_application_error(-20001, '数字转换大写出现错误!' || sqlerrm);
  107. end;

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28878983/viewspace-2133889/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28878983/viewspace-2133889/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值