Convert given Float value to equivalent Fraction | Recursive function to return gcd of a and b ; Function to convert the floating values into fraction ; Initialize variables ; Traverse the floating String ; Check if decimal part exist ; Check if recurrence sequence exist ; Retrieve decimal part and recurrence resquence ; Traverse the String ; Convert String to integer ; If no recurrence sequence exist ; Initialize numerator & denominator ; No reccuring term ; Print the result ; If reccuring term exist ; Convert reccuring term to integer ; reccu . size ( ) is num of digit in reccur term ; eq 2 - eq 1 ; Print the result ; Given String str ; Function Call
function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } function findFraction ( s ) { let be_deci = " " , af_deci = " " , reccu = " " ; let x = true , y = false , z = false ; for ( let i = 0 ; i < s . length ; ++ i ) { if ( s [ i ] == ' ' ) { x = false ; y = true ; continue ; } if ( s [ i ] == ' ' ) { z = true ; y = false ; continue ; } if ( x ) be_deci += s [ i ] ; if ( y ) af_deci += s [ i ] ; if ( z ) { for ( ; i < s . length && s [ i ] != ' ' ; ++ i ) reccu += s [ i ] ; break ; } } let num_be_deci = parseInt ( be_deci ) ; let num_af_deci = 0 ; if ( af_deci . length != 0 ) num_af_deci = parseInt ( af_deci ) ; let numr = num_be_deci * Math . pow ( 10 , af_deci . length ) + num_af_deci ; let deno = Math . pow ( 10 , af_deci . length ) ; if ( reccu . length == 0 ) { let gd = __gcd ( numr , deno ) ; document . write ( numr / gd + " " + deno / gd ) ; } else { let reccu_num = parseInt ( reccu ) ; let numr1 = numr * Math . pow ( 10 , reccu . length ) + reccu_num ; let deno1 = deno * Math . pow ( 10 , reccu . length ) ; let res_numr = numr1 - numr , res_deno = deno1 - deno ; let gd = __gcd ( res_numr , res_deno ) ; document . write ( res_numr / gd + " " + res_deno / gd ) ; } } let str = " " ; findFraction ( str ) ;