Epic - Decimal Number

Let the user enter a decimal number. Therange allowed is 0.0001 to 0.9999. Only four decimal places are allowed. Theoutput should be an irreducible fraction. E.g.: If the user enters 0.35,the irreducible fraction will be 7/20.

等于找与10000的公约数

def fraction(d)

  x = d*10000

  gcd = findgcd(x,10000)

  (x/gcd).to_i.to_s + '/' + (10000/gcd).to_i.to_s

end



def findgcd(a,b)

  return a if b == 0

  findgcd(b,a%b)

end

 

你可能感兴趣的:(number)