oracle裡儲存函式將金額數字轉換成大寫

2021-09-29 04:28:13 字數 4393 閱讀 1262

create or replace function number_to_chinese(p_num in number default null)

return nvarchar2 is

/*ver:1.0 created by xsb on 2003-8-18 for:

將金額數字(單位元)轉換為大寫(採用從低至高演算法)

數字整數部分不得超過16位,可以是負數。

個位數處理也放在for迴圈中。

分後不帶整字。

完善測試用例。

測試用例:

select '無引數時='||number_to_chinese() from dual;

select 'null='||number_to_chinese(null) from dual;

select '0='||number_to_chinese(0) from dual;

select '0.01='||number_to_chinese(0.01) from dual;

select '0.126='||number_to_chinese(0.126) from dual;

select '01.234='||number_to_chinese(01.234) from dual;

select '10='||number_to_chinese(10) from dual;

select '100.1='||number_to_chinese(100.1) from dual;

select '100.01='||number_to_chinese(100.01) from dual;

select '10000='||number_to_chinese(10000) from dual;

select '10012.12='||number_to_chinese(10012.12) from dual;

select '20000020.01='||number_to_chinese(20000020.01) from dual;

select '3040506708.901='||number_to_chinese(3040506708.901) from dual;

select '40005006078.001='||number_to_chinese(40005006078.001) from dual;

select '-123456789.98='||number_to_chinese(-123456789.98) from dual;

select '123456789123456789.89='||number_to_chinese(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; --當前數字位數

num1        number;

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; --取得小數點右邊的數字

select case

when length(num_left) >= 8 then

to_number(substr(to_char(num_left), -8, 4))

else

to_number(substr(to_char(num_left),

-length(num_left),

length(num_left) - 4))

endinto num1

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 num_count = 5 then

if mod(num_count - 1, 4) = 0 and num1 <> 0 then

result  := substr(str2, num_count, 1) || result;

num_pre := 0; --元、萬,億前不准加零 --當前位是元、萬或億時

end if;

else

if mod(num_count - 1, 4) = 0 then

result  := substr(str2, num_count, 1) || result;

num_pre := 0; --元、萬,億前不准加零

end if;

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;

人民幣金額列印,將金額數字轉換為漢字描述。

題目描述 銀行在列印票據的時候,常常需要將阿拉伯數字表示的人民幣金額轉換為大寫表示,現在請你來完成這樣乙個程式。在中文大寫方式中,0到10以及100 1000 10000被依次表示為 零 壹 貳 叄 肆 伍 陸 柒 捌 玖 拾 佰 仟 萬 以下的例子示範了阿拉伯數字到人民幣大寫的轉換規則 1 壹圓 ...

PHP實現金額數字轉換成大寫函式

header content type text html charset utf 8 function num to upper num elseif len pointdigit 2 else for i 0 i c i for j len pointdigit j 1 j chinses st...

Oracle 儲存函式

查詢指定員工年薪 create orreplace function func getsal vempno number return number is 宣告變數,儲存年薪 vnum number begin 根據傳入值vempno查詢年薪,並賦值給變數vnum nvl進行獎金判斷是否為空 sel...