Skip to content
instance method

Integer#truncate

() -> ::Integer
(::int ndigits) -> ::Integer
truncate(ndigits = 0) → integer

Returns self truncated (toward zero) to a precision of ndigits decimal digits.

When ndigits is negative, the returned value has at least ndigits.abs trailing zeros:

Example
555.truncate(-1)  # => 550
555.truncate(-2)  # => 500
-555.truncate(-2) # => -500

Returns self when ndigits is zero or positive.

Example
555.truncate     # => 555
555.truncate(50) # => 555

Related: Integer#round.

See also #to_s #upto #to_r #zero? also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0