Sha256: 3ca245bf8a34365793ae5fb7fc9e6c34fc31aa098b82d8e10ac2e872d807faa2

Contents?: true

Size: 638 Bytes

Versions: 1

Compression:

Stored size: 638 Bytes

Contents

require 'bigdecimal'
require 'bigdecimal/util'

module CurrencyMath
  def multiply(to_decimal, multiplier)
    (to_decimal.to_d * multiplier.to_d).currency_string
  end

  def add(to_decimal, addend)
    (to_decimal.to_d + addend.to_d).currency_string
  end

  def subtract(to_decimal, subtrahend)
    (to_decimal.to_d - subtrahend.to_d).currency_string
  end
end

class BigDecimal
  def currency_string
    truncate(2).to_s('F').with_trailing_zeros
  end
end

class String
  def with_trailing_zeros
    s = split('.')
    s.last << '0' while s.last.size < 2 && s.size > 1
    s.join('.')
  end

  def abs
    split('-').last
  end
end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
currency-math-0.1.3 lib/currency_math.rb