Sha256: 9a94a7f60744c65ae16d3eef3013aa6d691d3e01abb39a624fd156bccbec7edf

Contents?: true

Size: 1.55 KB

Versions: 9

Compression:

Stored size: 1.55 KB

Contents

module Polyfill
  module V2_4
    module Integer
      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

      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

      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

      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

      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
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
polyfill-1.9.0 lib/polyfill/v2_4/integer.rb
polyfill-1.8.0 lib/polyfill/v2_4/integer.rb
polyfill-1.7.0 lib/polyfill/v2_4/integer.rb
polyfill-1.6.0 lib/polyfill/v2_4/integer.rb
polyfill-1.5.0 lib/polyfill/v2_4/integer.rb
polyfill-1.4.0 lib/polyfill/v2_4/integer.rb
polyfill-1.3.0 lib/polyfill/v2_4/integer.rb
polyfill-1.2.0 lib/polyfill/v2_4/integer.rb
polyfill-1.1.0 lib/polyfill/v2_4/integer.rb