Ruby 4.1 リファレンスマニュアル

instance method Numeric#truncate

truncate(ndigits = 0) -> Integer | Float[permalink][rdoc][edit]
() → Integer
(Integer ndigits) → (Integer | Numeric)

0 から self までの整数で、self にもっとも近い整数を返します。

self を to_f で Float に変換してから Float#truncate を呼び出すのと同じです。

Complex では未定義化されています。

[PARAM] ndigits:
10進数での小数点以下の有効桁数を整数で指定します。正の整数を指定した場合、Float を返します。小数点以下を、最大 ndigits 桁にします。負の整数を指定した場合、Integer を返します。小数点位置から左に少なくとも ndigits 個の 0 が並びます。
例
p 1.truncate      # => 1
p 1.2.truncate    # => 1
p (-1.2).truncate # => -1
p (-1.5).truncate # => -1

p 1.234567.truncate(2)   # => 1.23
p 34567.89.truncate(-2)  # => 34500

[SEE_ALSO] Numeric#ceil, Numeric#floor, Numeric#round