Method: BigDecimal#add

Defined in:
bigdecimal.c

#add(value, ndigits) ⇒ Object

Returns the BigDecimal sum of self and value with a precision of ndigits decimal digits.

When ndigits is less than the number of significant digits in the sum, the sum is rounded to that number of digits, according to the current rounding mode; see BigDecimal.mode.

Examples:

# Set the rounding mode.
BigDecimal.mode(BigDecimal::ROUND_MODE, :half_up)
b = BigDecimal('111111.111')
b.add(1, 0)               # => 0.111112111e6
b.add(1, 3)               # => 0.111e6
b.add(1, 6)               # => 0.111112e6
b.add(1, 15)              # => 0.111112111e6
b.add(1.0, 15)            # => 0.111112111e6
b.add(Rational(1, 1), 15) # => 0.111112111e6


2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
# File 'bigdecimal.c', line 2290

static VALUE
BigDecimal_add2(VALUE self, VALUE b, VALUE n)
{
    ENTER(2);
    Real *cv;
    SIGNED_VALUE mx = check_int_precision(n);
    if (mx == 0) return BigDecimal_add(self, b);
    else {
	size_t pl = VpSetPrecLimit(0);
	VALUE   c = BigDecimal_add(self, b);
	VpSetPrecLimit(pl);
	GUARD_OBJ(cv, GetVpValue(c, 1));
	VpLeftRound(cv, VpGetRoundMode(), mx);
        return VpCheckGetValue(cv);
    }
}