PrimeBrickPrimeBrick
  • Docs
  • Contact
  • MIT License
  • Documentation
  • MCP Server
  • API Catalog
  • Services
  • Libraries
PrimeBrickPrimeBrick

© 2026 PrimeBrick. MIT License. v3.11.2

github
DAL Library
SDK Library
Decimal Library
    Developer Guide
      Getting startedPrecision modelDivision and periodicityRoundingWire formatWhen to useWhen NOT to useExamplesAPI ReferenceContributing
powered by Zudoku
Developer Guide

title: API Reference description: Complete API reference for @primebrick/decimal — every exported symbol.

API Reference

Every exported symbol from @primebrick/decimal, rendered mechanically from the TypeDoc extraction. This page is regenerated from docs/dev-guide/_extracted/api.json on every pnpm extract-docs run — do not edit by hand.

Classes

Decimal

PropertyTypeDescription
vbigintUnscaled integer: value = v × 10^-scale.
scalenumberNumber of base-10 digits after the decimal point. Always >= 0.
periodicbooleanProvenance flag: the producing operation truncated a non-terminating expansion.

Decimal(v, scale, periodic)

Internal constructor — prefer dec(), fromPG(), fromNumber(), fromBigInt().

ParameterTypeDescription
vbigint
scalenumber
periodicboolean

Decimal.toString(): string

Canonical plain decimal string, e.g. -12.3400 (scale preserved).

Decimal.toJSON(): string

Wire form for ext-json / JSON.stringify: "12.34d" — a self-describing string so the value survives naive JSON.parse visibly typed and can be revived by schema-aware hydration (fromWire).

Decimal.toNumber(): number

Lossy coercion — may round. Use only for display hints, never for math.

DecimalError

Errors raised by @primebrick/decimal.

PropertyTypeDescription
code"INVALID_INPUT" | "NON_FINITE" | "DIVISION_BY_ZERO" | "INVALID_SCALE" | "INVALID_ROUNDING"

DecimalError(code, message)

ParameterTypeDescription
code"INVALID_INPUT" | "NON_FINITE" | "DIVISION_BY_ZERO" | "INVALID_SCALE" | "INVALID_ROUNDING"
messagestring

Types & Enums

DecimalInput

Anything coercion functions accept: Decimal, native number, bigint, or a decimal string.

Type: Decimal \| number \| bigint \| string

RoundingMode

Type: "UP" \| "DOWN" \| "CEILING" \| "FLOOR" \| "HALF_UP" \| "HALF_DOWN" \| "HALF_EVEN"

Constants

fromPG

Alias of dec() — parse a PostgreSQL numeric/decimal text value.

Type: (input: DecimalInput) => Decimal

fromString

Type: (input: DecimalInput) => Decimal

fromWire

Alias of dec() — tolerates the "…d" wire suffix.

Type: (input: DecimalInput) => Decimal

NUMERIC_MIN_SIG_DIGITS

Minimum significant digits preserved by automatic div scale — mirrors PostgreSQL NUMERIC_MIN_SIG_DIGITS (16). See pgDivScale.

Type: 16

ONE

Type: Decimal

TEN

Type: Decimal

ZERO

Type: Decimal

Functions

abs(d): Decimal

ParameterTypeDescription
dDecimalInput

add(a, b): Decimal

ParameterTypeDescription
aDecimalInput
bDecimalInput

ceil(d): Decimal

Smallest integer >= d.

ParameterTypeDescription
dDecimalInput

cmp(a, b): -1 \| 0 \| 1

Numeric comparison ignoring scale: cmp(1.0, 1.00) === 0. Returns -1 | 0 | 1.

ParameterTypeDescription
aDecimalInput
bDecimalInput

dec(input): Decimal

Decimal — pass-through. string — parsed (PG text form or "…d" wire form). number — via shortest round-trip toString so 0.1 stays "0.1". bigint — scale 0.

ParameterTypeDescription
inputDecimalInput

div(a, b, opts): Decimal

a / b. Without scale, the result uses the PostgreSQL numeric division rule (pgDivScale — e.g. 1/3 → scale 20, 10/4 → 16), so Decimal↔DB computations compare equal digit-for-digit. Rounding defaults to HALF_UP. When the true quotient does not terminate at the target scale the result is flagged periodic (isPeriodic(d) → true).

div(1, 3) // 0.33333333333333333333 — periodic div(10, 4) // 2.5000000000000000 div(1, 3, 2) // 0.33 div(a, b, { scale: 6, rounding: "HALF_EVEN" })

ParameterTypeDescription
aDecimalInput
bDecimalInput
optsnumber | { scale: number; rounding: RoundingMode }

eq(a, b): boolean

Numeric equality ignoring scale — eq(1.0, 1.00) is true.

ParameterTypeDescription
aDecimalInput
bDecimalInput

floor(d): Decimal

Largest integer <= d.

ParameterTypeDescription
dDecimalInput

fromBigInt(v, scale): Decimal

ParameterTypeDescription
vbigint
scalenumber

fromNumber(n): Decimal

ParameterTypeDescription
nnumber

isDecimal(v): v is Decimal

ParameterTypeDescription
vunknown

isPeriodic(d): boolean

true when the value was produced by a truncated non-terminating division.

ParameterTypeDescription
dDecimalInput

max(a, b): Decimal

ParameterTypeDescription
aDecimalInput
bDecimalInput

min(a, b): Decimal

ParameterTypeDescription
aDecimalInput
bDecimalInput

mod(a, b): Decimal

a mod b — remainder of truncated division at the common scale.

ParameterTypeDescription
aDecimalInput
bDecimalInput

mul(a, b): Decimal

ParameterTypeDescription
aDecimalInput
bDecimalInput

neg(d): Decimal

ParameterTypeDescription
dDecimalInput

normalize(d): Decimal

Strip trailing fractional zeros — 1.230 → 1.23, 12.000 → 12.

ParameterTypeDescription
dDecimalInput

parseDecimal(input): Decimal

Parse a decimal string into unscaled form. Accepts: "12", "-0.5", ".5", "5.", "1.5e3", "1E-3", "+4". Rejects: NaN, Infinity, empty strings, hex, non-numeric garbage. Scale is preserved as written ("1.230" → scale 3).

ParameterTypeDescription
inputstring

pgDivScale(a, b): number

PostgreSQL-faithful result scale for x / y (mirrors select_div_scale): max(16 - 4·qweight, x.scale, y.scale) — 16 significant digits minimum. Verified live: 1/3→20, 10/4→16, 1.00/8→20, 2/1→16, 100/3→16.

ParameterTypeDescription
aDecimalInput
bDecimalInput

pow10(n): bigint

ParameterTypeDescription
nnumber

rescale(d, newScale, mode): Decimal

Re-express d at newScale. Growing is exact; shrinking applies mode (default HALF_UP, matching PostgreSQL round()).

ParameterTypeDescription
dDecimalInput
newScalenumber
modeRoundingMode

round(d, scale, mode): Decimal

Round to scale fractional digits (default HALF_UP).

ParameterTypeDescription
dDecimalInput
scalenumber
modeRoundingMode

signum(d): -1 \| 0 \| 1

ParameterTypeDescription
dDecimalInput

strictEq(a, b): boolean

Exact structural equality — value AND scale must match (strictEq(1.0, 1.00) is false).

ParameterTypeDescription
aDecimalInput
bDecimalInput

sub(a, b): Decimal

ParameterTypeDescription
aDecimalInput
bDecimalInput

toPG(d): string

PostgreSQL parameter form — plain canonical decimal string, no d suffix.

ParameterTypeDescription
dDecimalInput

toWire(d): string

Wire form — "…d" suffixed string.

ParameterTypeDescription
dDecimalInput

trunc(d): Decimal

Integer part, toward zero.

ParameterTypeDescription
dDecimalInput
Last modified on October 28, 2026
ExamplesContributing
On this page
  • Classes
    • Decimal
    • DecimalError
  • Types & Enums
    • DecimalInput
    • RoundingMode
  • Constants
    • fromPG
    • fromString
    • fromWire
    • NUMERIC_MIN_SIG_DIGITS
    • ONE
    • TEN
    • ZERO
  • Functions
    • abs(d): Decimal
    • add(a, b): Decimal
    • ceil(d): Decimal
    • cmp(a, b): -1 \| 0 \| 1
    • dec(input): Decimal
    • div(a, b, opts): Decimal
    • eq(a, b): boolean
    • floor(d): Decimal
    • fromBigInt(v, scale): Decimal
    • fromNumber(n): Decimal
    • isDecimal(v): v is Decimal
    • isPeriodic(d): boolean
    • max(a, b): Decimal
    • min(a, b): Decimal
    • mod(a, b): Decimal
    • mul(a, b): Decimal
    • neg(d): Decimal
    • normalize(d): Decimal
    • parseDecimal(input): Decimal
    • pgDivScale(a, b): number
    • pow10(n): bigint
    • rescale(d, newScale, mode): Decimal
    • round(d, scale, mode): Decimal
    • signum(d): -1 \| 0 \| 1
    • strictEq(a, b): boolean
    • sub(a, b): Decimal
    • toPG(d): string
    • toWire(d): string
    • trunc(d): Decimal