Smallest number to multiply to convert floating point to natural | Finding GCD of two number ; Returns smallest integer k such that k * str becomes natural . str is an input floating point number ; Find size of string representing a floating point number . ; Below is used to find denominator in fraction form . ; Used to find value of count_after_dot ; To find numerator in fraction form of given number . For example , for 30.25 , numerator would be 3025. ; If there was no dot , then number is already a natural . ; Find denominator in fraction form . For example , for 30.25 , denominator is 100 ; Result is denominator divided by GCD - of - numerator - and - denominator . For example , for 30.25 , result is 100 / GCD ( 3025 , 100 ) = 100 / 25 = 4 ; Driver code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function findnum ( str ) { var n = str . length ; var count_after_dot = 0 ; let dot_seen = false ; var num = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) != ' ' ) { num = num * 10 + ( str . charAt ( i ) - ' ' ) ; if ( dot_seen == true ) count_after_dot ++ ; } else dot_seen = true ; } if ( dot_seen == false ) return 1 ; var dem = parseInt ( Math . pow ( 10 , count_after_dot ) ) ; return ( dem / gcd ( num , dem ) ) ; } let str = " " ; document . write ( findnum ( str ) ) ;