Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

Official content
Converts self to a Float:
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:
(10**400).to_f  # => Infinity
(-10**400).to_f # => -Infinity
 
               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);
}
            

Was this page useful?