Login
`
Templates, Tools and Utilities
|
||
Add a comment to an Icetips ArticlePlease add your comments to this article.
Please note that you must provide both a name and a valid email address in order
for us to publish your comment. Comments are moderated and are not visible until they have been approved. Spam is never approved!
Back to article list Search Articles Add Comment Printer friendly Direct link 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)
Today is November 21, 2024, 7:15 am This article has been viewed 35241 times.
|
|