百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程字典 > 正文

Oracle实现金额小写转大写函数

toyiye 2024-06-21 12:26 8 浏览 0 评论

函数代码:

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

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

begin
if p_num is null then return null;end if;--转换数字为null时返回null

select to_char(
nvl(substr(to_char(num_round),1,
decode(instr(to_char(num_round),'.'),0,
length(num_round),instr(to_char(num_round),'.')-1)),
0)) into num_left from dual;--取得小数点左边的数字
select substr(to_char(num_round),
decode(instr(to_char(num_round),'.'),0,
length(num_round)+1,instr(to_char(num_round),'.')+1),2)
into num_right from dual;--取得小数点右边的数字

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

  --采用从低至高的算法,先处理小数点右边的数字
  if length(num_right)=2 then
    if to_number(substr(num_right,1,1))=0 then
     result:='零'||substr(str1,to_number(substr(num_right,2,1))+1,1)||'分';
    else
     result:=substr(str1,to_number(substr(num_right,1,1))+1,1)||'角'||
             substr(str1,to_number(substr(num_right,2,1))+1,1)||'分';
    end if;           
  elsif length(num_right)=1 then
    result:=substr(str1,to_number(substr(num_right,1,1))+1,1)||'角整';
  else  
    result :='整';
  end if;
  --再处理小数点左边的数字
  for i in reverse 1..length(num_left)  loop --(从低至高)
      num_count:=num_count+1;--当前数字位数
      num_current:=to_number(substr(num_left,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_left)=1 then  --上一位数字不为0或只有个位时
              result:=substr(str1,num_current+1,1)||result;
          end if;        
       end if;
       num_pre:=num_current;
  end loop;
  
  if p_num<0 then --转换数字是负数时
     result:='负'||result;
  end if;
  
  return Result;
  
  exception
  when others  then
  raise_application_error(-20001,'数字转换大写出现错误!'||sqlerrm);
end ;

测试用例:

-- 无参数,结果为:null=
select 'null='||f_upper_money(null) total from dual;

-- 0=零元整
select '0='||f_upper_money(0) total from dual;

-- 0.05=零元零伍分
select '0.05='||f_upper_money(0.05) total from dual;

-- 0.155=零元壹角陆分
select '0.155='||f_upper_money(0.155) total from dual;

-- 01.125=壹元壹角参分
select '01.125='||f_upper_money(01.125) total from dual;

-- 10=壹拾元整
select '10='||f_upper_money(10) total from dual;

-- 100.5=壹佰元伍角整
select '100.5='||f_upper_money(100.5) total from dual;

-- 100.05=壹佰元零伍分
select '100.05='||f_upper_money(100.05) from dual;

-- 10000=壹万元整
select '10000='||f_upper_money(10000) from dual;

-- 10025.52=壹万零贰拾伍元伍角贰分
select '10025.52='||f_upper_money(10025.52) from dual;

-- 50000020.05=伍仟万零贰拾元零伍分
select '50000020.05='||f_upper_money(50000020.05) from dual;

-- 5040302105.501=伍拾亿肆仟零参拾万贰仟壹佰零伍元伍角整
select '5040302105.501='||f_upper_money(5040302105.501) from dual;

-- 50002001075.001=伍佰亿零贰佰万壹仟零柒拾伍元整
select '50002001075.001='||f_upper_money(50002001075.001) from dual;

-- -123456789.15=负壹亿贰仟参佰肆拾伍万陆仟柒佰捌拾玖元壹角伍分
select '-123456789.15='||f_upper_money(-123456789.15) from dual;

-- 123456789123456789.89=**********
select '123456789123456789.89='||f_upper_money(123456789123456789.89) from dual;

相关推荐

蓝牙电话-关联FreeSwitch中继SIP账号通过Rest接口

蓝牙电话-关联FreeSwitch中继SIP账号通过Rest接口前言上一篇章《蓝牙电话-与FreeSwitch服务器和UA坐席的通话.docx》中,我们使用开源的B2B-UA当中经典的FreeSWIT...

技术分享|Sip与WebRTC互通-SRProxy开源库讲解

SRProxy介绍目前WebRTC协议跟SIP协议互通场景主要运用在企业呼叫中心、企业内部通信、电话会议(PSTN)、智能门禁等场景,要想让WebRTC与SIP互通,要解决两个层面的...

全网第N篇SIP协议之GB28181注册 JAVA版本

鉴于网上大部分关于SIP注册服务器编写都是C/C++/python,故开此贴,JAVA实现也贴出分享GB28181定义了了基于SIP架构的视频监控互联规范,而对于多数私有协议实现的监控系统...

「linux专栏」top命令用法详解,再也不怕看不懂top了

在linux系统中,我们经常使用到的一个命令就是top,它主要是用来显示系统运行中所有的进程和进程对应资源的使用等信息,所有的用户都可以使用top命令。top命令内容量丰富,可令使用者头疼的是无法全部...

Linux 中借助 perf 对 php 程序模拟CPU高的案例分析

导语本文是一篇Linux借助工具分析CPU高的优化案例,没有任何干货内容,很详细的展示了优化CPU高的具体步骤,非常适合初中级读者阅读!...

centos漏洞处理方法(centos podman)

centos服务器最近有诸多漏洞,修复命令及对应的漏洞整理后,分享给大家RHSA-2020:1176-低危:avahi安全更新yumupdateavahi-libsRHSA-2017:326...

Linux上的free命令详解(Buffer和Cache)

解释一下Linux上free命令的输出。下面是free的运行结果,一共有4行。为了方便说明,我加上了列号。这样可以把free的输出看成一个二维数组FO(FreeOutput)。例如:FO[2][1]...

linux 命令行之你真的会用吗?--free 基本用法篇

free命令行统计内存使用率及swap交换分区的使用率数据。是由sourceforge负责维护的,在ubuntu上其包名为procps,这个源码包中,除了free还有ps,top,vmstat,ki...

kong api gateway 初体验(konga github)

kongapigateway初体验(firstsight?)。Kong是一个可扩展的开源API层(也称为API网关或API中间件)。Kong运行在任何RESTfulAPI的前面,并通过插件...

在Ubuntu下开启IP转发的方法(ubuntu20 ip)

IP地址分为公有ip地址和私有ip地址,PublicAddress是由INIC(internetnetworkinformationcenter)负责的,这些IP地址分配给了注册并向INIC提...

基于 Kubernetes 的 Serverless PaaS 稳定性建设万字总结

作者:许成铭(竞霄)数字经济的今天,云计算俨然已经作为基础设施融入到人们的日常生活中,稳定性作为云产品的基本要求,研发人员的技术底线,其不仅仅是文档里承诺的几个九的SLA数字,更是与客户切身利益乃...

跟老韩学Ubuntu Linux系列-sysctl 帮助文档

sysctl一般用于基于内核级别的系统调优,man帮助手册如下。...

如何在 Linux/Unix/Windows 中发现隐藏的进程和端口

unhide是一个小巧的网络取证工具,能够发现那些借助rootkit、LKM及其它技术隐藏的进程和TCP/UDP端口。这个工具在Linux、UNIX类、MS-Windows等操作系统下都...

跟老韩学Ubuntu Server 2204-Linux性能管理-uptime指令帮助手册

uptime指令是每个从事Linux系统工作的相关同学必知必会的指令之一,如下是uptime指令的帮助手册。UPTIME(1)...

Openwrt+Rclone+emby+KODI搭建完美家庭影音服务器

特别声明:本篇内容参考了波仔分享,在此表示感谢!上一篇《Openwrt+emby+KODI搭建家庭影音服务器》只适用影音下载到本地的情形,不能播放云盘中的影音,内容较少,缺少了趣味性,也不直观。...

取消回复欢迎 发表评论:

请填写验证码