Sha256: 2beb2df4cfff12c8f49dc277f9f00d3ec480c19deeec65f1a4f193106395f930

Contents?: true

Size: 644 Bytes

Versions: 2

Compression:

Stored size: 644 Bytes

Contents

# frozen_string_literal: true

require 'bigdecimal'

module Docdata
  module Order
    # Helper for converting/formatting amounts.
    class Amount
      class << self
        def from_cents(cents)
          new(cents.to_i / 100.0)
        end
      end

      def initialize(amount)
        @amount = BigDecimal(amount.to_s)
      end

      def to_d
        @amount
      end

      def to_amount
        @amount / 100.0
      end

      def to_cents
        cents = @amount * 100
        cents.to_i
      end

      # Convert the amount to a String with 2 decimals.
      def to_s
        format("%.2f", @amount)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
docdata-order-2.1.0 lib/docdata/order/amount.rb
docdata-order-2.0.0 lib/docdata/order/amount.rb