Method: Kernel#Float

Defined in:
object.c

#Float(arg, exception: true) ⇒ Float?

Returns arg converted to a float. Numeric types are converted directly, and with exception to String and nil the rest are converted using arg.to_f. Converting a String with invalid characters will result in a ArgumentError. Converting nil generates a TypeError. Exceptions can be suppressed by passing exception: false.

Float(1)                 #=> 1.0
Float("123.456")         #=> 123.456
Float("123.0_badstring") #=> ArgumentError: invalid value for Float(): "123.0_badstring"
Float(nil)               #=> TypeError: can't convert nil into Float
Float("123.0_badstring", exception: false)  #=> nil

Returns:



3772
3773
3774
3775
3776
3777
3778
3779
# File 'object.c', line 3772

static VALUE
rb_f_float(int argc, VALUE *argv, VALUE obj)
{
    VALUE arg = Qnil, opts = Qnil;

    rb_scan_args(argc, argv, "1:", &arg, &opts);
    return rb_convert_to_float(arg, opts_exception_p(opts));
}