io.randomseed.bankster.money
->clojure-symbol
added in 1.1.0
(->clojure-symbol a)
Converts an amount of the given money to a clojure symbol (with added M when needed).
->double
added in 1.0.0
(->double a)
(->double a scale)
(->double a rounding-mode)
(->double a scale rounding-mode)
Converts an amount of the given money to a double with optional rescaling.
->float
added in 1.0.0
(->float a)
(->float a scale)
(->float a rounding-mode)
(->float a scale rounding-mode)
Converts an amount of the given money to a float with optional rescaling.
Accountable
protocol
added in 1.0.0
This protocol is used to express (monetary) values using various numeric types and currency representations.
members
cast
added in 1.1.0
(cast money)
(cast money currency)
(cast money currency rounding-mode)
Casts an existing Money object to another having a different currency, rescaling the amount and optionally rounding it. This is useful when operating on multiple currency registries compatible with different data sources or processing engines. For simply ensuring that a currency is sourced in the right registry, use of-registry
.
value
added in 1.0.0
(value num)
(value currency num)
(value currency num rounding-mode)
Creates new Money object for the given value which will become an amount. If the currency is not given it will try to use the default one, taken from the *default-currency*
dynamic variable. Optional rounding-mode should be a rounding mode used when the conversion to a scaled monetary amount requires rounding.
In its unary form, when the argument is not numeric, it will try to get the currency object (identified by a string, a symbol or a keyword) from the default, global registry of currencies.
For simple money creation the following macros may be convenient way to go: of, of-major, of-minor.
Be careful about using number literals for big-scale amounts (16–17 digits). Use either big decimal literals, e.g. 1234.45689101112M, or strings.
add
added in 1.0.0
(add)
(add a)
(add a b)
(add a b & more)
Adds two or more amounts of money of the same currency. When called without any arguments, returns 0.
add-major
added in 1.0.0
(add-major a b)
Increases major amount by the given number. If the number is also expressed as money and it has decimal parts, they will be truncated.
add-minor
added in 1.0.0
(add-minor a b)
Increases minor amount by the given number. If the number is also expressed as money and it has decimal parts, they will be truncated..
add-scaled
added in 1.0.0
(add-scaled)
(add-scaled a)
(add-scaled a b)
(add-scaled a b & more)
Alias for add.
amount
added in 1.0.0
(amount money)
(amount a b)
(amount a b r)
Returns the amount of the given money. For more than one argument the money is created ad-hoc using a and b objects passed to the function named value.
apply
added in 1.2.7
(apply money f)
(apply money f a)
(apply money f a b)
(apply money f a b & more)
Alias for on-amount.
auto-scaled?
added in 1.1.2
(auto-scaled? m)
Returns true if the given Money object is based on a currency which is auto-scaled.
code-literal
added in 1.2.4
(code-literal arg)
Tagged literal handler for Money objects expressed as tagged literals in Clojure code. Returns compound forms (parsing function invocations that can be influenced by the runtime environment, e.g. dynamic variables like scale/rounding-mode).
code-readers
added in 1.2.4
Data readers map for currency and money (intended to be used when evaluating Clojure code with tagged literals: #currency and #money). Handlers assigned to literals may emit compound forms, like function call forms (as Clojure code). To operate on EDN data files, see data-readers.
compare
added in 1.0.0
(compare a)
(compare a b)
Compares two monetary amounts of the same currency and scale. Returns -1 if the second one is less than, 0 if equal to, and 1 if it is greater than the first. Nil values are always considered lower when comparing.
compare-amounts
added in 1.0.0
(compare-amounts a)
(compare-amounts a b)
Compares two monetary amounts of the same currency, regardless of their scales. Returns -1 if the second one is less than, 0 if equal to, and 1 if it is greater than the first. Nil values are always considered lower when comparing.
convert
added in 1.0.0
(convert money price)
(convert money price rounding-mode)
(convert money to-currency exchange-rate)
(convert money to-currency exchange-rate rounding-mode)
Converts a monetary amount from one currency to another using the given multiplier (expressing a conversion rate). When two money objects are given, the second one should express a price of 1 unit of the first currency.
When no rounding mode is given and rounding is required during scaling to another currency then the value of dynamic variable io.randomseed.bankster.scale/*rounding-mode*
will be used.
currency
added in 1.0.0
(currency money)
(currency a b)
(currency a b r)
Returns the currency of the given money. For more than one argument the money is created ad-hoc using a and b objects passed to the function named value.
data-literal
added in 1.2.4
(data-literal arg)
Data reader for Money objects expressed as tagged literals in EDN data files.
data-readers
added in 1.2.4
Data readers map for currency and money (intended to be used when reading EDN data files containing tagged literals: #currency and #money). Handlers assigned to literals are always emitting constant forms. Please note that tagged literal handlers for Clojure code are using different readers (defined as code-readers).
defliteral
added in 1.0.0
(defliteral c)
For the given currency identifier or a currency object it creates a tagged literal in a form of #money/CURRENCY where the CURRENCY is a currency code. As a side effect it creates a function of name io.randomseed.bankster.money/of-CURRENCY that will handle the literal.
The literals will be bound to data-readers in a local thread.
div
added in 1.0.0
(div a)
(div a b)
(div a b & more)
Divides two or more amounts of money of the same currency or numbers. If two of the given values are a kind of Money, the result will be a BigDecimal number. If the earlier value is a kind of Money and the second is a number, the result will be a Money. If the first argument is a number and the second argument is a monetary amount, an exception will be thrown. The result will always be either a kind of Money or a BigDecimal number.
For a single value it returns a division of 1 by the given number or monetary value.
For more than 2 arguments it repeatedly divides their values as described. If the previous calculations produce a regular number, all consequent divisors must be regular numbers. Practically, if the division begins with a monetary amount, only one monetary amount at some point later is permitted (which will cause the function to switch to the regular numbers calculation since the units will cancel themselves).
Note that rescaling is not based on the original scale of a currency. This is designed that way is to handle cases when monetary amount was intentionally rescaled earlier.
For regular numbers and for monetary values of auto-scaled currencies the dynamic rescaling is performed to handle corner cases where there would be a non-terminating decimal expansion. Trailing zeros are stripped before any calculation.
Rescaling is performed after all calculations are done. However, the scale can be applied on each calculation if the dynamic variable io.randomseed.bankster.scale/*each*
is set to a truthy value. If the scaling requires rounding then enclosing the expression within with-rounding
is required. This can also be achieved by enclosing the expression within io.randomseed.bankster.scale/with-rescaling
(aliased in this namespace under the same name) macro combining the switch and setting the scale.
div-scaled
added in 1.0.0
(div-scaled a)
(div-scaled a b)
(div-scaled a b & more)
Divides two or more amounts of money of the same currency or numbers. Scales and optionally rounds each intermediate result (if there are more divisors).
If the first given argument is a kind of money and one of the divisors is also a kind of Money, the result will be a BigDecimal kind of number. If the earlier value is a kind of Money and the second is a number, the result will be a Money. If the first argument is a number and the second argument is a monetary amount, an exception will be thrown.
For a single value it returns a division of 1 by the given number or monetary value.
For more than 2 arguments it repeatedly divides their values as described and re-scales each result to match the scale of the first encountered monetary amount. If there are no monetary amounts involved the scale is calculated dynamically.
Note that rescaling is not based on the original scale of a currency. This is designed that way is to handle cases when monetary amount was intentionally rescaled earlier.
For regular numbers and for monetary values of auto-scaled currencies the dynamic rescaling is performed to handle corner cases where there would be a non-terminating decimal expansion. Trailing zeros are stripped before any calculation.
If the scaling requires rounding then enclosing the expression within with-rounding
(also present in io.randomseed.bankster.scale
namespace) is required.
eq-am?
added in 1.0.0
(eq-am? a)
(eq-am? a b)
(eq-am? a b & more)
Return true if the monetary amounts and their currencies are equal, regardless of their scales.
eq?
added in 1.0.0
(eq? a)
(eq? a b)
(eq? a b & more)
Return true if the money amounts and their currencies are equal. Note that currencies with different scales or other properties are considered different. Use eq-am? (aliased as ==) to compare amounts regardless of their scales.
format
added in 1.0.0
(format money)
(format money locale)
(format money locale opts)
Formats the given amount of money as a string according to localization rules. If the locale argument is not given then the default is used.
It is advised to express locale using a keyword when huge amount of operations is expected.
If there are 3 arguments passed the third one should be a map of options customizing the formatting. It can have the following keys:
- :rounding-mode - RoundingMode, rounding mode to apply when scaling (if
scale/*rounding-mode*
is not set) - :grouping - Boolean, if true then grouping will be used
- :grouping-size - integer, size of a group when grouping
- :negative-prefix - String, negative prefix to use
- :negative-suffix - String, negative suffix to use
- :positive-prefix - String, positive prefix to use
- :positive-suffix - String, positive suffix to use
- :always-sep-dec - Boolean, if true then the decimal separator will always be shown
- :currency-symbol-fn - a function used on a bankster/Currency object to get its symbol as a string
- :min-fraction-digits - integer, the minimum number of digits allowed in the fraction portion of an amount
- :min-integer-digits - integer, the minimum number of digits allowed in the integer portion of an amount
- :max-fraction-digits - integer, the maximum number of digits allowed in the fraction portion of an amount
- :max-integer-digits - integer, the maximum number of digits allowed in the integer portion of an amount
- :scale - sets both :min-fraction-digits and :max-fraction-digits to the same value.
The function assigned to the :currency-symbol-fn should take 3 arguments: currency, locale and registry.
Note that you may gain some speed by re-using the extended formatter (basic formatter is already cached). See the documentation of the format-with function for more information.
format-with
added in 1.0.0
(format-with formatter money)
Formats the amount of money using the formatter provided. Formatters can be created with io.randomseed.bankster.currency/formatter and io.randomseed.bankster.currency/formatter-extended.
ge?
added in 1.0.0
(ge? a)
(ge? a b)
(ge? a b & more)
Returns non-nil if monetary amounts are in monotonically non-increasing order, otherwise false.
gt?
added in 1.0.0
(gt? a)
(gt? a b)
(gt? a b & more)
Returns non-nil if monetary amounts are in monotonically decreasing order, otherwise false.
is-neg-or-zero?
added in 1.0.0
(is-neg-or-zero? a)
Returns true if the given monetary amount is a negative number or zero.
is-neg?
added in 1.0.0
(is-neg? a)
Returns true if the given monetary amount is a negative number.
is-pos-or-zero?
added in 1.0.0
(is-pos-or-zero? a)
Returns true if the given monetary amount is a positive number or zero.
is-pos?
added in 1.0.0
(is-pos? a)
Returns true if the given monetary amount is a positive number.
is-zero?
added in 1.0.0
(is-zero? a)
Returns true if the given monetary amount is a positive number.
le?
added in 1.0.0
(le? a)
(le? a b)
(le? a b & more)
Returns non-nil if monetary amounts are in monotonically non-decreasing order, otherwise false.
lt?
added in 1.0.0
(lt? a)
(lt? a b)
(lt? a b & more)
Returns non-nil if monetary amounts are in monotonically increasing order, otherwise false.
major->int
added in 1.0.0
(major->int a)
Returns the major part of the given amount as a int number.
major->long
added in 1.0.0
(major->long a)
Returns the major part of the given amount as a long number.
major-minor
added in 1.0.0
(major-minor a)
Returns a vector with major and minor parts of the given monetary amount.
major-minor->int
added in 1.0.0
(major-minor->int a)
Returns a vector with major and minor parts of the given monetary amount represented as integer numbers.
major-minor->long
added in 1.0.0
(major-minor->long a)
Returns a vector with major and minor parts of the given monetary amount represented as long numbers.
major-value
(major-value a)
(major-value currency amount)
(major-value currency amount rounding-mode)
Creates new Money object for the given value which will become a major part of the amount. If the given number has fractional part it will be truncated. If the currency is not given it should try to use the default one, taken from the io.randomseed.bankster.currency/*default*
dynamic variable. Optional rounding-mode should be a rounding mode used when the conversion to a scaled monetary amount requires rounding.
For simple money creation the following macros may be convenient way to go: of, of-major, of-minor.
max-amount
added in 1.0.0
(max-amount a)
(max-amount a b)
(max-amount a b & more)
Returns the greatest of the monetary amounts.
min-amount
added in 1.0.0
(min-amount a)
(min-amount a b)
(min-amount a b & more)
Returns the least of the monetary amounts.
minor->int
added in 1.0.0
(minor->int a)
Returns the minor part of the given amount as an integer number.
minor->long
added in 1.0.0
(minor->long a)
Returns the minor part of the given amount as a long number.
minor-value
(minor-value a)
(minor-value currency amount)
(minor-value currency amount rounding-mode)
Creates new Money object for the given value which will become a minor part of the amount. If the given number has fractional part it will be truncated. If the currency is not given it should try to use the default one, taken from the io.randomseed.bankster.currency/*default*
dynamic variable. Optional rounding-mode should be a rounding mode used when the conversion to a scaled monetary amount requires rounding.
For simple money creation the following macros may be convenient way to go: of, of-major, of-minor.
mul
added in 1.0.0
(mul)
(mul a)
(mul a b)
(mul a b & more)
Multiplies two or more values where one may be a Money object. If one of the values is a kind of Money then the result will also be a kind of Money. For a single value it simply returns it. For more than 2 arguments it repeatedly multiplies them as described.
When there is a multiplication of a monetary value by the regular number, the final result of all operations is re-scaled to match the scale of monetary amount. If the scaling requires rounding, enclosing the expression within io.randomseed.bankster.scale/with-rounding
is required (the macro is aliased under the same name in this namespace).
Note that rescaling is not based on the original scale of a currency. This is designed that way is to handle cases when monetary amount was intentionally rescaled earlier.
For regular numbers dynamic rescaling is performed only to handle corner cases where there would be a non-terminating decimal expansion.
Rescaling is performed after performing all operations unless all values are regular numbers (without any Money object among them).
mul-scaled
added in 1.0.0
(mul-scaled)
(mul-scaled a)
(mul-scaled a b)
(mul-scaled a b & more)
Multiplies two or more values where one may be a Money object. If one of the values is a kind of Money then the result will also be a kind of Money. For a single value it simply returns it. For more than 2 arguments it repeatedly multiplies them as described.
When there is a multiplication of a monetary value by the regular number, the final result of all operations is re-scaled to match the scale of the monetary amount. If the scaling requires rounding, enclosing the expression within io.randomseed.bankster.scale/with-rounding
is required (the macro is aliased under the same name in this namespace).
Note that rescaling is not based on the original scale of a currency. This is designed that way is to handle cases when monetary amount was intentionally rescaled earlier.
For regular numbers dynamic rescaling is performed only to handle corner cases where there would be a non-terminating decimal expansion.
Rescaling is performed on each operation unless/until all values are regular numbers (without any Money object among them).
ne-am?
added in 1.0.0
(ne-am? a)
(ne-am? a b)
(ne-am? a b & more)
Returns true if the money amounts or their currencies are different, regardless of their scales.
ne?
added in 1.0.0
(ne? a)
(ne? a b)
(ne? a b & more)
Returns true if the money amounts or their currencies are different, regardless of their scales. Note that currencies with different scales are considered different.
neg
added in 1.0.0
(neg a)
Returns the negated amount of the given money. For negative amount it will reverse their sign. Same as (sub x).
of
macro
(of)
(of a)
(of a b)
(of a b rounding-mode)
Returns the amount of money as a Money object consisting of a currency and a value. Currency can be a currency object and for registered currencies: a keyword, a symbol or a string (e.g. EUR, :EUR, “PLN” or crypto/ETH), or even a number (for ISO-compliant currencies).
The given amount can be any numeric value or a string that can be converted to java.math.BigDecimal.
When a number must be downscaled to fulfill the number of decimal places for a currency, rounding mode must be given, which may be a symbol, a keyword or a string of the following:
- CEILING - rounds towards positive infinity.
- DOWN - rounds towards zero.
- FLOOR - rounds towards negative infinity.
- HALF_DOWN - rounds towards nearest neighbor unless both neighbors are equidistant, in which case rounds down.
- HALF_EVEN - rounds towards the nearest neighbor unless both neighbors are equidistant, and if so, rounds towards the even.
- HALF_UP - rounds towards the nearest neighbor unless both neighbors are equidistant, and if so, rounds up.
- UP – rounds away from zero
- UNNECESSARY - asserts that the requested operation has an exact result, hence no rounding is necessary.
To create a monetary object using function, call io.randomseed.bankster.money/value
.
Be careful about using number literals for big-scale amounts (16–17 digits). Use either big decimal literals, e.g. 1234.45689101112M, or strings.
of-major
macro
(of-major)
(of-major a)
(of-major a b)
(of-major a b rounding-mode)
Like io.randomseed.money/of but sets the amount of major part only.
of-minor
macro
(of-minor)
(of-minor a)
(of-minor a b)
(of-minor a b rounding-mode)
Like io.randomseed.money/of but sets the amount of minor part only.
of-registry
added in 1.1.2
(of-registry money)
(of-registry registry money)
(of-registry registry money rounding-mode)
Ensures that a currency of the given money originates from the given registry. If the registry is not given and a dynamic registry is not set, the default one is used. Rescales the amount if needed to match the nominal scale. Optional rounding-mode can be supplied to be used when downscaling is needed (nominal currency from a registry has lower number of decimal places than the amount of money).
Money can be expressed as a Money object or any other object that will create Money when passed to the value
function. Returns money.
on-amount
added in 1.1.2
(on-amount money f)
(on-amount money f a)
(on-amount money f a b)
(on-amount money f a b & more)
Performs an operation expressed with a function f on an amount of the given money. Additional arguments will be passed to the f. Returns the money with the amount updated. The function f must return a number. Short-circuits on nil as an argument. Rescales the result to the existing scale of an amount, not the nominal scale of a currency.
rem
added in 1.2.0
(rem a b)
(rem a b rounding-mode)
Returns the remainder of dividing an mount of money by the given number. For two money objects the result is BigDecimal. When only the first one is a kind of Money the result will also be a Money. In the last case the result will be rescaled to the scale of the monetary amount. When rounding mode is not give io.randomseed.bankster.scale/*rounding-mode*
will be used when set.
rescale
added in 1.0.0
(rescale money)
(rescale money scale)
(rescale money scale rounding-mode)
Same as scale but its unary variant will rescale an amount of the given money to conform it to its currency settings instead of returning the scale. It has the same effect as calling io.randomseed.bankster.scale/apply
on a Money object.
rescaled?
added in 1.0.0
(rescaled? a)
Returns true if the given monetary value has different scale than its currency (so it was rescaled). Returns false if the scale is no different. Returns nil if the currency does not have a fixed scale.
round
added in 1.0.0
(round money scale)
(round money scale rounding-mode)
Rounds the amount of money using the given scale and rounding mode. Returns money with rounded amount preserving the original scale. If the rounding mode is not given the one from scale/rounding-mode is used.
round-to
added in 1.2.9
(round-to money)
(round-to money i)
(round-to money i rounding-mode)
Rounds a Money to an interval i (which should also be Money or a number). If rounding mode is not provided (via the last argument or scale/rounding-mode) then it defaults to HALF_EVEN. Retains original scale of the amount (but not the nominal scale of a currency, if money was given). For nil intervals or intervals which amounts are negative or zero, returns unaltered monetary object.
same-currencies?
added in 1.0.0
(same-currencies? a b)
Returns true if both currencies are the same for the given money objects.
same-currency-ids?
added in 1.0.0
(same-currency-ids? a b)
Returns true if both currencies have the same IDs for the given money objects.
scale
added in 1.0.0
(scale money)
(scale money scale)
(scale money scale rounding-mode)
Re-scales the given money using a scale (number of decimal places) and an optional rounding mode (required when downscaling). The internal scale for a currency object is NOT updated. If no scale is given, returns the current scale of an amount (not the nominal scale of a currency – which is important in rescaled amount or auto-scaled currencies).
Auto-scaled monetary values are returned unchanged.
Use with caution since it can make money object no longer compliant with a scale of the currency.
set-amount
added in 1.2.7
(set-amount money v)
(set-amount money v rounding-mode)
Sets the amount of the given monetary object. Rescales value to a scale of the given monetary object, not the nominal scale of its currency.
strip
added in 1.0.0
(strip a)
Strips trailing zeros from the amount of money. The internal scale for a currency object is NOT updated.
Use with caution since it can make money object no longer compliant with a scale of the registered currency.
stripped-amount
added in 1.0.0
(stripped-amount money)
(stripped-amount a b)
(stripped-amount a b r)
Returns the amount of the given money with trailing zeros removed. For more than one argument the money is created ad-hoc using a and b objects passed to the function named value.
sub
added in 1.0.0
(sub a)
(sub a b)
(sub a b & more)
Subtracts two or more amounts of money of the same currency. When called with a single argument, negates its value.
sub-major
added in 1.0.0
(sub-major a b)
Decreases major amount by the given number. If the number is also expressed as money and it has decimal parts, they will be truncated.
sub-minor
added in 1.0.0
(sub-minor a b)
Decreases minor amount by the given number. If the number is also expressed as money and it has decimal parts, they will be truncated. If the major component comes from a money object, its currency must match the given money. This check is performed to prevent mistakes; if you need to subtract minor parts of money with different currencies, use (minor x) on the second argument.
sub-scaled
added in 1.0.0
(sub-scaled a)
(sub-scaled a b)
(sub-scaled a b & more)
Alias for sub.
unparse
added in 1.0.7
(unparse money)
(unparse a b)
(unparse a b r)
Returns a vector with symbolic representations of amount and currency. Useful for printing to EDN or displaying on a console. The letter M will be added to the amount if its precision exceeds 15. For more than one argument the money is created ad-hoc using a and b objects passed to the function named value.
with-currency
macro
added in 1.0.0
(with-currency currency & body)
Sets a default currency in a lexical context of the body. Has the same effect as io.randomseed.bankster.currency/with.
with-rescaling
macro
added in 1.0.0
(with-rescaling rounding-mode & body)
Alias for io.randomseed.bankster.scale/with-rescaling.
with-rounding
macro
added in 1.0.0
(with-rounding rounding-mode & body)
Alias for io.randomseed.bankster.scale/with-rounding.