Skip to content

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

instance method

Complex#to_f

cmp.to_f → float

Returns the value as a float if possible (the imaginary part should be exactly zero).

Example
Complex(1, 0).to_f    #=> 1.0
Complex(1, 0.0).to_f  # RangeError
Complex(1, 2).to_f    # RangeError
See also #to_d #to_i #to_c #to_json also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1