struct Money
- Money
- Struct
- Value
- Object
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
- Comparable(Money)
- JSON::Serializable
- Money::Allocate
- Money::Arithmetic
- Money::Casting
- Money::Exchange
- Money::Formatting
- Money::Rounding
- Steppable
Extended Modules
- Money::Constructors
- Money::Context::Delegators
- Money::Parse
Defined in:
money.crmoney/context.cr
money/currency.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/money/rounding.cr
money/version.cr
Constant Summary
-
VERSION =
{{ (`shards version \"/home/runner/work/money/money/src/money\"`).chomp.stringify }}
Constructors
- .new(pull : JSON::PullParser)
-
.new(value : Int = 0, currency = Money.default_currency, exchange = nil)
Creates a new
Money
object of value given as an fractional amount of the given currency. -
.new(value : Number, currency = Money.default_currency, exchange = nil)
Creates a new
Money
object of value given as an whole amount of the given currency. -
.new(*, amount : Number, currency = Money.default_currency, exchange : Money::Currency::Exchange | Nil = nil)
Creates a new
Money
object of value given as an amount of the given currency. -
.new(*, fractional : Number, currency = Money.default_currency, exchange : Money::Currency::Exchange | Nil = nil)
Creates a new
Money
object of value given as a fractional of the given currency.
Class Method Summary
-
.configure(& : Money::Context -> ) : Nil
Yields the given block with the current
Money.context
as an argument. -
.disallow_currency_conversion!
Sets the default exchange to be a
Currency::Exchange::SingleCurrency
exchange that raises on currency exchange. -
.spawn_with_same_context(**options, &block : -> ) : Nil
Spawns a new fiber with the
Money::Context
copied from the current fiber. -
.with_infinite_precision(enabled = true, &)
Sets the given infinite precision value within the lifetime of the given block.
-
.with_rounding_mode(mode : Number::RoundingMode, &)
Sets the given rounding mode within the lifetime of the given block.
-
.without_currency_conversion(&)
Disallows currency conversion within the lifetime of the given block.
Instance Method Summary
-
#<=>(other : Money) : Int32
Compares two
Money
objects. -
#amount : BigDecimal
Returns the numerical value of the money.
-
#cents : BigDecimal
Alias of
#fractional
. -
#currency : Currency
The money's currency.
- #eql?(other : Money) : Bool
- #exchange : Currency::Exchange
- #exchange=(exchange : Currency::Exchange | Nil)
-
#fractional : BigDecimal
The value of the monetary amount represented in the fractional or subunit of the currency.
- #hash(hasher)
-
#succ : Money
Returns a new
Money
instance with incremented#fractional
value. - #to_json(json : JSON::Builder)
- #with_currency(new_currency : String | Symbol | Currency) : Money
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::Rounding
round(precision : Int = 0, mode : Number::RoundingMode = Money.rounding_mode) : Money
round,
round_to_nearest_cash_value(rounding_mode : Number::RoundingMode = Money.rounding_mode) : Money
round_to_nearest_cash_value,
round_to_nearest_cash_value!(rounding_mode : Number::RoundingMode = Money.rounding_mode) : Money
round_to_nearest_cash_value!,
round_to_nearest_cash_value?(rounding_mode : Number::RoundingMode = Money.rounding_mode) : Money | Nil
round_to_nearest_cash_value?
Instance methods inherited from module Money::Formatting
format(*, format : String | Nil = nil, display_free : String | Nil = nil, sign_positive : Bool = false, html : Bool = false, no_cents : Bool = false, no_cents_if_whole : Bool = false, drop_trailing_zeros : Bool = false, disambiguate : Bool = false, symbol : String | Char | Bool | Nil = true, decimal_mark : String | Char | Bool | Nil = true, thousands_separator : String | Char | Bool | Nil = true) : 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?, percentage(value : Number) : Money percentage, positive? positive?, remainder(other : Number) : Money remainder, sign : Int32 sign, 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
Creates a new Money
object of value given as an fractional amount
of the given currency.
Money.new(0, "USD") # => Money(@amount=0.0 @currency="USD")
Money.new(1_50, "USD") # => Money(@amount=1.5 @currency="USD")
Creates a new Money
object of value given as an whole amount
of the given currency.
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")
WARNING Floating points cannot guarantee precision. Therefore, they should only be used when you no longer need to represent currency or working with another system that requires floats.
Creates a new Money
object of value given as an amount
of the given currency.
Money.new(amount: 13.37) # => Money(@amount=13.37)
Creates a new Money
object of value given as a fractional
of the given currency.
Money.new(fractional: 13_37) # => Money(@amount=13.37)
Class Method Detail
Yields the given block with the current Money.context
as an argument.
Money.configure do |context|
context.infinite_precision = true
context.default_currency = "EUR"
end
Sets the default exchange to be a Currency::Exchange::SingleCurrency
exchange that raises
on currency exchange. Useful when apps operate in a single currency at a time.
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, exchange, rate_store}
properties
will be shared between the current fiber and the spawned fiber.
Sets the given infinite precision value within the lifetime of the given block.
See also Money.infinite_precision?
.
Sets the given rounding mode within the lifetime of the given block.
See also Money.rounding_mode
.
Disallows currency conversion within the lifetime of the given block.
See also Money.disallow_currency_conversion!
.
Instance Method Detail
Compares two Money
objects.
NOTE Two Money
objects with 0
amount are considered equal,
regardless of their currency.
NOTE Performs currency conversion if necessary.
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
.
Returns true
if the two Money
objects have same #amount
and #currency
,
false
otherwise.
NOTE Unlike #==
it does not perform currency conversion.
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
.
Returns a new Money
instance with incremented #fractional
value.
Money.new(1_00, "USD").succ # => Money(@amount=1.01 @currency="USD")
Money.new(1, "JPY").succ # => Money(@amount=2 @currency="JPY")