title: Lowest fraction Value In Oracle
body: <p>I am trying to create a function to return lowest fraction value. the sample code is here : </p>
  
  <pre><code>create or replace function fraction_sh(x number) return varchar2
  is
      fra1 number;
      pwr number;
      intprt number;
      v4 number;
      numer number;
      denom number;
      gcdval number;
      frac varchar2(50);
      begin
         if x &lt;&gt; 0 then  
             fra1 := mod(x,1);
             pwr := length(mod(x,1))-1;
             intprt := trunc(x);
             numer :=mod(x,1)*power(10,length(mod(x,1))-1);
             denom :=power(10,length(mod(x,1))-1);
             gcdval := gcdnew(power(10,length(mod(x,1))-1),mod(x,1)*power(10,length(mod(x,1))-1));
                 if intprt = 0 then 
                         frac := to_char(trunc(numer/gcdval))||'/'||to_char(trunc(denom/gcdval));
                  DBMS_OUTPUT.put_line(1||' '||denom||' '||gcdval||' '||numer);
                else
                      frac := (intprt*to_char(trunc(denom/gcdval)))+to_char(trunc(numer/gcdval))||'/'||to_char(trunc(denom/gcdval));
                     DBMS_OUTPUT.put_line(2||' '||denom||' '||gcdval||' '||numer);
                end if;
          end if;
       return frac;
  end;
  
  
  create or replace function gcdnew (a number, b number, p_precision number default null, orig_larger_num number default null) return number is
  v_orig_larger_num number := greatest(nvl(orig_larger_num,-1),a,b);
  v_precision_level number := p_precision;
  begin
    if a is null or b is null or (a = 0 and b = 0) then return 1; end if;
  
    if p_precision is null or p_precision &lt;= 0 then
        v_precision_level := 4; 
    end if;
  
    if b is null or b = 0 or (b/v_orig_larger_num &lt;= power(10,-1*v_precision_level) and greatest(a,b) &lt;&gt; v_orig_larger_num) then
        return a;
    else
        return (gcdnew(b,mod(a,b),v_precision_level,v_orig_larger_num));
    end if;
  </code></pre>
  
  <p>end;</p>
  
  <p>Inmost cases it works, but when i try to pass 2/11 it returns 2/10.</p>
  
  <p>Any help appreciated.</p>