End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Integer#truncate
int.truncate([ndigits]) → integer or float
numeric.c
View on GitHub ↗
static VALUE
int_truncate(int argc, VALUE* argv, VALUE num)
{
int ndigits;
if (!rb_check_arity(argc, 0, 1)) return num;
ndigits = NUM2INT(argv[0]);
if (ndigits >= 0) {
return num;
}
return rb_int_truncate(num, ndigits);
}
Returns int truncated (toward zero) to a precision of ndigits decimal digits (default: 0).
When the precision is negative, the returned value is an integer with at least ndigits.abs trailing zeros.
Returns self when ndigits is zero or positive.
Example
1.truncate #=> 1 1.truncate(2) #=> 1 18.truncate(-1) #=> 10 (-18).truncate(-1) #=> -10