Sha256: bfe4f631a9bae15d172643a60ef531d27823104deff2ca961dfde9d4dc101d63

Contents?: true

Size: 828 Bytes

Versions: 14

Compression:

Stored size: 828 Bytes

Contents

require_relative 'constants/en'

module Humanize
  class En
    def humanize(number)
      iteration = 0
      parts = []
      use_and = false
      until number.zero?
        number, remainder = number.divmod(1000)
        unless remainder.zero?
          if iteration.zero? && remainder < 100
            use_and = true
          else
            add_grouping(parts, use_and, iteration)
          end

          parts << SUB_ONE_GROUPING[remainder]
        end

        iteration += 1
      end

      parts
    end

    private

    def conjunction(parts, use_and)
      return '' if parts.empty?

      use_and ? ' and' : ','
    end

    def add_grouping(parts, use_and, iteration)
      grouping = LOTS[iteration]
      return unless grouping

      parts << "#{grouping}#{conjunction(parts, use_and)}"
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
humanize-3.0.0 lib/humanize/locales/en.rb
humanize-2.5.1 lib/humanize/locales/en.rb
humanize-2.5.0 lib/humanize/locales/en.rb
humanize-2.4.3 lib/humanize/locales/en.rb
humanize-2.4.2 lib/humanize/locales/en.rb
humanize-2.4.1 lib/humanize/locales/en.rb
humanize-2.4.0 lib/humanize/locales/en.rb
humanize-2.3.0 lib/humanize/locales/en.rb
humanize-2.2.1 lib/humanize/locales/en.rb
humanize-2.2.0 lib/humanize/locales/en.rb
humanize-2.1.2 lib/humanize/locales/en.rb
humanize-2.1.1 lib/humanize/locales/en.rb
humanize-2.1.0 lib/humanize/locales/en.rb
humanize-2.0.0 lib/humanize/locales/en.rb