Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Integer#to_f
() -> ::Float
to_f → float
numeric.c
View on GitHub ↗
static VALUE
int_to_f(VALUE num)
{
double val;
if (FIXNUM_P(num)) {
val = (double)FIX2LONG(num);
}
else if (RB_BIGNUM_TYPE_P(num)) {
val = rb_big2dbl(num);
}
else {
rb_raise(rb_eNotImpError, "Unknown subclass for to_f: %s", rb_obj_classname(num));
}
return DBL2NUM(val);
}
Converts self to a Float:
Example
1.to_f # => 1.0 -1.to_f # => -1.0
If the value of self does not fit in a Float, the result is infinity:
Example
(10**400).to_f # => Infinity (-10**400).to_f # => -Infinity