Bankster library, front version 2 API money helpers.
Bankster library, front version 2 API money helpers.
(->clojure-symbol a)Converts an amount of the given money to a clojure symbol (with added M when needed).
Converts an amount of the given money to a clojure symbol (with added M when needed).
(->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.
Converts an amount of the given money to a double with optional rescaling.
(->edn money)(->edn money opts)Serializes money-like value to an EDN tagged literal string.
Serializes money-like value to an EDN tagged literal string.
(->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.
Converts an amount of the given money to a float with optional rescaling. Resulting type will be widened to double.
(->json money)(->json money opts)Serializes money-like value to a JSON string.
Serializes money-like value to a JSON string.
(->map money)Coerces a money-like value into an EDN-friendly map.
Coerces a money-like value into an EDN-friendly map.
(->symbol a)Converts an amount of the given money to a symbol.
Converts an amount of the given money to a symbol.
(abs a)Returns the absolute value of the given money.
Returns the absolute value of the given money.
This protocol is used to express (monetary) values using various numeric types and currency representations.
This protocol is used to express (monetary) values using various numeric types and currency representations.
(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.
Adds two or more monetary amounts of the same currency. Delegates to `io.randomseed.bankster.money/add`.
(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.
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 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..
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)(add-scaled a)(add-scaled a b)(add-scaled a b & more)Alias for add.
Alias for add.
(allocate money ratios)Allocates a monetary amount into parts according to integer ratios.
Delegates to io.randomseed.bankster.money/allocate.
Allocates a monetary amount into parts according to integer ratios. Delegates to `io.randomseed.bankster.money/allocate`.
(amount money)(amount a b)(amount a b rounding)Returns Money amount as BigDecimal.
Returns Money amount as `BigDecimal`.
(apply money f)(apply money f a)(apply money f a b)(apply money f a b & more)Alias for on-amount.
Alias for on-amount.
(auto-scaled? money)Returns true if the scale returned by scale is auto-scaled.
Delegates to io.randomseed.bankster.scale/auto?.
Returns `true` if the scale returned by `scale` is auto-scaled. Delegates to `io.randomseed.bankster.scale/auto?`.
(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.
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 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).
Soft variant of `cast`. Returns `nil` when casting fails (e.g. missing currency or required rounding).
(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*).
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*`).
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.
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 _)(compare a b)Compares monetary amounts of the same currency and scale.
Delegates to io.randomseed.bankster.money/compare.
Compares monetary amounts of the same currency and scale. Delegates to `io.randomseed.bankster.money/compare`.
(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.
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 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.
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 money)(currency a b)(currency a b rounding)Returns Money currency.
Returns Money currency.
(data-literal arg)Data reader for Money objects expressed as tagged literals in EDN data files.
Data reader for Money objects expressed as tagged literals in EDN data files.
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).
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).
(dec-major a)Decreases major amount by 1.
Decreases major amount by 1.
(dec-minor a)Decreases minor amount by 1.
Decreases minor amount by 1.
(default-registry)Returns the default registry (honors io.randomseed.bankster.registry/*default*).
Returns the default registry (honors `io.randomseed.bankster.registry/*default*`).
(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.
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 money n)Distributes a monetary amount into n as-even-as-possible parts.
Delegates to io.randomseed.bankster.money/distribute.
Distributes a monetary amount into `n` as-even-as-possible parts. Delegates to `io.randomseed.bankster.money/distribute`.
(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.
Divides monetary amounts or numbers according to Money rules. Delegates to `io.randomseed.bankster.money/div`.
(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.
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? _)(eq-am? a b)(eq-am? a b & more)Returns true if the monetary amounts and their currencies are equal, regardless of their scales.
Returns true if the monetary amounts and their currencies are equal, regardless of their scales.
(eq? _)(eq? a b)(eq? a b & more)Returns true if monetary amounts and currencies are equal.
Delegates to io.randomseed.bankster.money/eq?.
Returns true if monetary amounts and currencies are equal. Delegates to `io.randomseed.bankster.money/eq?`.
(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.
Formats the given amount of money as a string according to localization rules. Delegates to `io.randomseed.bankster.money/format`.
(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.
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 x)(from-edn x opts)Deserializes Money from EDN string or map.
Deserializes Money from EDN string or map.
(from-edn-text x)(from-edn-text x opts)Deserializes Money from raw EDN text.
Deserializes Money from raw EDN text.
(from-json x)(from-json x opts)Deserializes Money from JSON string or map.
Deserializes Money from JSON string or map.
(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.RoundingMode for rescalingDeserializes Money from a JSON map. Options: - `:registry` - registry to use for currency lookup (default: `registry/get`) - `:rounding-mode` - `java.math.RoundingMode` for rescaling
(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.RoundingMode for rescalingDeserializes Money from a JSON string. Options: - `:registry` - registry to use for currency lookup (default: `registry/get`) - `:rounding-mode` - `java.math.RoundingMode` for rescaling
(from-json-text x)(from-json-text x opts)Deserializes Money from raw JSON text.
Deserializes Money from raw JSON text.
(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?.
Returns true if the first monetary amount is greater than or equal to the second. Delegates to `io.randomseed.bankster.money/ge?`.
(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?.
Returns true if the first monetary amount is greater than the second. Delegates to `io.randomseed.bankster.money/gt?`.
(inc-major a)Increases major amount by 1.
Increases major amount by 1.
(inc-minor a)Increases minor amount by 1.
Increases minor amount by 1.
(info money)Returns a map with :currency and :amount for the given money-like input.
Delegates to io.randomseed.bankster.money/info.
Returns a map with `:currency` and `:amount` for the given money-like input. Delegates to `io.randomseed.bankster.money/info`.
(is-neg-or-zero? a)Returns true if the given monetary amount is a negative number or zero.
Returns true if the given monetary amount is a negative number or zero.
(is-neg? a)Returns true if the given monetary amount is a negative number.
Returns true if the given monetary amount is a negative number.
(is-pos-or-zero? a)Returns true if the given monetary amount is a positive number or zero.
Returns true if the given monetary amount is a positive number or zero.
(is-pos? a)Returns true if the given monetary amount is a positive number.
Returns true if the given monetary amount is a positive number.
(is-zero? a)Returns true if the given monetary amount is zero.
Returns true if the given monetary amount is zero.
(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?.
Returns true if the first monetary amount is less than or equal to the second. Delegates to `io.randomseed.bankster.money/le?`.
(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?.
Returns true if the first monetary amount is less than the second. Delegates to `io.randomseed.bankster.money/lt?`.
(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.
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 a)Returns the major part of the given amount as an int number widened to long.
Returns the major part of the given amount as an int number widened to long.
(major->long a)Returns the major part of the given amount as a long number.
Returns the major part of the given amount as a long number.
(major-minor a)Returns a vector with major and minor parts of the given monetary amount.
Returns a vector with major and minor parts of the given monetary amount.
(major-minor->int a)Returns a vector with major and minor parts of the given monetary amount represented as integer numbers (widened to long).
Returns a vector with major and minor parts of the given monetary amount represented as integer numbers (widened to long).
(major-minor->long a)Returns a vector with major and minor parts of the given monetary amount represented as long numbers.
Returns a vector with major and minor parts of the given monetary amount represented as long numbers.
(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.
Like `io.randomseed.bankster.money/of` but sets the amount of major part only.
(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.
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.
(max a)(max a b)(max a b & more)Alias for max-amount.
Alias for max-amount.
(max-amount a)(max-amount a b)(max-amount a b & more)Returns the greatest of the monetary amounts.
Returns the greatest of the monetary amounts.
(min a)(min a b)(min a b & more)Alias for min-amount.
Alias for min-amount.
(min-amount a)(min-amount a b)(min-amount a b & more)Returns the least of the monetary amounts.
Returns the least of the monetary amounts.
(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.
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 a)Returns the minor part of the given amount as an int number widened to long.
Returns the minor part of the given amount as an int number widened to long.
(minor->long a)Returns the minor part of the given amount as a long number.
Returns the minor part of the given amount as a long number.
(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.
Like `io.randomseed.bankster.money/of` but sets the amount of minor part only.
(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.
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? x)Returns true when x is an instance of io.randomseed.bankster.Money.
Returns `true` when `x` is an instance of `io.randomseed.bankster.Money`.
(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.
Multiplies monetary amount by number or other values as allowed. Delegates to `io.randomseed.bankster.money/mul`.
(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).
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? _)(ne-am? a b)(ne-am? a b & more)Returns true if the money amounts or their currencies are different, regardless of their scales.
Returns true if the money amounts or their currencies are different, regardless of their scales.
(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?.
Returns true if monetary amounts or currencies are not equal. Delegates to `io.randomseed.bankster.money/ne?`.
(neg a)Returns the negated amount of the given money. For a negative amount it will reverse
its sign. Same as (sub a).
Returns the negated amount of the given money. For a negative amount it will reverse its sign. Same as `(sub a)`.
(neg-or-zero? a)Alias for is-neg-or-zero?.
Alias for is-neg-or-zero?.
(neg? a)Returns true if the monetary amount is negative.
Delegates to io.randomseed.bankster.money/is-neg?.
Returns true if the monetary amount is negative. Delegates to `io.randomseed.bankster.money/is-neg?`.
(normalize amount)(normalize amount currency)(normalize amount currency rounding)Normalizes money-like input into a Money value.
([amount]) accepts Money, EDN-like map (via money/of-map), or a literal
amount (delegates to money/parse).([amount currency]) and ([amount currency rounding]) use amount-first order
and delegate to money/parse. Rounding is parsed via
scale/post-parse-rounding.Normalizes money-like input into a `Money` value. - `([amount])` accepts `Money`, EDN-like map (via `money/of-map`), or a literal amount (delegates to `money/parse`). - `([amount currency])` and `([amount currency rounding])` use amount-first order and delegate to `money/parse`. Rounding is parsed via `scale/post-parse-rounding`.
(not== a)(not== a b)(not== a b & more)Alias for ne-am?.
Alias for ne-am?.
(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.
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)(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.
Like `io.randomseed.bankster.money/of` but sets the amount of major part only.
(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:rounding-mode / :roundingThis 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.
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)(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.
Like `io.randomseed.bankster.money/of` but sets the amount of minor part only.
(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.
When registry is true, the default registry (global or dynamically bound) is used.
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. When `registry` is `true`, the default registry (global or dynamically bound) is used.
(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.
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 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.
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 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.
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 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.
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-or-zero? a)Alias for is-pos-or-zero?.
Alias for is-pos-or-zero?.
(pos? a)Returns true if the monetary amount is positive.
Delegates to io.randomseed.bankster.money/is-pos?.
Returns true if the monetary amount is positive. Delegates to `io.randomseed.bankster.money/is-pos?`.
(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:
#money and #bankster.money,#currency and #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.
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: - `#money` and `#bankster.money`, - `#currency` and `#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 registry)Resolves true or nil into the current default registry, otherwise returns the
given value.
When registry is true, the default registry (global or dynamically bound) is used.
Resolves `true` or `nil` into the current default registry, otherwise returns the given value. When `registry` is `true`, the default registry (global or dynamically bound) is used.
(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.
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 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.
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? 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.
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)(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 unary money/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.
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 unary `money/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 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.
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 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.
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 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.
Rounds a Money to a given interval. Delegates to `io.randomseed.bankster.money/round-to`.
(rounding-mode)(rounding-mode default)Returns the current rounding mode.
Delegates to io.randomseed.bankster.scale/rounding-mode.
Returns the current rounding mode. Delegates to `io.randomseed.bankster.scale/rounding-mode`.
(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?.
Returns `true` if both currencies are the same for the given money objects. Delegates to `io.randomseed.bankster.money/same-currencies?`.
(same-currency-ids? a b)Returns true if both currencies have the same IDs for the given money objects.
Returns `true` if both currencies have the same IDs for the given money objects.
(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.
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 x)(scale-apply x sc)(scale-apply x sc rounding)Applies scaling to a value.
Delegates to io.randomseed.bankster.scale/apply.
Applies scaling to a value. Delegates to `io.randomseed.bankster.scale/apply`.
(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.
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 m)Expands to an (int ...) expression computing scale for Money.
Expands to an (int ...) expression computing scale for Money.
(strip money)Strips trailing zeros from a Money amount.
Strips trailing zeros from a `Money` amount.
(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.
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 a)(sub a b)(sub a b & more)Subtracts monetary amounts of the same currency.
Delegates to io.randomseed.bankster.money/sub.
Subtracts monetary amounts of the same currency. Delegates to `io.randomseed.bankster.money/sub`.
(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.
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 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.
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 a)(sub-scaled a b)(sub-scaled a b & more)Alias for sub.
Alias for sub.
(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"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 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"Serializes a money-like value to a canonical JSON string. Options: - `:code-only?` - when truthy, namespace is omitted: `:crypto/ETH` → `"ETH"`
(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.
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 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.
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 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.
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 currency & body)Sets a default currency in a lexical context of the body.
Sets a default currency in a lexical context of the body.
(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.
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 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.
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 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.
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? money)Returns true if the given monetary amount is zero.
Delegates to io.randomseed.bankster.money/is-zero?.
Returns `true` if the given monetary amount is zero. Delegates to `io.randomseed.bankster.money/is-zero?`.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |