# Need assistance to Convert Decimal to fraction in cobol

Author Message
SENTHIL MURUGAAN
Warnings : 1

New User

Joined: 12 Jan 2013
Posts: 32
Location: India

 Posted: Fri Oct 16, 2015 12:57 pm Hi, Need help to convert decimal value to fraction in cobol. Input File only holds only the below values file Record Length=80,FB Input File: 1.3333 0.25 The output file should hold in below format Output File: 4/3 1/4 Is this possible in cobol? Thanks, Senthil
Nic Clouston

Global Moderator

Joined: 10 May 2007
Posts: 2429
Location: Hampshire, UK

 Posted: Fri Oct 16, 2015 4:31 pm If you have a method for defining the divisor then yes.
Bill Woodger

Moderator Emeritus

Joined: 09 Mar 2011
Posts: 7312
Location: Inside the Matrix

 Posted: Fri Oct 16, 2015 4:54 pm You'd better post some more representative examples. Are you looking for things like 90/89? Or will they all be thirds, and quarters and fifths, and halves?
enrico-sorichetti

Senior Member

Joined: 14 Mar 2007
Posts: 10695
Location: italy

Posted: Fri Oct 16, 2015 5:35 pm

for non periodic numbers the algorithm is pretty simple ...

 Code: #! /usr/bin/rexx /* REXX */ Trace "O" numeric digits 32 signal on novalue f.1 = 1.25 f.2 = 0.25 f.0 = 2 do f = 1 to f.0     parse var f.f i "." d     p = length(d)     n = i || d     d = 10 ** p     rn = n / gcd(n,d)     rd = d / gcd(n,d)     say "numerator="rn", denominator="rd end exit /*  error handlers */ logic_error: say "++"copies(" -",35) say "++ Logic error at line '"sigl"' " say "++"copies(" -",35) exit novalue: say "++"copies(" -",35) say "++ Novalue trapped, line '"sigl"' var '"condition("D")"' " say "++"copies(" -",35) exit gcd: procedure     parse arg x, y     if  y > x then do         r = x; x = y; y = r     end     do  until r = 0         r = x // y; x = y; y = r     end     return x

for a periodic number

 Code: #! /usr/bin/rexx /* REXX */ Trace "O" numeric digits 32 signal on novalue n = 1.333333333 parse var n i "."  f p = lrs(f) say n i f p say length(f) say length(p) d   = copies("9", length(p) ) n   = i*d + p rn = n / gcd(n,d) rd = d / gcd(n,d) say "numerator="rn", denominator="rd exit /*  error handlers */ logic_error: say "++"copies(" -",35) say "++ Logic error at line '"sigl"' " say "++"copies(" -",35) exit novalue: say "++"copies(" -",35) say "++ Novalue trapped, line '"sigl"' var '"condition("D")"' " say "++"copies(" -",35) exit /*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ lrs:procedure     use strict arg s     m = s~length()     k   = 0     L   = .array~new     r   = ""     do  i = 1 to m         do  j = i + 1 to m             if substr(s,i,1) = substr(s,j,1) then do                 if  i = 1 then ,                     L[i,j] = 1                 else do                     if  j-i > L[i-1,j-1] then do                         L[i,j]  = L[i-1,j-1] + 1                         if  L[i,j] > k then do                             k   = L[i,j]                             r   = substr(s,i-k+1,k)                         end                     end                     else do                         L[i,j]  = 0                     end                 end             end             else do                 L[i,j]  = 0             end         end /* do  j = i + 1 to m */     end /* do  i = 1 to m */     return r /*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ gcd: procedure     parse arg x, y     if  y > x then do         r = x; x = y; y = r     end     do  until r = 0         r = x // y         x = y         y = r     end     say x y r     return x

quick and dirty just to show the algorithms.
enrico-sorichetti

Senior Member

Joined: 14 Mar 2007
Posts: 10695
Location: italy

 Posted: Sat Oct 17, 2015 2:36 am a bit of explanation of the algorithms ... for normal ( non periodic ) just find out how many decimal digits 1.2345 ==> 4 decimal digits 12345/10000 and simplify the fraction for a periodic number things are a bit trickier to find the period I wrote a the LRS ( longest repeated substring )subroutine after that the fraction for the period is numerator the period, denominator as may nines as the period length 0.33333333 3/9 ==> 1/3 if there is an integer part period/(9....9) + integer_part and after that simplify the fraction 1.333333 3/9 + 1 ==> (3+9)/9 =>> 4/3
prino

Senior Member

Joined: 07 Feb 2009
Posts: 1230
Location: Vilnius, Lithuania

 Posted: Sat Oct 17, 2015 5:43 am Is this a business requirement? No, it's my homework...
 All times are GMT + 6 Hours
 Page 1 of 1

Search our Forum:

 Topic Forum Replies Similar Topics JCL for replacing code in Cobol JCL & VSAM 8 COBOL VS SORT Utility for file format... COBOL Programming 6 XML Parsing in COBOL creating "h... COBOL Programming 0 Calling Rexx program from Cobol CLIST & REXX 12 Need assistance formatting when joini... SYNCSORT 8