Sha256: ef3f6037c8b16f1a656ec1bb87e7215395b74fc3fe080fdf7644de27b80a7521

Contents?: true

Size: 963 Bytes

Versions: 16

Compression:

Stored size: 963 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

16 entries across 16 versions & 1 rubygems

Version Path
has_accounts-2.0.3 lib/has_accounts/core_ext/rounding.rb
has_accounts-2.0.2 lib/has_accounts/core_ext/rounding.rb
has_accounts-2.0.1 lib/has_accounts/core_ext/rounding.rb
has_accounts-2.0.0 lib/has_accounts/core_ext/rounding.rb
has_accounts-1.1.3 lib/has_accounts/core_ext/rounding.rb
has_accounts-1.1.2 lib/has_accounts/core_ext/rounding.rb
has_accounts-1.1.1 lib/has_accounts/core_ext/rounding.rb
has_accounts-1.0.2 lib/has_accounts/core_ext/rounding.rb
has_accounts-1.0.1 lib/has_accounts/core_ext/rounding.rb
has_accounts-1.0.0 lib/has_accounts/core_ext/rounding.rb
has_accounts-0.20.1 lib/has_accounts/core_ext/rounding.rb
has_accounts-0.20.0 lib/has_accounts/core_ext/rounding.rb
has_accounts-0.19.1 lib/has_accounts/core_ext/rounding.rb
has_accounts-0.19.0 lib/has_accounts/core_ext/rounding.rb
has_accounts-0.18.4 lib/has_accounts/core_ext/rounding.rb
has_accounts-0.18.3 lib/has_accounts/core_ext/rounding.rb