What is all about oracle decimal fraction conversion?

Asked By 20 points N/A Posted on -
qa-featured

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.

SHARE
Answered By 0 points N/A #172602

What is all about oracle decimal fraction conversion?

qa-featured

 

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.
 
 
[email protected]> 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.

 

 

[email protected]> 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.

 

Login/Register to Answer

Related Questions