对于数字大小写的转换,也是有一定中国特色的东西。但是在账务报表中,我们还是需要有这样的功能的。今天写了一个将数字金额转化为大写的函数。
功能:将任意长度的数字金额转化为大写。***位为圆或角时,后面加“整”;为“分”时不加“整”。
说明:小数点后保留两位。超过两位的部分被截断。
代码:
PL/SQL代码 Create Or Replace Function Money2Chinese(MoneyIn Number)Return Varchar2Is strYuanVarchar2(150); strYuanFenVarchar2(152); numLenYuanNumber; numLenYuanFenNumber; strRstYuanVarchar2(600); strRstFenVarchar2(200); strRstVarchar2(800); TypetypeTabMappingIs Table Of Varchar2(2)Index By Binary_Integer; tabNumMappingtypeTabMapping; tabUnitMappingtypeTabMapping; numUnitIndexNumber; iNumber; jNumber; charCurrentNumChar (1); Begin IfMoneyIs Null Then Return Null ; End If; strYuan:=TO_CHAR(FLOOR(Money)); IfstrYuan='0' Then numLenYuan:=0; strYuanFen:=lpad(TO_CHAR(FLOOR(Money*100)),2,'0' ); Else numLenYuan:=length(strYuan); strYuanFen:=TO_CHAR(FLOOR(Money*100)); End If; IfstrYuanFen='0' Then numLenYuanFen:=0; Else numLenYuanFen:=length(strYuanFen); End If; IfnumLenYuan=0Or numLenYuanFen=0Then strRst:='零圆整' ; Return strRst; End If; tabNumMapping(0):='零' ; tabNumMapping(1):='壹' ; tabNumMapping(2):='贰' ; tabNumMapping(3):='叁' ; tabNumMapping(4):='肆' ; tabNumMapping(5):='伍' ; tabNumMapping(6):='陆' ; tabNumMapping(7):='柒' ; tabNumMapping(8):='捌' ; tabNumMapping(9):='玖' ; tabUnitMapping(-2):='分' ; tabUnitMapping(-1):='角' ; tabUnitMapping(1):='' ; tabUnitMapping(2):='拾' ; tabUnitMapping(3):='佰' ; tabUnitMapping(4):='仟' ; tabUnitMapping(5):='万' ; tabUnitMapping(6):='拾' ; tabUnitMapping(7):='佰' ; tabUnitMapping(8):='仟' ; tabUnitMapping(9):='亿' ; For iIn 1..numLenYuanLoop j:=numLenYuan-i+1; numUnitIndex:=Mod(i,8); IfnumUnitIndex=0Then numUnitIndex:=8; End If; IfnumUnitIndex=1And i>1Then strRstYuan:=tabUnitMapping(9)||strRstYuan; End If; charCurrentNum:=substr(strYuan,j,1); IfcharCurrentNum<>0Then strRstYuan:=tabNumMapping(charCurrentNum)|| tabUnitMapping(numUnitIndex)||strRstYuan; Else If(i=1Or i=5)Then Ifsubstr(strYuan,j-3,4)<>'0000' Then strRstYuan:=tabUnitMapping(numUnitIndex)||strRstYuan; End If; Else Ifsubstr(strYuan,j+1,1)<>'0' Then strRstYuan:=tabNumMapping(charCurrentNum)||strRstYuan; End If; End If; End If; End Loop; For iIn -2..-1Loop j:=numLenYuan-i; charCurrentNum:=substr(strYuanFen,j,1); IfcharCurrentNum<>'0' Then strRstFen:=tabNumMapping(charCurrentNum)||tabUnitMapping(i)|| strRstFen; End If; End Loop; IfstrRstYuanIs Not Null Then strRstYuan:=strRstYuan||'圆' ; End If; IfstrRstFenIs Null Then strRstYuan:=strRstYuan||'整' ; Elsiflength(strRstFen)=2And substr(strRstFen,2)='角' Then strRstFen:=strRstFen||'整' ; End If; strRst:=strRstYuan||strRstFen; --strRst:=Replace(strRst,'亿零','亿'); --strRst:=Replace(strRst,'万零','万'); Return strRst; End Money2Chinese;
测试SQL如下:
Select Money2Chinese(0932402934024.213)From dual;
结果:
玖仟叁佰贰拾肆亿零贰佰玖拾叁万肆仟零贰拾肆圆贰角整
测试通过
相关文章
标签:Oracle