Sha256: f4fdd4f27d76a02d6ce4e325b1d8c58197c17d2af2f1ab4975483b81ddf83735

Contents?: true

Size: 733 Bytes

Versions: 1

Compression:

Stored size: 733 Bytes

Contents

# frozen_string_literal: true

module FFaker
  module Number
    extend ModuleUtils
    extend self

    def number(digits: 1)
      FFaker.numerify(generate_pattern(digits)).to_i
    end

    def decimal(whole_digits: 1, fractional_digits: 1)
      whole_part_pattern = generate_pattern(whole_digits)
      fractional_part_pattern = generate_pattern(fractional_digits).reverse
      FFaker.numerify("#{whole_part_pattern}.#{fractional_part_pattern}").to_f
    end

    def between(from: 1.00, to: 5000.00)
      fetch_sample(from..to)
    end

    private

    def generate_pattern(digits)
      raise ArgumentError, 'Digits cannot be less than 1' if digits < 1

      "#{fetch_sample(1..9)}#{'#' * (digits - 1)}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ffaker-2.23.0 lib/ffaker/number.rb