Sha256: f4729a35619fe3509ecabb0b1671471c453adbc09e0c4ace83197ca090445a93

Contents?: true

Size: 1.53 KB

Versions: 124

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

class ReeNumber::NumberToPercentage
  include Ree::FnDSL

  fn :number_to_percentage do
    link :number_to_rounded, import: -> { ROUND_MODES }
    link :slice, from: :ree_hash
  end

  DEFAULTS = {
    format: "%n%",
    precision: 3,
    significant: false,
    strip_insignificant_zeros: false,
    delimiter: "",
    round_mode: :default
  }.freeze

  doc(<<~DOC)
    Converts Integer, String or Float to a String with percentage at the end.
    The +opts+ parameter takes a hash of any of these keys:
        <tt>:format</tt>, <tt>:precision</tt>, 
        <tt>:significant</tt>, <tt>:strip_insignificant_zeros</tt>, <tt>:delimiter</tt>,
        <tt>:round_mode</tt>
      number_to_percentage(100)                                                      # => "100.000%"
      number_to_percentage(123.400, precision: 3, strip_insignificant_zeros: true)   # => "123.4%"
      number_to_percentage.("-0.13", format_percentage: " %", precision: 2)          # => "-0.13 %"
  DOC

  contract(
    Or[Integer, String, Float], 
    Ksplat[
      format?: String, 
      precision?: Integer,
      significant?: Bool,
      strip_insignificant_zeros?: Bool,
      delimiter?: String,
      round_mode?: Or[*ROUND_MODES]
    ] => String
  )
  def call(number, **opts)
    options = DEFAULTS.merge(opts)

    rounded_number = number_to_rounded(
      number,
      **slice(
        options,
        [:precision, :significant, :strip_insignificant_zeros, :round_mode]
      )
    )

    options[:format].gsub("%n", rounded_number)
  end
end

Version data entries

124 entries across 124 versions & 1 rubygems

Version Path
ree_lib-1.0.124 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_percentage.rb