このマニュアルは既にメンテナンスが終了したバージョンの Ruby を対象としています。 最新版のマニュアルへ

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

instance method Integer#round

round(ndigits = 0, half: :up) -> Integer[permalink][rdoc][edit]

self ともっとも近い整数を返します。

[PARAM] ndigits:
10進数での小数点以下の有効桁数を整数で指定します。負の整数を指定した場合、小数点位置から左に少なくとも n 個の 0 が並びます。
[PARAM] half:
ちょうど半分の値の丸め方を指定します。サポートされている値は以下の通りです。
  • :up or nil: 0から遠い方に丸められます。
  • :even: もっとも近い偶数に丸められます。
  • :down: 0に近い方に丸められます。

p 1.round       # => 1
p 1.round(2)    # => 1
p 15.round(-1)  # =>  20
p (-15).round(-1) # => -20

p 25.round(-1, half: :up)    # => 30
p 25.round(-1, half: :down)  # => 20
p 25.round(-1, half: :even)  # => 20
p 35.round(-1, half: :up)    # => 40
p 35.round(-1, half: :down)  # => 30
p 35.round(-1, half: :even)  # => 40
p (-25).round(-1, half: :up) # => -30
p (-25).round(-1, half: :down) # => -20
p (-25).round(-1, half: :even) # => -20

[SEE_ALSO] Numeric#round