`
Par2: Decimal to Fraction 2004-05-03 -- Geoff Robinson VitCreateFraction FUNCTION (p:Str) ! Written by Geoff Robinson, Vitesse Information Systems P/L, vitesse@mira.net ! Date: 4 May 2004 ! - Assumes passed string holds ONLY digits and decimal point ! Feel free to use at own risk! L:Len LONG,AUTO temp LONG,AUTO u LONG,AUTO v LONG,AUTO Numerator LONG Denominator LONG,AUTO WholeNumber STRING(20) CODE p:Str = left(p:Str) L:Len = len(clip(p:Str)) if ~L:Len then return(''). !blank string passed temp = instring('.',p:Str,1,1) !find the decimal point - some countries may use a comma instead... if temp > 1 then WholeNumber = p:Str[1 : temp - 1]. ! get whole number if temp and temp < L:Len Numerator = p:Str[temp + 1 : L:Len] ! get decimal fraction digits for the numerator end if ~Numerator then return(clip(WholeNumber)). ! no decimal fraction to work with eg. '1.000' Denominator = '1' & all('0',L:Len - temp) ! get the initial denominator v = Numerator u = Denominator !get the greatest common divisor based on Euclid's algorithm LOOP u = u % v if ~u then break. temp = u u = v v = temp END!loop return(choose(WholeNumber <> 0,clip(WholeNumber) & ' ','') & Numerator/v & '/' & Denominator/v) Printed November 23, 2024, 10:15 pm This article has been viewed/printed 35245 times. |