round(p1 = v1, p2 = v2)
public
Round to the nearest integer (by default), returning the result as a BigDecimal.
BigDecimal('3.14159').round
BigDecimal('8.7').round
BigDecimal('-9.9').round
If n is specified and positive, the fractional part of the result has no
more than that many digits.
If n is specified and negative, at least that many digits to the left of
the decimal point will be 0 in the result.
BigDecimal('3.14159').round(3)
BigDecimal('13345.234').round(-2)
The value of the optional mode
argument can be used to determine how rounding is performed; see
BigDecimal.mode.
static VALUE
BigDecimal_round(int argc, VALUE *argv, VALUE self)
{
ENTER(5);
Real *c, *a;
int iLoc = 0;
VALUE vLoc;
VALUE vRound;
size_t mx, pl;
unsigned short sw = VpGetRoundMode();
switch (rb_scan_args(argc, argv, "02", &vLoc, &vRound)) {
case 0:
iLoc = 0;
break;
case 1:
if (RB_TYPE_P(vLoc, T_HASH)) {
sw = check_rounding_mode_option(vLoc);
}
else {
iLoc = NUM2INT(vLoc);
}
break;
case 2:
iLoc = NUM2INT(vLoc);
if (RB_TYPE_P(vRound, T_HASH)) {
sw = check_rounding_mode_option(vRound);
}
else {
sw = check_rounding_mode(vRound);
}
break;
default:
break;
}
pl = VpSetPrecLimit(0);
GUARD_OBJ(a, GetVpValue(self, 1));
mx = a->Prec * (VpBaseFig() + 1);
GUARD_OBJ(c, VpCreateRbObject(mx, "0"));
VpSetPrecLimit(pl);
VpActiveRound(c, a, sw, iLoc);
if (argc == 0) {
return BigDecimal_to_i(ToValue(c));
}
return ToValue(c);
}