Re: 3/4 to .75

From: Eric C. Janzen <ejanzen_at_telusplanet.net>
Date: Thu, 24 Feb 2000 16:14:58 GMT
Message-ID: <6Oct4.7732$pn1.272860_at_news0.telusplanet.net>


Jeff,

The problem is that the slash "/" is being interpreted as a string and not as a mathematical function simply because it is being passed as part of a character string. What you have to do it parse the string to remove the numerical portions, then perform the math. You could create a function that would do this, lets call it:
fraction_to_decimal

CREATE OR REPLACE FUNCTION FRACTION_TO_DECIMAL(i_fraction IN VARCHAR2) RETURN NUMBER IS

  v_numerator   NUMBER;
  v_denominator NUMBER;
  v_decimal     NUMBER;

BEGIN
  v_numerator   := substr(i_fraction,1,instr(i_fraction,'/')-1);
  v_denominator := substr(i_fraction,instr(i_fraction,'/')+1);
  v_decimal     := v_numerator / v_denominator;
  RETURN v_decimal;
END;
/

You can now call this when you want to convert. Test it like so: select fraction_to_decimal('3/4') from dual; Returns 0.75

select fraction_to_decimal('56/48') from dual; Returns 1.166666667

Hope this helps.



Eric Janzen
E. Janzen Consulting Inc.
Oracle Development Specialist
www.telusplanet.net/public/ejanzen

Jeff Smelser <tradergt_at_bigfoot.com> wrote in message news:Pine.LNX.4.10.10002240928100.670-100000_at_server.smelser.org...
> I am trying to convert 3/4 to .75.. Its harder than it seems, or at least
> for me! :)
>
> When I do select to_number(3/4) from dual, it works. Add ' ' around the
> 3/4 and no go. Problem is, I am receiinge all these fractions as
> characters, and can't find a way to convert it to number as decimal..
>
> Thanks in advance,
> Jeff
>
>
Received on Thu Feb 24 2000 - 17:14:58 CET

Original text of this message