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

Examples

Invoice line total with VAT

Code
import { dec, add, mul, div, round, toPG, isPeriodic } from "@primebrick/decimal"; const unit = dec("19.99"); const qty = dec(3); const line = mul(unit, qty); // 59.97 const vatRate = dec("0.22"); const vat = round(mul(line, vatRate), 2); // 13.19 (HALF_UP) const total = add(line, vat); // 73.16

Splitting a bill (periodicity-aware)

Code
const total = dec("100.00"); const guests = 3; const share = div(total, guests); // 33.3333333333333333 (PG scale) isPeriodic(share); // true — non-terminating const shareRounded = round(share, 2); // 33.33 — for display // remainder: total - shareRounded*guests = 0.01 → assign cents explicitly const remainder = sub(total, mul(shareRounded, guests)); // 0.01

Money in/out of PostgreSQL

Code
// numeric column → Decimal const amount = fromPG(row.amount); // row.amount: "1234.56" // Decimal → numeric parameter await db.query("UPDATE invoices SET total = $1", [toPG(total)]);

Comparison

Code
cmp("1.0", "1") // 0 — same value eq("1.0", "1") // true strictEq("1.0", "1")// false — different scale min("5", "3.9") // 3.9 max(price, ZERO) // clamp to non-negative signum(balance) // -1 | 0 | 1

Scale control

Code
round("2.675", 2) // 2.68 — exact, unlike Number(2.675).toFixed(2) setScale("5", 2) // 5.00 — pad for display normalize("5.000") // 5 — strip zeros trunc("9.99") // 9 floor("-1.2") // -2
Last modified on October 28, 2026
When NOT to useAPI Reference
On this page
  • Invoice line total with VAT
  • Splitting a bill (periodicity-aware)
  • Money in/out of PostgreSQL
  • Comparison
  • Scale control
TypeScript
TypeScript
TypeScript
TypeScript
TypeScript