Method: BigDecimal#truncate

Defined in:
bigdecimal.c

#truncateObject

truncate(n)

Truncate to the nearest 1, returning the result as a BigDecimal.

BigDecimal('3.14159').truncate -> 3

BigDecimal('8.7').truncate -> 8

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').truncate(3) -> 3.141

BigDecimal('13345.234').truncate(-2) -> 13300.0



# File 'bigdecimal.c'

/* call-seq:
 * truncate(n)
 *
 * Truncate to the nearest 1, returning the result as a BigDecimal.
 *
 * BigDecimal('3.14159').truncate -> 3
 *
 * BigDecimal('8.7').truncate -> 8
 *
 * 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').truncate(3) -> 3.141
 *
 * BigDecimal('13345.234').truncate(-2) -> 13300.0
 */
static VALUE
BigDecimal_truncate(int argc, VALUE *argv, VALUE self)
{
    ENTER(5);
    Real *c, *a;
    int iLoc;
    U_LONG mx;
    VALUE vLoc;
    U_LONG pl = VpSetPrecLimit(0);

    if(rb_scan_args(argc,argv,"01",&vLoc)==0) {
        iLoc = 0;
    } else {
        Check_Type(vLoc, T_FIXNUM);
        iLoc = FIX2INT(vLoc);
    }

    GUARD_OBJ(a,GetVpValue(self,1));
    mx = a->Prec *(VpBaseFig() + 1);
    GUARD_OBJ(c,VpCreateRbObject(mx, "0"));
    VpSetPrecLimit(pl);
    VpActiveRound(c,a,VP_ROUND_DOWN,iLoc); /* 0: truncate */
    return ToValue(c);
}