Sha256: e18c8c597dcfe6af6076988d896f2378d2f098db63243bbc95df28a356bb172c

Contents?: true

Size: 905 Bytes

Versions: 1

Compression:

Stored size: 905 Bytes

Contents

# frozen_string_literal: true
require_relative "./under_hundred.rb"
module ToWords
  module Utils
    include UnderHundred

    def result_below_one_thousand(num, counter)
      hundred, remaining = num.divmod(100)

      return higher_than_hundred(hundred, remaining, counter) if hundred != 0
      UNDER_HUNDRED[remaining] if hundred == 0 && remaining != 0
    end

    def higher_than_hundred(hundred, remaining, counter)
      century = UNDER_HUNDRED[hundred]
      if remaining != 0
        return century + " Hundred " + UNDER_HUNDRED[remaining] if counter != 0
        return century + " Hundred and " + UNDER_HUNDRED[remaining]
      end
      return century + " Hundred " if remaining == 0
    end

    def check_sign(num)
      num < 0 ? [num.abs, "negative "] : [num, ""]
    end

    def numerical?(num)
      Integer(num)
    rescue
      raise "A whole number is expected"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
to_words-1.1.1 lib/to_words/utils.rb