Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Rational#to_f

rat.to_f → float

Returns the value as a Float.

Example
Rational(2).to_f      #=> 2.0
Rational(9, 4).to_f   #=> 2.25
Rational(-3, 4).to_f  #=> -0.75
Rational(20, 3).to_f  #=> 6.666666666666667
See also #to_d #to_i #round #to_json also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0