Hello fellows,
What is all about oracle decimal fraction conversion? Can an oracle database handle some calculations such as decimal fraction conversion? Or can it save some fractional form numbers? I am planning to use oracle for my system which includes fractional calculations and conversion. I hope you can give some suggestion.
Regards,
Martha Nelson.
What is all about oracle decimal fraction conversion?
Â
Hi Martha,
Â
You can make conversion from decimal to fraction using oracle.
You have just to create the function to_fraction, as shown below, which uses the function gcd also listed below.
Â
In total, you have to create these two functions and the work is done.
Â
Â
test@ORA10G> create or replace function gcd(a number, b number)
 2 return number
 3 as
 4 begin
 5    if if nvl(b,0) = 0 then
 6       return a;
 7    else
 8       return gcd(b,mod(a,b));
 9    end if;
10Â end;
11Â /
Â
Function created.
Â
Â
test@ORA10G> create or replace function to_fraction( p_number in number )
 2 return varchar2
 3 as
 4         l_gcd number;
 5         l_int integer;
 6         l_fract_str varchar2(30);
 7         l_numerator number;
 8         l_denominator number;
 9 begin
10Â Â Â Â Â Â Â Â Â l_int := trunc(p_number);
11Â Â Â Â Â Â Â Â Â l_fract_str := rtrim( to_char( p_number - l_int, 'fmv999999999' ), '0' );
12
13Â Â Â Â Â Â Â Â Â l_numerator := to_number( l_fract_str );
14Â Â Â Â Â Â Â Â Â l_denominator := to_number( power(10,length(l_fract_str)) );
15
16Â Â Â Â Â Â Â Â Â l_gcd := gcd( l_numerator, l_denominator );
17
18Â Â Â Â Â Â Â Â Â return to_char( l_int ) || ' ' || l_numerator / l_gcd || '/' ||
l_denominator / l_gcd;
19Â end;
20Â /
Â
Function created.
Â
Â
I hope this suggestion solves your problem.
Â
Best Regards.
Â