io.randomseed.bankster.api.money
added in 2.2.0
Bankster library, front API money helpers.
->clojure-symbol
added in 2.2.0
(->clojure-symbol a)Converts an amount of the given money to a clojure symbol (with added M when needed).
->double
added in 2.2.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.
->edn
added in 2.2.0
(->edn money)(->edn money opts)Serializes money-like value to an EDN tagged literal string.
->float
added in 2.2.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. Resulting type will be widened to double.
->json
added in 2.2.0
(->json money)(->json money opts)Serializes money-like value to a JSON string.
->LastMoney
added in 2.2.0
(->LastMoney m scale auto-scaled)Positional factory function for class io.randomseed.bankster.money.LastMoney.
Accountable
protocol
added in 2.2.0
This protocol is used to express (monetary) values using various numeric types and currency representations.
add
added in 2.2.0
(add)(add a)(add a b)(add a b & more)Adds two or more monetary amounts of the same currency.
Delegates to io.randomseed.bankster.money/add.
add-major
added in 2.2.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 2.2.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 2.2.0
(add-scaled)(add-scaled a)(add-scaled a b)(add-scaled a b & more)Alias for add.
allocate
added in 2.2.0
(allocate money ratios)Allocates a monetary amount into parts according to integer ratios.
Delegates to io.randomseed.bankster.money/allocate.
amount
added in 2.2.0
(amount money)(amount a b)(amount a b rounding)Returns Money amount as BigDecimal.
apply
added in 2.2.0
(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 2.2.0
(auto-scaled? money)Returns true if the scale returned by scale is auto-scaled.
Delegates to io.randomseed.bankster.scale/auto?.
cast
added in 2.2.0
(cast money)(cast money currency)(cast money currency rounding)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.
Delegates to io.randomseed.bankster.money/cast.
cast-try
added in 2.2.0
(cast-try money)(cast-try money currency)(cast-try money currency rounding)Soft variant of cast.
Returns nil when casting fails (e.g. missing currency or required rounding).
code-literal
added in 2.2.0
(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 2.2.0
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 2.2.0
(compare _)(compare a b)Compares monetary amounts of the same currency and scale.
Delegates to io.randomseed.bankster.money/compare.
compare-amounts
added in 2.2.0
(compare-amounts _)(compare-amounts a b)Compares two monetary amounts of the same currency, regardless of their scales. Returns -1 if the first one is less than the second, 0 if equal to, and 1 if it is greater than the second. Nil values are always considered lower when comparing.
convert
added in 2.2.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 2.2.0
(currency money)(currency a b)(currency a b rounding)Returns Money currency.
data-literal
added in 2.2.0
(data-literal arg)Data reader for Money objects expressed as tagged literals in EDN data files.
data-readers
added in 2.2.0
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).
default-registry
added in 2.2.0
(default-registry)Returns the default registry (honors io.randomseed.bankster.registry/*default*).
defliteral
added in 2.2.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.
distribute
added in 2.2.0
(distribute money n)Distributes a monetary amount into n as-even-as-possible parts.
Delegates to io.randomseed.bankster.money/distribute.
div
added in 2.2.0
(div a)(div a b)(div a b & more)Divides monetary amounts or numbers according to Money rules.
Delegates to io.randomseed.bankster.money/div.
div-scaled
added in 2.2.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 more than 2 arguments, if currency cancels out (Money divided by Money), the intermediate result becomes a regular number, so all consequent divisors must also be regular numbers.
For a single value it returns a division of 1 by the given number. For a monetary value it will throw, because dividing a regular number by Money is not allowed.
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 2.2.0
(eq-am? _)(eq-am? a b)(eq-am? a b & more)Returns true if the monetary amounts and their currencies are equal, regardless of their scales.
eq?
added in 2.2.0
(eq? _)(eq? a b)(eq? a b & more)Returns true if monetary amounts and currencies are equal.
Delegates to io.randomseed.bankster.money/eq?.
format
added in 2.2.0
(format money)(format money locale)(format money locale opts)Formats the given amount of money as a string according to localization rules.
Delegates to io.randomseed.bankster.money/format.
format-with
added in 2.2.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.
from-edn
added in 2.2.0
(from-edn x)(from-edn x opts)Deserializes Money from EDN string or map.
from-edn-text
added in 2.2.0
(from-edn-text x)(from-edn-text x opts)Deserializes Money from raw EDN text.
from-json
added in 2.2.0
(from-json x)(from-json x opts)Deserializes Money from JSON string or map.
from-json-map
added in 2.2.0
(from-json-map m)(from-json-map m opts)Deserializes Money from a JSON map.
Options:
:registry- registry to use for currency lookup (default:registry/get):rounding-mode-java.math.RoundingModefor rescaling
from-json-string
added in 2.2.0
(from-json-string s)(from-json-string s opts)Deserializes Money from a JSON string.
Options:
:registry- registry to use for currency lookup (default:registry/get):rounding-mode-java.math.RoundingModefor rescaling
from-json-text
added in 2.2.0
(from-json-text x)(from-json-text x opts)Deserializes Money from raw JSON text.
ge?
added in 2.2.0
(ge? _)(ge? a b)(ge? a b & more)Returns true if the first monetary amount is greater than or equal to the second.
Delegates to io.randomseed.bankster.money/ge?.
gt?
added in 2.2.0
(gt? _)(gt? a b)(gt? a b & more)Returns true if the first monetary amount is greater than the second.
Delegates to io.randomseed.bankster.money/gt?.
info
added in 2.2.0
(info money)Returns a map with :currency and :amount for the given money-like input.
Delegates to io.randomseed.bankster.money/info.
is-neg-or-zero?
added in 2.2.0
(is-neg-or-zero? a)Returns true if the given monetary amount is a negative number or zero.
is-neg?
added in 2.2.0
(is-neg? a)Returns true if the given monetary amount is a negative number.
is-pos-or-zero?
added in 2.2.0
(is-pos-or-zero? a)Returns true if the given monetary amount is a positive number or zero.
is-pos?
added in 2.2.0
(is-pos? a)Returns true if the given monetary amount is a positive number.
le?
added in 2.2.0
(le? _)(le? a b)(le? a b & more)Returns true if the first monetary amount is less than or equal to the second.
Delegates to io.randomseed.bankster.money/le?.
lt?
added in 2.2.0
(lt? _)(lt? a b)(lt? a b & more)Returns true if the first monetary amount is less than the second.
Delegates to io.randomseed.bankster.money/lt?.
major
added in 2.2.0
(major amount)(major amount currency)(major amount currency rounding)(major amount currency registry)(major amount currency rounding registry)First-class constructor for major-part amounts (amount-first).
Delegates to io.randomseed.bankster.money/major-value.
When registry is true, the default registry (global or dynamically bound) is used.
major->int
added in 2.2.0
(major->int a)Returns the major part of the given amount as an int number widened to long.
major->long
added in 2.2.0
(major->long a)Returns the major part of the given amount as a long number.
major-minor
added in 2.2.0
(major-minor a)Returns a vector with major and minor parts of the given monetary amount.
major-minor->int
added in 2.2.0
(major-minor->int a)Returns a vector with major and minor parts of the given monetary amount represented as integer numbers (widened to long).
major-minor->long
added in 2.2.0
(major-minor->long a)Returns a vector with major and minor parts of the given monetary amount represented as long numbers.
major-of
macro
added in 2.2.0
(major-of)(major-of a)(major-of a b)(major-of a b rounding-mode)Like io.randomseed.bankster.money/of but sets the amount of major part only.
major-value
added in 2.2.0
(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 will 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 a convenient way to go: of, of-major, of-minor.
map->LastMoney
added in 2.2.0
(map->LastMoney m__8001__auto__)Factory function for class io.randomseed.bankster.money.LastMoney, taking a map of keywords to field values.
max
added in 2.2.0
deprecated in 2.2.3
(max a)(max a b)(max a b & more)Alias for max-amount.
max-amount
added in 2.2.0
(max-amount a)(max-amount a b)(max-amount a b & more)Returns the greatest of the monetary amounts.
min
added in 2.2.0
deprecated in 2.2.3
(min a)(min a b)(min a b & more)Alias for min-amount.
min-amount
added in 2.2.0
(min-amount a)(min-amount a b)(min-amount a b & more)Returns the least of the monetary amounts.
minor
added in 2.2.0
(minor amount)(minor amount currency)(minor amount currency rounding)(minor amount currency registry)(minor amount currency rounding registry)First-class constructor for minor-part amounts (amount-first).
Delegates to io.randomseed.bankster.money/minor-value.
When registry is true, the default registry (global or dynamically bound) is used.
minor->int
added in 2.2.0
(minor->int a)Returns the minor part of the given amount as an int number widened to long.
minor->long
added in 2.2.0
(minor->long a)Returns the minor part of the given amount as a long number.
minor-of
macro
added in 2.2.0
(minor-of)(minor-of a)(minor-of a b)(minor-of a b rounding-mode)Like io.randomseed.bankster.money/of but sets the amount of minor part only.
minor-value
added in 2.2.0
(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 will 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 a convenient way to go: of, of-major, of-minor.
money?
added in 2.2.0
(money? x)Returns true when x is an instance of io.randomseed.bankster.Money.
mul
added in 2.2.0
(mul)(mul a)(mul a b)(mul a b & more)Multiplies monetary amount by number or other values as allowed.
Delegates to io.randomseed.bankster.money/mul.
mul-scaled
added in 2.2.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 2.2.0
(ne-am? _)(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 2.2.0
(ne? _)(ne? a b)(ne? a b & more)Returns true if monetary amounts or currencies are not equal.
Delegates to io.randomseed.bankster.money/ne?.
neg
added in 2.2.0
(neg a)Returns the negated amount of the given money. For a negative amount it will reverse its sign. Same as (sub a).
neg?
added in 2.2.0
(neg? a)Returns true if the monetary amount is negative.
Delegates to io.randomseed.bankster.money/is-neg?.
normalize
added in 2.2.0
(normalize amount)(normalize amount currency)(normalize amount currency rounding)Normalizes money-like input into a Money value.
([amount])acceptsMoney, EDN-like map (viamoney/of-map), or a literal amount (delegates tomoney/parse).([amount currency])and([amount currency rounding])use amount-first order and delegate tomoney/parse. Rounding is parsed viascale/post-parse-rounding.
not==
added in 2.2.0
deprecated in 2.2.3
(not== a)(not== a b)(not== a b & more)Alias for ne-am?.
of
macro
added in 2.2.0
(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 zeroUNNECESSARY- 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
added in 2.2.0
(of-major)(of-major a)(of-major a b)(of-major a b rounding-mode)Like io.randomseed.bankster.money/of but sets the amount of major part only.
of-map
added in 2.2.0
(of-map m)Creates Money from a map representation.
Expected keys:
:currency(or:cur) - any currency representation accepted by Bankster:amount- amount as BigDecimal/number/string- optional
:rounding-mode/:rounding
This is a general-purpose helper meant for EDN-like data. It accepts keyword/symbol rounding-mode names (e.g. :HALF_UP), not only java.math.RoundingMode.
of-minor
macro
added in 2.2.0
(of-minor)(of-minor a)(of-minor a b)(of-minor a b rounding-mode)Like io.randomseed.bankster.money/of but sets the amount of minor part only.
of-registry
added in 2.2.0
(of-registry money)(of-registry registry money)(of-registry registry money rounding)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.
When registry is true, the default registry (global or dynamically bound) is used.
on-amount
added in 2.2.0
(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.
parse
added in 2.2.0
(parse amount)(parse currency amount)(parse currency amount rounding)Parses money-like input into a Money value.
Delegates to io.randomseed.bankster.money/parse. Rounding is parsed via scale/post-parse-rounding.
parse-major
added in 2.2.0
(parse-major amount)(parse-major currency amount)(parse-major currency amount rounding)Parses money-like input and treats the amount as major part.
Delegates to io.randomseed.bankster.money/parse-major. Rounding is parsed via scale/post-parse-rounding.
parse-minor
added in 2.2.0
(parse-minor amount)(parse-minor currency amount)(parse-minor currency amount rounding)Parses money-like input and treats the amount as minor part.
Delegates to io.randomseed.bankster.money/parse-minor. Rounding is parsed via scale/post-parse-rounding.
pos?
added in 2.2.0
(pos? a)Returns true if the monetary amount is positive.
Delegates to io.randomseed.bankster.money/is-pos?.
readers
added in 2.2.0
(readers)(readers registry)Returns a data readers map (tag symbol -> handler function) suitable for clojure.edn/read / clojure.edn/read-string.
The returned map always includes the base tags:
#moneyand#bankster.money,#currencyand#bankster.currency.
When a registry is available it also includes tags of form #money/NS (and #bankster.money/NS) for each currency namespace present in the registry, so EDN can disambiguate codes between currency domains (e.g. #money/crypto[1 ETH]).
Note: this helper does not mutate clojure.core/*data-readers*.
When registry is true, the default registry (global or dynamically bound) is used.
registry-or-default
added in 2.2.0
(registry-or-default registry)Resolves true or nil into the current default registry, otherwise returns the given value.
rem
added in 2.2.0
(rem a b)(rem a b rounding-mode)Returns the remainder of dividing an amount of money by the given number. For two Money objects the result is a BigDecimal. When only the first argument is a kind of Money, the result will also be a Money, rescaled to the scale of the monetary amount. When the rounding mode is not given, io.randomseed.bankster.scale/*rounding-mode* will be used when set.
rescale
added in 2.2.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 2.2.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.
resolve
added in 2.2.0
(resolve)(resolve amount)(resolve amount currency)(resolve amount currency rounding)(resolve amount currency registry)(resolve amount currency rounding registry)First-class Money constructor with amount-first argument order.
Delegates to io.randomseed.bankster.money/value and resolves currency via io.randomseed.bankster.currency/unit when a currency argument is provided.
Arity:
[]creates zero in the default currency.[amount]delegates to unarymoney/value.[amount currency]uses amount + currency.[amount currency rounding]uses amount + currency + rounding.[amount currency registry]uses amount + currency (resolved using registry).[amount currency rounding registry]explicit rounding + registry for resolution.
currency may be a Currency, an identifier (keyword/symbol/string/number), or a currency map (lookup spec).
When a registry is consulted and the currency cannot be resolved, it throws.
When registry is true, the default registry (global or dynamically bound) is used.
rounding may be a java.math.RoundingMode or a keyword/symbol/string like “HALF_UP” (see scale/post-parse-rounding). If no default currency is configured and the amount does not encode one, money/value will throw.
resolve-try
(resolve-try)(resolve-try amount)(resolve-try amount currency)(resolve-try amount currency rounding)(resolve-try amount currency registry)(resolve-try amount currency rounding registry)Non-throwing variant of resolve for currency resolution.
Uses io.randomseed.bankster.currency/unit-try and returns nil when the currency cannot be resolved in the given (or default) registry. Accepts the same argument shapes as resolve. Rounding is parsed.
When registry is true, the default registry (global or dynamically bound) is used.
round
added in 2.2.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 2.2.0
(round-to money)(round-to money interval)(round-to money interval rounding)Rounds a Money to a given interval.
Delegates to io.randomseed.bankster.money/round-to.
rounding-mode
added in 2.2.0
(rounding-mode)(rounding-mode default)Returns the current rounding mode.
Delegates to io.randomseed.bankster.scale/rounding-mode.
same-currencies?
added in 2.2.0
(same-currencies? a b)Returns true if both currencies are the same for the given money objects.
Delegates to io.randomseed.bankster.money/same-currencies?.
same-currency-ids?
added in 2.2.0
(same-currency-ids? a b)Returns true if both currencies have the same IDs for the given money objects.
scale
added in 2.2.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 the amount (not the nominal scale of a currency). For auto-scaled currencies this means the scale tracks the current value (it grows/shrinks to fit the amount).
Use with caution since it can make money object no longer compliant with a scale of the currency.
scale-apply
added in 2.2.0
(scale-apply x)(scale-apply x sc)(scale-apply x sc rounding)Applies scaling to a value.
Delegates to io.randomseed.bankster.scale/apply.
set-amount
added in 2.2.0
(set-amount money v)(set-amount money v rounding-mode)Sets the amount of the given monetary object. Rescales value v to a scale of the given monetary object, not the nominal scale of its currency.
split-scale-int
macro
added in 2.2.0
(split-scale-int m)Expands to an (int …) expression computing scale for Money.
stripped-amount
added in 2.2.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 2.2.0
(sub a)(sub a b)(sub a b & more)Subtracts monetary amounts of the same currency.
Delegates to io.randomseed.bankster.money/sub.
sub-major
added in 2.2.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 2.2.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 2.2.0
(sub-scaled a)(sub-scaled a b)(sub-scaled a b & more)Alias for sub.
to-json-map
added in 2.2.0
(to-json-map money)(to-json-map money opts)Serializes a money-like value to a JSON-friendly map.
The returned map uses keyword keys but is safe to pass to JSON encoders.
Options:
:code-only?- when truthy, namespace is omitted::crypto/ETH→"ETH"
to-json-string
added in 2.2.0
(to-json-string money)(to-json-string money opts)Serializes a money-like value to a canonical JSON string.
Options:
:code-only?- when truthy, namespace is omitted::crypto/ETH→"ETH"
to-map
added in 2.2.0
(to-map money)Coerces a money-like value into an EDN-friendly map.
Canonical shape:
:currency- currency ID keyword (may be namespaced, e.g.:crypto/USDT):amount- BigDecimal amount (scale preserved)
This is intended as a stable, data-oriented representation (close to what Bankster already supports in #money{...} literals). For JSON wire formats see to-json-map / to-json-string.
unparse
added in 2.2.0
(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.
value
added in 2.2.0
(value num)(value currency num)(value currency num rounding-mode)Creates new Money object for the given value num which will become an amount. If the currency is not given it will 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.
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.
with-currency
macro
added in 2.2.0
(with-currency currency & body)Sets a default currency in a lexical context of the body.
with-registry
macro
added in 2.2.0
(with-registry registry & body)Sets a registry in a lexical context of the body to be used instead of a global one in functions which require the registry and it was not passed as an argument.
with-rescaling
macro
added in 2.2.0
(with-rescaling rounding-mode & body)Enables re-scaling on some consecutive operations which support it and sets the rounding mode for operations on scaled values. Internally sets *each* to true and *rounding-mode* to the given value.
The first argument should be a valid rounding (from io.randomseed.bankster.scale or java.math.RoundingMode) or one 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.
with-rounding
macro
added in 2.2.0
(with-rounding rounding-mode & body)Sets the rounding mode for operations on scaled values.
The first argument should be a valid rounding (from io.randomseed.bankster.scale or java.math.RoundingMode) or one 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.
zero?
added in 2.2.0
(zero? money)Returns true if the given monetary amount is zero.
Delegates to io.randomseed.bankster.money/is-zero?.