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

Division and periodicity

PostgreSQL-faithful scale

div(a, b) without an explicit scale uses PostgreSQL's own select_div_scale rule — max(16 - 4·qweight, scale_a, scale_b) where qweight is the weight of the quotient's integer part. The output is digit-for-digit identical to numeric division on the database:

PostgreSQLdiv() result
1/30.33333333333333333333
10/42.5000000000000000
100/333.3333333333333333

pgDivScale(a, b) exposes the computed scale if you need it for pre-validation.

Fixed scale override

Code
div(total, 8, 2); // force scale 2 (HALF_UP) div(a, b, { scale: 6, rounding: "HALF_EVEN" }); // force scale + rounding mode

The isPeriodic flag

isPeriodic(d) is true when the last division produced a non-terminating expansion that was truncated (e.g. 1/3). The flag propagates through operations — a downstream computation on a periodic operand stays flagged, so callers can decide to round for display while keeping the precise value internally:

Code
const third = div(1, 3); isPeriodic(third); // true isPeriodic(mul(third, 2)); // true — propagates isPeriodic(add(third, "1")); // true — propagates isPeriodic(div(4, 2)); // false
Last modified on October 28, 2026
Precision modelRounding
On this page
  • PostgreSQL-faithful scale
  • Fixed scale override
  • The isPeriodic flag
TypeScript
TypeScript