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
Extended Modules
Defined in:
money.crmoney/bank.cr
money/bank/single_currency.cr
money/bank/variable_exchange.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 \"/srv/crystaldoc.info/github-crystal-money-money-v1.3.0/src/money\"`).chomp.stringify }}
Constructors
-
.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 ifInt
, or whole amount otherwise) - .new(pull : JSON::PullParser)
Class Method Summary
-
.default_bank : Bank
Each
Money
object is associated to a bank object, which is responsible for currency exchange. -
.default_bank=(default_bank : Bank)
Each
Money
object is associated to a bank object, which is responsible for currency exchange. -
.default_currency : Currency
Sets the default currency for creating new
Money
object. -
.default_currency=(default_currency : Currency)
Sets the default currency for creating new
Money
object. -
.default_currency=(currency_code : String | Symbol)
Sets the default currency for creating new
Money
object. -
.disallow_currency_conversion!
Sets the default bank to be a
Bank::SingleCurrency
bank that raises on currency exchange. -
.infinite_precision=(infinite_precision : Bool)
Use this to enable infinite precision cents
-
.infinite_precision? : Bool
Use this to enable infinite precision cents
-
.rounding_mode : Number::RoundingMode
Default rounding mode
-
.rounding_mode=(rounding_mode : Number::RoundingMode)
Default rounding mode
-
.with_rounding_mode(mode : Number::RoundingMode, &)
Sets the given rounding mode within the scope of the given block
Instance Method Summary
-
#<=>(other : Money) : Int32
Compares two
Money
objects. -
#amount : BigDecimal
Returns the numerical value of the money.
-
#bank : Bank
The
Bank
object which currency exchanges are performed with. -
#bank=(bank : Bank | Nil)
The
Bank
object which currency exchanges are performed with. -
#cents : BigInt
Alias of
#fractional
. -
#currency : Currency
The money's currency.
-
#dollars : BigDecimal
Alias of
#amount
. -
#fractional : BigInt
The value of the monetary amount represented in the fractional or subunit of the currency.
- #hash(hasher)
-
#nearest_cash_value : BigInt
Returns the nearest possible amount in cash value (cents).
-
#rounded_to_nearest_cash_value : Money
See
#nearest_cash_value
.
Instance methods inherited from module Money::Exchange
exchange_to(other_currency) : Money
exchange_to,
with_same_currency(other : Money, &)
with_same_currency
Instance methods inherited from module Money::Formatting
format(options : NamedTuple) : Stringformat(**options) : String format, to_s(io : IO) : Nil to_s
Instance methods inherited from module Money::Allocate
allocate(splits : Enumerable(Number)) : Array(Money)allocate(*splits : Number) : Array(Money) allocate, split(num : Int) : Array(Money) split
Instance methods inherited from module Money::Arithmetic
%(other) : 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(BigInt, Money)
divmod(other : Number) : Tuple(Money, Money) divmod, modulo(other) : Money modulo, negative? negative?, positive? positive?, remainder(other : Number) : Money remainder, round(precision : Int = 0, mode : Number::RoundingMode = Money.rounding_mode) : Money round, 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 amount
of the given currency (as fractional if Int
, or whole amount otherwise)
Money.new # => Money(@amount=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")
Class Method Detail
Each Money
object is associated to a bank object, which is responsible
for currency exchange. This property allows you to specify the default
bank object. The default value for this property is an instance of
Bank::VariableExchange
. It allows one to specify custom exchange rates.
Each Money
object is associated to a bank object, which is responsible
for currency exchange. This property allows you to specify the default
bank object. The default value for this property is an instance of
Bank::VariableExchange
. It allows one to specify custom exchange rates.
Sets the default currency for creating new Money
object.
Sets the default currency for creating new Money
object.
Sets the default currency for creating new Money
object.
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.
Use this to enable infinite precision cents
Sets the given rounding mode within the scope of the given block
Instance Method Detail
Returns the numerical value of the money.
Money.new(1_00, "USD").amount # => 1.0
See #to_big_d
and #fractional
, also Money.rounding_mode
.
The Bank
object which currency exchanges are performed with.
NOTE Setting nil
(the default) will delegate to Money.default_bank
.
The Bank
object which currency exchanges are performed with.
NOTE Setting nil
(the default) will delegate to Money.default_bank
.
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.
FIXME Doesn't work with Money.infinite_precision?
yet
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 Currency#smallest_denomination
, also Money.rounding_mode
.