Module: Polyfill::V2_4::Integer

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

Instance Method Summary collapse

Instance Method Details

#ceil(ndigits = 0) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/polyfill/v2_4/integer.rb', line 4

def ceil(ndigits = 0)
  ndigits = InternalUtils.to_int(ndigits)
  return super() if ndigits == 0
  return to_f if ndigits > 0

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

#digits(base = 10) ⇒ Object

Raises:

  • (Math::DomainError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/polyfill/v2_4/integer.rb', line 13

def digits(base = 10)
  base = InternalUtils.to_int(base)
  raise Math::DomainError, 'out of domain' if self < 0
  raise ArgumentError, 'negative radix' if base < 0
  raise ArgumentError, "invalid radix #{base}" if base < 2

  acc = []
  remainder = self
  while remainder > 0
    remainder, value = remainder.divmod(base)
    acc.push(value)
  end
  acc
end

#floor(ndigits = 0) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/polyfill/v2_4/integer.rb', line 28

def floor(ndigits = 0)
  ndigits = InternalUtils.to_int(ndigits)
  return super() if ndigits == 0
  return to_f if ndigits > 0

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

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



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/polyfill/v2_4/integer.rb', line 37

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

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

#truncate(ndigits = 0) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/polyfill/v2_4/integer.rb', line 49

def truncate(ndigits = 0)
  ndigits = InternalUtils.to_int(ndigits)
  return super() if ndigits == 0
  return to_f if ndigits > 0

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