struct Money

Overview

"Money is any object or record that is generally accepted as payment for goods and services and repayment of debts in a given socio-economic context or country." - Wikipedia

An instance of Money represents an amount of a specific currency.

Money is a value object and should be treated as immutable.

Included Modules

Extended Modules

Defined in:

money.cr
money/bank.cr
money/bank/single_currency.cr
money/bank/variable_exchange.cr
money/context.cr
money/currency.cr
money/currency/enumeration.cr
money/currency/loader.cr
money/error.cr
money/money.cr
money/money/allocate.cr
money/money/arithmetic.cr
money/money/casting.cr
money/money/constructors.cr
money/money/exchange.cr
money/money/formatting.cr
money/money/json.cr
money/money/parse.cr
money/version.cr

Constant Summary

VERSION = {{ (`shards version \"/home/runner/work/money/money/src/money\"`).chomp.stringify }}

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Money::Exchange

exchange_to(other_currency : String | Symbol | Currency) : Money exchange_to, with_same_currency(other : Money, &) with_same_currency

Instance methods inherited from module Money::Formatting

format(options : NamedTuple) : String format, to_s(io : IO) : Nil to_s

Instance methods inherited from module Money::Allocate

allocate(parts : Enumerable(Number)) : Array(Money)
allocate(*parts : Number) : Array(Money)
allocate
, split(parts : Int) : Array(Money) split

Instance methods inherited from module Money::Arithmetic

%(other : Number | Money) : Money %, *(other : Number) : Money *, +(other : Money) : Money
+ : Money
+
, -(other : Money) : Money
- : Money
-
, /(other : Number) : Money
/(other : Money) : BigDecimal
/
, abs : Money abs, divmod(other : Money) : Tuple(BigDecimal, Money)
divmod(other : Number) : Tuple(Money, Money)
divmod
, modulo(other : Number | Money) : Money modulo, negative? negative?, positive? positive?, remainder(other : Number) : Money remainder, zero? zero?

Instance methods inherited from module Money::Casting

to_big_d : BigDecimal to_big_d, to_big_f : BigFloat to_big_f

Constructor Detail

def self.new(amount : Number = 0, currency = Money.default_currency, bank = nil) #

Creates a new Money object of value given as an amount of the given currency (as fractional if Int, or whole amount otherwise).

Money.new                      # => Money(@amount=0.0 @currency="USD")
Money.new(1_50)                # => Money(@amount=1.5 @currency="USD")
Money.new(1.5, :usd)           # => Money(@amount=1.5 @currency="USD")
Money.new(1.5.to_big_d, "USD") # => Money(@amount=1.5 @currency="USD")

[View source]
def self.new(pull : JSON::PullParser) #

[View source]

Class Method Detail

def self.disallow_currency_conversion! #

Sets the default bank to be a Bank::SingleCurrency bank that raises on currency exchange. Useful when apps operate in a single currency at a time.


[View source]
def self.same_context_wrapper(&block : -> ) : Proc(Nil) #

Returns a proc that sets the Fiber.current#money_context to the current context (copied from the current fiber - references will be shared though) and then calls the given block. Useful when you need to spawn a new fiber later with the same context as the current fiber.

Money.default_currency = "PLN"

spawn &Money.same_context_wrapper(&block)
# or
spawn &Money.same_context_wrapper do
  Money.default_currency.code # => "PLN"
end

NOTE The proc will not set the context back to the original value.

See also #spawn_with_same_context.


[View source]
def self.spawn_with_same_context(**options, &block : -> ) : Nil #

Spawns a new fiber with the Money::Context copied from the current fiber.

Money.default_currency.code # => "USD"
Money.default_currency = "PLN"

spawn do
  Money.default_currency.code # => "USD"
end

# vs

Money.spawn_with_same_context do
  Money.default_currency.code # => "PLN"
end

NOTE References to the #default_{currency, bank, rate_store} properties will be shared between the current fiber and the spawned fiber.

See also #same_context_wrapper.


[View source]
def self.with_infinite_precision(enabled = true, &) #

Sets the given infinite precision value within the lifetime of the given block.

See also Money.infinite_precision?.


[View source]
def self.with_rounding_mode(mode : Number::RoundingMode, &) #

Sets the given rounding mode within the lifetime of the given block.

See also Money.rounding_mode.


[View source]

Instance Method Detail

def <=>(other : Money) : Int32 #

Compares two Money objects.


[View source]
def amount : BigDecimal #

Returns the numerical value of the money.

Money.new(1_00, "USD").amount # => 1.0

See also #fractional, Money.infinite_precision? and Money.rounding_mode.


[View source]
def bank : Bank #

The Bank object which currency exchanges are performed with.

NOTE Will return Money.default_bank if set to nil (the default).


[View source]
def bank=(bank : Bank | Nil) #

The Bank object which currency exchanges are performed with.

NOTE Will return Money.default_bank if set to nil (the default).


[View source]
def cents : BigDecimal #

Alias of #fractional.


[View source]
def currency : Currency #

The money's currency.


[View source]
def dollars : BigDecimal #

Alias of #amount.


[View source]
def fractional : BigDecimal #

The value of the monetary amount represented in the fractional or subunit of the currency.

For example, in the US dollar currency the fractional unit is cents, and there are 100 cents in one US dollar. So given the Money representation of one US dollar, the fractional interpretation is 100.

Another example is that of the Kuwaiti dinar. In this case the fractional unit is the fils and there 1000 fils to one Kuwaiti dinar. So given the Money representation of one Kuwaiti dinar, the fractional interpretation is 1000.

See also Money.infinite_precision? and Money.rounding_mode.


[View source]
def hash(hasher) #

Returns hash value based on the #amount and #currency attributes.


[View source]
def nearest_cash_value(rounding_mode : Number::RoundingMode = Money.rounding_mode) : BigDecimal #

Returns the nearest possible amount in cash value (cents).

For example, in Swiss franc (CHF), the smallest possible amount of cash value is CHF 0.05. Therefore, for CHF 0.07 this method returns CHF 0.05, and for CHF 0.08, CHF 0.10.

See also #rounded_to_nearest_cash_value and Currency#smallest_denomination.


[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.0, @currency="USD")
Money.new(10.5, "USD").round(mode: :ties_even) # => Money(@amount=10.0, @currency="USD")
Money.new(10.5, "USD").round(mode: :ties_away) # => Money(@amount=11.0, @currency="USD")

[View source]
def rounded_to_nearest_cash_value(rounding_mode : Number::RoundingMode = Money.rounding_mode) : Money #

Returns a new Money instance with the nearest possible amount in cash value.

See also #nearest_cash_value.


[View source]
def with_currency(new_currency : String | Symbol | Currency) : Money #

Returns a new Money instance in a given currency - if it's different from the current #currency - or self otherwise, leaving the amount intact and not performing currency conversion.


[View source]