Flowdock
round(p1) public

No documentation

This method has no description. You can help the Ruby community by adding new notes.

Hide source
static VALUE
int_round(int argc, VALUE* argv, VALUE num)
{
    VALUE n, f, h, r;
    int ndigits;

    if (argc == 0) return num;
    rb_scan_args(argc, argv, "1", &n);
    ndigits = NUM2INT(n);
    if (ndigits > 0) {
        return rb_Float(num);
    }
    if (ndigits == 0) {
        return num;
    }
    ndigits = -ndigits;
    if (ndigits < 0) {
        rb_raise(rb_eArgError, "ndigits out of range");
    }
    f = int_pow(10, ndigits);
    if (FIXNUM_P(num) && FIXNUM_P(f)) {
        SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f);
        int neg = x < 0;
        if (neg) x = -x;
        x = (x + y / 2) / y * y;
        if (neg) x = -x;
        return LONG2NUM(x);
    }
    h = rb_funcall(f, '/', 1, INT2FIX(2));
    r = rb_funcall(num, '%', 1, f);
    n = rb_funcall(num, '-', 1, r);
    if (!RTEST(rb_funcall(r, '<', 1, h))) {
        n = rb_funcall(n, '+', 1, f);
    }
    return n;
}
Register or log in to add new notes.