Module: Polyfill::V2_4::Integer::Instance::Round::Method

Defined in:
lib/polyfill/v2_4/integer/instance/round.rb

Instance Method Summary collapse

Instance Method Details

#round(ndigits = 0, half: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/polyfill/v2_4/integer/instance/round.rb', line 7

def round(ndigits = 0, half: nil)
  unless [nil, :down, :even, :up, 'down', 'even', 'up'].include?(half)
    raise ArgumentError, "invalid rounding mode: #{half}"
  end
  ndigits = ndigits.to_int
  return super() if ndigits == 0
  return to_f if ndigits > 0

  place = 10 ** -ndigits
  (self.to_f / place).round * place
end