round(p1 = v1, p2 = {})
public
Rounds int to a given precision in decimal digits (default 0 digits).
Precision may be negative. Returns a
floating point number when ndigits is positive, self for
zero, and round down for negative.
1.round
1.round(2)
15.round(-1)
Show source
static VALUE
int_round(int argc, VALUE* argv, VALUE num)
{
int ndigits;
int mode;
VALUE nd, opt;
if (!rb_scan_args(argc, argv, "01:", &nd, &opt)) return num;
ndigits = NUM2INT(nd);
mode = rb_num_get_rounding_option(opt);
if (ndigits > 0) {
return rb_Float(num);
}
if (ndigits == 0) {
return num;
}
return rb_int_round(num, ndigits, mode);
}