Convert given Float value to equivalent Fraction | C # program for the above approach ; 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 . Count is num of digit in reccur term ; eq 2 - eq 1 ; Print the result ; Driver Code ; Given String str ; Function Call
using System ; class GFG { static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static void findFraction ( String s ) { String be_deci = " " , af_deci = " " , reccu = " " ; bool x = true , y = false , z = false ; for ( int 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 ; } } int num_be_deci = Int32 . Parse ( be_deci ) ; int num_af_deci = 0 ; if ( af_deci . Length != 0 ) num_af_deci = Int32 . Parse ( af_deci ) ; int numr = num_be_deci * ( int ) Math . Pow ( 10 , af_deci . Length ) + num_af_deci ; int deno = ( int ) Math . Pow ( 10 , af_deci . Length ) ; if ( reccu . Length == 0 ) { int gd = __gcd ( numr , deno ) ; Console . Write ( numr / gd + " ▁ / ▁ " + deno / gd ) ; } else { int reccu_num = Int32 . Parse ( reccu ) ; int numr1 = numr * ( int ) Math . Pow ( 10 , reccu . Length ) + reccu_num ; int deno1 = deno * ( int ) Math . Pow ( 10 , reccu . Length ) ; int res_numr = numr1 - numr , res_deno = deno1 - deno ; int gd = __gcd ( res_numr , res_deno ) ; Console . Write ( res_numr / gd + " ▁ / ▁ " + res_deno / gd ) ; } } public static void Main ( String [ ] args ) { String str = "23.98(231 ) " ; findFraction ( str ) ; } }