plsql - Function dont return decimal places -


i have oracle function performs calculations, didn't return decimal of result.

create or replace function my_teste    return decimal    begin    return 1/3;  end; 

if executed function

select my_teste    dual 

it return value 0 decimal places. expect return 0.3333 ideas ?

decimal declared in package sys.standard subtype of number (as numeric types in oracle), number(38, 0), means decimal values not able have digits right of decimal point. in order have decimal values numbers right of decimal point have declared such, similar to

dvalue  decimal(38, 4) 

however, won't if want return such value function, because can't have precision specifiers on function return type.

in order have decimal value digits right of decimal place returned function need declare subtype of decimal specifies correct number of decimal places , use subtype return type of function. here's example:

declare    subtype dec_10_3 decimal(10, 3);    function test1 return decimal       d  decimal := 1/3;   begin     return d;   end test1;    function test2 return decimal       d  decimal(10, 3) := 1/3;   begin     return d;   end test2;    function test3 return dec_10_3     d dec_10_3 := 1/3;   begin     return d;   end test3; begin   -- test statements here   dbms_output.put_line('test1=' || test1);   dbms_output.put_line('test2=' || test2);   dbms_output.put_line('test3=' || test3); end; 

running above produce

test1=0 test2=0 test3=.333 

share , enjoy.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -