Sha256: 66a337ecf78d6d1cbc78e55fc44d6724734c9b894bf5f082e238aa0d1af5bd12

Contents?: true

Size: 959 Bytes

Versions: 6

Compression:

Stored size: 959 Bytes

Contents

module HasAccounts #:nodoc:
  module CoreExtensions #:nodoc:
    module Rounding
      # Rounds the float according to currency rules.
      # Currently targeted to Swiss Francs (CHF), usable
      # for all currencies having 0.05 as smallest unit.
      #
      #   x = 1.337
      #   x.round    # => 1.35
      def currency_round
        if self.nil?
          return 0.0
        else
          return (self * 20).round / 20.0
        end
      end
    end

    module BigDecimal
      module Rounding
        def currency_round
          if self.nil?
            return BigDecimal.new('0')
          else
            return (self * 20).round / BigDecimal('20')
          end
        end
      end
    end
  end
end

class Float #:nodoc:
  include HasAccounts::CoreExtensions::Rounding
end

class BigDecimal #:nodoc:
  include HasAccounts::CoreExtensions::BigDecimal::Rounding
end

class Fixnum #:nodoc:
  include HasAccounts::CoreExtensions::Rounding
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
has_accounts-3.1.0 lib/has_accounts/core_ext/rounding.rb
has_accounts-3.0.0.beta3 lib/has_accounts/core_ext/rounding.rb
has_accounts-3.0.0.beta2 lib/has_accounts/core_ext/rounding.rb
has_accounts-3.0.0.beta1 lib/has_accounts/core_ext/rounding.rb
has_accounts-3.0.0.beta0 lib/has_accounts/core_ext/rounding.rb
has_accounts-2.1.0 lib/has_accounts/core_ext/rounding.rb