module Money::Arithmetic

Direct including types

Defined in:

money/money/arithmetic.cr

Instance Method Summary

Instance Method Detail

def %(other) : Money #

Alias of #modulo.


[View source]
def *(other : Number) : Money #

Multiplies the monetary value with the given other Number and returns a new Money object with this monetary value and the same #currency.

Money.new(100) * 2 # => Money(@amount=2)

[View source]
def +(other : Money) : Money #

Returns a new Money object containing the sum of the two operands' monetary values.

Money.new(100) + Money.new(100) # => Money(@amount=2)

[View source]
def + : Money #

Alias of #abs.

+Money.new(-100) # => Money(@amount=1)

[View source]
def -(other : Money) : Money #

Returns a new Money object containing the difference between the two operands' monetary values.

Money.new(100) - Money.new(99) # => Money(@amount=0.01)

[View source]
def - : Money #

Returns a new Money object with changed polarity.

-Money.new(100) # => Money(@amount=-1)

[View source]
def /(other : Number) : Money #

Divides the monetary value with the given other Number and returns a new Money object with this monetary value and the same #currency.

Money.new(100) / 10 # => Money(@amount=0.1)

[View source]
def /(other : Money) : BigDecimal #

Divides the monetary value with the given other Money object and returns a ratio.

Money.new(100) / Money.new(10) # => 10.0

[View source]
def abs : Money #

Returns absolute value of self as a new Money object.

Money.new(-100).abs # => Money(@amount=1)

[View source]
def divmod(other : Money) : Tuple(BigInt, Money) #

Divide by Money or Number and return Tuple containing quotient and modulus.

Money.new(100).divmod(9)            # => {Money(@amount=0.11), Money(@amount=0.01)}
Money.new(100).divmod(Money.new(9)) # => {11, Money(@amount=0.01)}

[View source]
def divmod(other : Number) : Tuple(Money, Money) #

Divide by Money or Number and return Tuple containing quotient and modulus.

Money.new(100).divmod(9)            # => {Money(@amount=0.11), Money(@amount=0.01)}
Money.new(100).divmod(Money.new(9)) # => {11, Money(@amount=0.01)}

[View source]
def modulo(other) : Money #

Equivalent to #divmod(other)[1].

Money.new(100).modulo(9)            # => Money(@amount=0.01)
Money.new(100).modulo(Money.new(9)) # => Money(@amount=0.01)

[View source]
def negative? #

Returns true if the money amount is less than 0, false otherwise.

Money.new(-1).negative? # => true
Money.new(0).negative?  # => false
Money.new(1).negative?  # => false

[View source]
def positive? #

Returns true if the money amount is greater than 0, false otherwise.

Money.new(1).positive?  # => true
Money.new(0).positive?  # => false
Money.new(-1).positive? # => false

[View source]
def remainder(other : Number) : Money #

If different signs #modulo(other) - other, otherwise #modulo(other).

Money.new(100).remainder(9) # => Money(@amount=0.01)

[View source]
def round(precision : Int = 0, mode : Number::RoundingMode = Money.rounding_mode) : Money #

Rounds the monetary amount to smallest unit of coinage, using rounding mode if given, or Money.rounding_mode otherwise.

Money.new(10.1, "USD").round                   # => Money(@amount=10, @currency="USD")
Money.new(10.5, "USD").round(mode: :ties_even) # => Money(@amount=10, @currency="USD")
Money.new(10.5, "USD").round(mode: :ties_away) # => Money(@amount=11, @currency="USD")

[View source]
def zero? #

Returns true if the money amount is zero.

Money.new(0).zero?    # => true
Money.new(100).zero?  # => false
Money.new(-100).zero? # => false

[View source]