Sha256: d37d447c2e5bdf61d0ebde477d056dcaf097c740e0cd4237054ed9fce52fc0d7

Contents?: true

Size: 1.86 KB

Versions: 17

Compression:

Stored size: 1.86 KB

Contents

require 'action_view'
module KonoUtils
##
# Classe che si occupa di rappresentare un numero percentuale
# ES:
# p = Percentage.new(100,20)
# p.percentage -> ritorna il valore percentuale float
# p.to_i -> ritorna percentuale intera con relativi arrotondamenti
# p.to_percentage -> si comporta come l'helper number_to_percentage
  class Percentage

    include ActionView::Helpers::NumberHelper

    attr_accessor :total, :partial

    def initialize(total=0, partial=0)
      @total = total
      @partial = partial
    end

    ##
    #
    # * *Args*    :
    #   - options -> Hash :
    #                 :locale - Sets the locale to be used for formatting (defaults to current locale).
    #
    #                 :precision - Sets the precision of the number (defaults to 3).
    #
    #                 :significant - If true, precision will be the # of significant_digits. If false, the # of fractional digits (defaults to false).
    #
    #                 :separator - Sets the separator between the fractional and integer digits (defaults to “.”).
    #
    #                 :delimiter - Sets the thousands delimiter (defaults to “”).
    #
    #                 :strip_insignificant_zeros - If true removes insignificant zeros after the decimal separator (defaults to false).
    #
    #                 :format - Specifies the format of the percentage string The number field is %n (defaults to “%n%”).
    #
    #                 :raise - If true, raises InvalidNumberError when the argument is invalid.
    # * *Returns* :
    #   - String
    #
    def to_percentage(options = {})
      number_to_percentage(percentage, options)
    end

    def percentage
      return 0 if @partial==0
      return 0 if @total<@partial
      @partial.to_f/@total.to_f*100.0
    end

    def to_i
      return 100 if @total.to_s == @partial.to_s
      percentage.to_i
    end


  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
kono_utils_helpers-0.1.2 lib/kono_utils/percentage.rb
kono_utils_helpers-0.1.1 lib/kono_utils/percentage.rb
kono_utils_helpers-0.1.0.pre.rc.1 lib/kono_utils/percentage.rb
kono_utils-1.0.1 lib/kono_utils/percentage.rb
kono_utils-1.0.0 lib/kono_utils/percentage.rb
kono_utils-0.15.16 lib/kono_utils/percentage.rb
kono_utils-0.15.15 lib/kono_utils/percentage.rb
kono_utils-0.15.14 lib/kono_utils/percentage.rb
kono_utils-0.15.13 lib/kono_utils/percentage.rb
kono_utils-0.15.12 lib/kono_utils/percentage.rb
kono_utils-0.15.11 lib/kono_utils/percentage.rb
kono_utils-0.15.10 lib/kono_utils/percentage.rb
kono_utils-0.15.9 lib/kono_utils/percentage.rb
kono_utils-0.15.8 lib/kono_utils/percentage.rb
kono_utils-0.15.7 lib/kono_utils/percentage.rb
kono_utils-0.15.6 lib/kono_utils/percentage.rb
kono_utils-0.15.5 lib/kono_utils/percentage.rb