Sha256: b9083e4402025f6ae9bec390c8cd7ee5113d48dd7c1f079a432ba6f1e9fd9fea

Contents?: true

Size: 564 Bytes

Versions: 4

Compression:

Stored size: 564 Bytes

Contents

# frozen_string_literal: true

require 'active_support'
require 'active_support/number_helper'
require 'bigdecimal'

Numeric.class_eval do
  # Converts an {#Integer}, {#Float}, or {#BigDecimal} into a percentage string.
  # @see ActiveSupport::NumberHelper#number_to_percentage
  #
  # @param options [Hash]
  #
  # @return [String]
  #
  def to_percentage(options = { precision: 2 })
    num = to_d * 100

    number_to_percentage(num, options)
  rescue
    "#{(to_f * 100).round(options.fetch(:precision, 2))}%"
  end

  alias_method :to_per, :to_percentage
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby-rails-extensions-2.1.1 lib/ruby-rails-extensions/extensions/to_percentage.rb
ruby-rails-extensions-2.1.0 lib/ruby-rails-extensions/extensions/to_percentage.rb
ruby-rails-extensions-2.1.0.pre.rc.14 lib/ruby-rails-extensions/extensions/to_percentage.rb
ruby-rails-extensions-2.1.0.pre.rc.13 lib/ruby-rails-extensions/extensions/to_percentage.rb