Skip to content
instance method

Float#rationalize

(?::Numeric eps) -> ::Rational
flt.rationalize([eps]) → rational

Returns a simpler approximation of the value (flt-|eps| <= result <= flt+|eps|). If the optional argument eps is not given, it will be chosen automatically.

Example
0.3.rationalize          #=> (3/10)
1.333.rationalize        #=> (1333/1000)
1.333.rationalize(0.01)  #=> (4/3)

See also Float#to_r.

See also #quo #round #prev_float #to_f also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0