Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: conveting decimal to a fraction.

Re: conveting decimal to a fraction.

From: Michel Cadot <micadot_at_altern.org>
Date: Fri, 13 Aug 1999 17:14:53 +0200
Message-ID: <7p1cqn$ctc$1@oceanite.cybercable.fr>


Here's a function to convert your decimal number into fractionnal number. The first parameter is the number to convert. The second the result you want: pure fraction or integer+fraction.

create or replace function to_fraction (

     value number,
     restype integer default 1)

return varchar2
is
   /* -------------------------------------------------------------

        Converts a decimal number to a fractionnal number string
        according to the second parameter :
        1 : pure fractionnal number
        2 : integer + fraction less than 1

        Examples :
        to_fraction (1.75, 1) => '7/4'
        to_fraction (1.75, 2) => '1 3/4'

      ------------------------------------------------------------- */

   result varchar2(50) := '';

   tempo  integer;
   pow    integer;
   gcd    integer;

   function myGCDloop (value1 integer, value2 integer)    return integer
   /* Computes the GCD of value1 and value2 */    is

      big   integer;
      small integer;
      rest integer;
   begin
      big   := greatest (value1, value2);
      small := least (value1, value2);
      rest  := mod (big, small);
      while ( rest <> 0 ) loop
         big   := small;
         small := rest;
         rest  := mod (big, small);
      end loop;
      return small;

   end;

   function myGCDrecursive (value1 integer, value2 integer)    return integer
   /* Computes the GCD of value1 and value2 */    is

      big   integer;
      small integer;
   begin
      big := greatest (value1, value2);
      small := least (value1, value2);
      if ( small = 0 ) then
         return big;
      else
         return myGCDrecursive (small, mod (big, small));
      end if;

   end;

begin

   if ( value = trunc(value) ) then

      return value;

   else

      pow := power (10,
                    length(to_char(value)) - instr(to_char(value),'.'));
      tempo := value * pow;
      gcd := myGCDloop (tempo, pow);
      tempo := pow / gcd;

      if ( restype = 1 ) then
         result := value * tempo || '/' || tempo;
      elsif ( restype = 2 ) then
         if ( value >= 1 ) then
            result := floor (value) || ' ';
         end if;
         result := result || (value-trunc(value))*tempo || '/' || tempo;
      end if;

   end if;

   return result;

end;
/

siddsoni_at_hotmail.com a écrit dans le message <7ov8gu$k52$1_at_nnrp1.deja.com>...
>Is there an easy way to convert, say 1.75 to display as "1 3/4"
>
>I know I can multiply by 10 until its a whole number.
>and then take the GCD of:
>175 and (10^num_times_multiplied)
>
>175/100 = 7/4
>then use mod and div to concat a string of the fraction
>
>
>Thanks for any help
>Or post the code if youve already got it.
>
>
>Sent via Deja.com http://www.deja.com/
>Share what you know. Learn what you don't.
Received on Fri Aug 13 1999 - 10:14:53 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US