parentheses - Can I use parenthesis to encapsulate information I want to convert into a string? (ruby) -
i want divide 2 integers , convert result string. have done putting division parentheses in attempt convert result of division string, instead of denominator. there don't seem errors way wanted double check proper syntax.
note:@numer , @denom both integers.
def redfrac gcd = @numer.gcd(@denom) if @denom != 0 rednumer = (@numer/gcd).to_s reddenom = (@denom/gcd).to_s if reddenom == "1" puts rednumer else puts rednumer + "/" + reddenom end else puts "cannot divide 0" end end
> (1.0 / 4.0).to_s => "0.25"
Comments
Post a Comment