io.randomseed.bankster.scale

added in 1.0.0

Bankster library, scalable protocol with implementation.

*each*

dynamic

added in 1.0.0

Enables re-scaling after each consecutive operation.

*rounding-mode*

dynamic

added in 1.0.0

Default rounding mode.

->double

added in 1.1.0

(->double n)(->double n scale)(->double n rounding-mode)(->double n scale rounding-mode)

Converts to a double with optional rescaling and rounding. If the precision of double is to small to express the value, rounding must be provided (either explicitly or using rounding-mode), otherwise an exception will be thrown.

->float

added in 1.1.0

(->float n)(->float n scale)(->float n rounding-mode)(->float n scale rounding-mode)

Converts to a float with optional rescaling and rounding. If the precision of float is to small to express the value, rounding must be provided (either explicitly or using rounding-mode), otherwise an exception will be thrown.

->int

added in 1.0.0

(->int n)(->int n rounding-mode)

Converts to an int (may cast to long on call boundaries) with optional rounding.

->long

added in 1.0.0

(->long n)(->long n rounding-mode)

Converts to a long with optional rounding.

auto-scaled

added in 2.1.0

Represents the scale of a currency that is automatic and not limited to specific decimal places. Used to indicate that the scale should be determined by the actual precision of the amount, not by a fixed currency scale.

auto-scaled*?

macro

added in 2.1.0

(auto-scaled*? scale)

Macro version of auto-scaled?. Returns true if the given scale equals the auto-scaled sentinel value (-1). Inlines the comparison for performance.

auto-scaled?

added in 2.1.0

(auto-scaled? scale)

Returns true if the given scale equals the auto-scaled sentinel value (-1).

auto?

added in 2.2.0

(auto? x)

Returns true if the scale derived from of is auto-scaled.

Uses scalable? as a soft guard (avoids strict resolution on unknown inputs).

div-math-context

(div-math-context a b)(div-math-context a b rounding-mode)

Returns the MathContext set to handle the possible precision for the operation of dividing two BigDecimal numbers. Optional rounding mode may be provided.

div-max-precision

added in 1.0.0

(div-max-precision a b)

Returns the maximum possible precision for the operation of dividing two BigDecimal numbers.

each

macro

added in 1.0.0

(each & body)

Enables re-scaling on some consecutive operations which support it and sets the rounding mode for operations on scaled values. Internally sets rescale-each to true and rounding-mode to the given value.

Practically, in most cases it is better to use the with-rescaling macro which also sets rounding mode.

fractional

added in 1.0.0

(fractional n)(fractional n scale)(fractional n scale rounding-mode)

Returns the fractional part of the given number, converted to decimal if required. Optional scale and rounding mode can be given. Makes use of rounding-mode if it’s set.

integer

added in 1.0.0

(integer n)(integer n scale)(integer n scale rounding-mode)

Returns the integer part of the given number, converted to decimal if required. Optional scale and rounding mode can be given. Makes use of rounding-mode if it’s set.

monetary-scale

added in 2.1.0

(monetary-scale n sc)(monetary-scale n sc rm)

Applies monetary scaling rules to a BigDecimal-like value n and a currency scale.

When the scale indicates auto-scaling (equals auto-scaled, i.e., -1), the value is returned as-is (converted to BigDecimal if necessary). Otherwise, the value is scaled to the given number of decimal places.

This function is locale-independent and strict - it will throw ArithmeticException (wrapped in ExceptionInfo) if rounding is needed but no rounding mode is available.

Arguments:

  • n - a value convertible to BigDecimal (number, string, BigDecimal)
  • sc - target scale (long); -1 means auto-scaled (no rescaling)
  • rm - optional RoundingMode for downscaling

Returns BigDecimal.

ROUND_CEILING

added in 1.0.0

Rounding mode to round towards positive infinity.

ROUND_DOWN

added in 1.0.0

Rounding mode to round towards zero.

ROUND_FLOOR

added in 1.0.0

Rounding mode to round towards negative infinity.

ROUND_HALF_DOWN

added in 1.0.0

Rounding mode to round towards nearest neighbor, unless neighbors are equidistant, in which case round down.

ROUND_HALF_EVEN

added in 1.0.0

Rounding mode to round towards nearest neighbor, unless neighbors are equidistant, in which case round towards the nearest one.

ROUND_HALF_UP

added in 1.0.0

Rounding mode to round towards nearest neighbor, unless neighbors are equidistant, in which case round up.

ROUND_UNNECESSARY

added in 1.0.0

Indication that no rounding is necessary.

ROUND_UP

added in 1.0.0

Rounding mode to round away from zero.

Scalable

protocol

added in 1.0.0

The Scalable protocol describes values that can be scaled.

members

amount

added in 1.0.0

(amount num)(amount num scale)(amount num scale rounding-mode)

Returns the amount of a scalable as a BigDecimal number. Some scalables may not be purely numeric so this function is to get the actual, calculable value out of them.

applied?

added in 1.0.0

(applied? num)

Returns true if the value is of a type that contains scaling information.

apply

added in 1.0.0

(apply num)(apply num scale)(apply num scale rounding-mode)

Converts the given value to a scalable with or without changing its scale. For values that already are scalable it changes their scales if called with a second argument. The third argument, rounding-mode, must be present when downscaling and rounding is needed. For compound values (like monetary amounts) it will rescale the amount but will NOT update scale information of the unit (e.g. currency component).

When operating on Money objects and called with a single argument, it reapplies the nominal currency scale.

of

added in 1.0.0

(of num)

Returns a scale. If the given value is not of type that scales (or is used to produce scaled types) it will be converted to such.

scalable?

added in 1.0.0

(scalable? num)

Returns true if the value can be converted to a scalable.

to-clojure-string

added in 1.1.0

(to-clojure-string n)

Converts the amount to a plain string, adding the M suffix when needed.

to-clojure-symbol

added in 1.1.0

(to-clojure-symbol n)

Converts the amount to a symbol, adding the M suffix when needed.

to-plain-string

added in 1.0.7

(to-plain-string n)

Converts the amount to a plain string.

to-symbol

added in 1.1.0

(to-symbol n)

Converts the amount to a symbol.

unscaled-context

added in 1.0.0

MathContext indicating auto-scaling and no rounding.

with

added in 1.0.0

(with num)(with num scale)(with num scale rounding-mode)

Alias for scale/apply.

with-rescaling

macro

added in 1.0.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 1.0.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.