Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Integer#truncate

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 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0