Metakit / @doeixd/metakit/domain

Decimal

namespace · packages/metakit/src/domain/decimal.ts:7

export * as Decimal from './domain/decimal.js'

Fixed-scale integer-backed Decimal Schema (@doeixd/metakit/domain) with compile-time scale safety, exact decimal text parsing and formatting, half-even integer rounding, and symbolic arithmetic lowering to num.*, ord.* and eq.*.

In the guides

Members

Decimal.add

const · packages/metakit/src/domain/decimal.ts:74

const add: <
  L extends ExprInput<never, DecimalValue<number>>,
  R extends Same<L>,
>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, ValueOfInput<L>>

Add two Decimal expressions of the same scale.

Decimal.between

const · packages/metakit/src/domain/decimal.ts:92

const between: <
  X extends ExprInput<never, DecimalValue<number>>,
  Lo extends Same<X>,
  Hi extends Same<X>,
>(
  value: X,
  lo: Lo,
  hi: Hi,
) => Expr<EnvOfInput<X> & EnvOfInput<Lo> & EnvOfInput<Hi>, boolean>

lo <= value <= hi, all at one scale.

Decimal.DecimalScaleBrand

const · packages/metakit/src/domain/decimal.ts:13

const DecimalScaleBrand: unique symbol

Decimal.DecimalValue

type · packages/metakit/src/domain/decimal.ts:16

type DecimalValue<S extends number = 2> = number & {
  readonly [DecimalScaleBrand]: S
}

Integer count of fixed-scale decimal units (10^-S).

Decimal.format

function · packages/metakit/src/domain/decimal.ts:66

function format<S extends number>(
  value: DecimalValue<S>,
  options: {
    readonly scale: NoInfer<S>
  },
): string

Format integer units as exact fixed-decimal text (1250 at scale 2 is "12.50"). The scale is required and must be the value's own: a DecimalValue<4> formatted at scale 2 does not compile. Throws on a value that is not a safe integer.

Decimal.gt

const · packages/metakit/src/domain/decimal.ts:87

const gt: <L extends ExprInput<never, DecimalValue<number>>, R extends Same<L>>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, boolean>

Decimal.gte

const · packages/metakit/src/domain/decimal.ts:88

const gte: <
  L extends ExprInput<never, DecimalValue<number>>,
  R extends Same<L>,
>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, boolean>

Decimal.InvalidDecimalError

class · packages/metakit/src/domain/fixed.ts:18

class InvalidDecimalError extends Data.TaggedError('InvalidDecimalError')<{
  readonly input: string
  readonly reason: string
}> {}

Decimal.is

const · packages/metakit/src/domain/decimal.ts:89

const is: <L extends ExprInput<never, DecimalValue<number>>, R extends Same<L>>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, boolean>

Decimal.isNot

const · packages/metakit/src/domain/decimal.ts:90

const isNot: <
  L extends ExprInput<never, DecimalValue<number>>,
  R extends Same<L>,
>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, boolean>

Decimal.lt

const · packages/metakit/src/domain/decimal.ts:85

const lt: <L extends ExprInput<never, DecimalValue<number>>, R extends Same<L>>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, boolean>

Decimal.lte

const · packages/metakit/src/domain/decimal.ts:86

const lte: <
  L extends ExprInput<never, DecimalValue<number>>,
  R extends Same<L>,
>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, boolean>

Decimal.make

function · packages/metakit/src/domain/decimal.ts:30

function make<const S extends number = 2>(options?: {
  readonly scale?: S
}): Schema.Codec<DecimalValue<S>, number>

Construct an integer-backed fixed-scale Decimal Schema (default scale: 2). Two Decimal values with different scales cannot be mixed in arithmetic or comparisons at compile time.

Decimal.max

const · packages/metakit/src/domain/decimal.ts:84

const max: <
  L extends ExprInput<never, DecimalValue<number>>,
  R extends Same<L>,
>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, ValueOfInput<L>>

The larger of two Decimal expressions of the same scale.

Decimal.min

const · packages/metakit/src/domain/decimal.ts:82

const min: <
  L extends ExprInput<never, DecimalValue<number>>,
  R extends Same<L>,
>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, ValueOfInput<L>>

The smaller of two Decimal expressions of the same scale.

Decimal.mul

function · packages/metakit/src/domain/decimal.ts:103

function mul<
  L extends MaybeDecimalInput,
  R extends MaybeDecimalInput,
  const T extends number,
>(
  left: L,
  right: R,
  options: {
    readonly leftScale: NoInfer<ScaleOf<ValueOfInput<L>>>
    readonly rightScale: NoInfer<ScaleOf<ValueOfInput<R>>>
    readonly scale: T
  },
): Expr<EnvOfInput<L> & EnvOfInput<R>, DecimalValue<T> | Fixed.NullIn<L | R>>

Multiply two decimals into a decimal at scale, rounding half-even. The product is computed exactly in integers; leftScale and rightScale must be the operands' own scales (checked by their types, and against a field's Schema). It builds num.mulScaled, which evaluation, SQL (SQLite and Postgres), the checkers and Z3 read or refuse by name. A missing operand evaluates to null (typed so when an operand may be null); a product of units outside the safe integer range is refused.

Decimal.mulInt

const · packages/metakit/src/domain/decimal.ts:80

const mulInt: <
  L extends ExprInput<never, DecimalValue<number>>,
  F extends ExprInput<never, number>,
>(
  amount: L,
  factor: F,
) => Expr<Env2<L, F>, ValueOfInput<L>>

Multiply a Decimal expression by a dimensionless integer factor (e.g. quantity).

Decimal.neg

const · packages/metakit/src/domain/decimal.ts:78

const neg: <X extends ExprInput<never, DecimalValue<number>>>(
  value: X,
) => Expr<EnvOfInput<X>, ValueOfInput<X>>

Negate a Decimal expression, keeping its scale.

Decimal.parse

function · packages/metakit/src/domain/decimal.ts:51

function parse<const S extends number = 2>(
  text: string,
  options?: {
    readonly scale?: S
  },
): Result.Result<DecimalValue<S>, Fixed.InvalidDecimalError>

Parse an exact base-10 decimal string (e.g. "-12.50") into integer units at scale (default 2) with no floating-point drift. Refuses text with more fractional digits than scale and a result outside the safe integer range.

Decimal.roundHalfEven

function · packages/metakit/src/domain/fixed.ts:85

function roundHalfEven(numerator: number, divisor: number): number

Exact half-even (banker's) rounding of numerator / divisor, both safe integers and divisor > 0. Integer arithmetic only, so no float drift.

Decimal.sub

const · packages/metakit/src/domain/decimal.ts:76

const sub: <
  L extends ExprInput<never, DecimalValue<number>>,
  R extends Same<L>,
>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, ValueOfInput<L>>

Subtract two Decimal expressions of the same scale.

Decimal.units

function · packages/metakit/src/domain/decimal.ts:41

function units<const S extends number = 2>(
  amount: number,
  scale?: S,
): DecimalValue<S>

Construct a typed DecimalValue<S> from a safe integer unit count (default scale: 2).